How to exit and cancel all pending orders using node js sdk

How to exit and cancel all pending orders using node js sdk

I am using node js sdk for placing intraday orders

For example when i place buy order and once this order execute, i am placing new order for stoploss and one for target, so there are 3 order 1 already execute and two orders are pending, before target or stoploss if i want to exit position and cancel order, I am using code like below example:

**Endpoint: **https://api-t1.fyers.in/api/v3/positions
and
post data like: {“id”: “NSE:SBIN-EQ-INTRADAY”,“pending_orders_cancel”: 1}

it’s close running position but it’s not cancelling target and stoploss orders.

How to exit and cancel all pending orders of that particular symbol ie NSE:SBIN-EQ

Hi Vishal,

To cancel all pending orders, you need to use the DELETE method. It appears that you may currently be sending a POST API request instead.

Naresh thank you for your response, i am using node js SDK, so do i need to send DELETE request through axios manually or i think it’s being handle through sdk?

following is my current code example, i am using for exit NSE:SBIN-EQ current position and it’s pending orders. is this correct way?

const FyersAPI = require("fyers-api-v3").fyersModel;
const exitReqBody = {
        id: "NSE:SBIN-EQ-INTRADAY",
        pending_orders_cancel: 1,
      };
const response = await fyers.exit_position(exitReqBody);

Hi Vishal,

Unfortunately, this particular API is not available in the API, we will get this updated in the SDK. With the REST API call, you need to use the DELETE method to cancel the orders.

I check sdk function i can see there is exit_positions, will following will not work?

exit_position = async (req) => {
        var funcname = 'exit_position'
        var logger = this.Logger
        let AuthrizationToken = this.AccessToken
        let vers = this.Version
        return new Promise(async function (resolve, reject) {
            try {
                var url = Config.SYNC_API + Config["positions"]
                await axios.delete((url), {
                    headers: {
                        Authorization: AuthrizationToken,
                        version: vers
                    },
                    data: req
                })
                    .then(Response => {
                        logger.debug(`${funcname} response`, Response.data, funcname)
                        resolve(Response.data)
                    })
            }
            catch (e) {
                var err = new errorHandler(e).getError()
                logger.error(`${funcname} response`, { "Error": err, "inputs": req }, funcname)
                reject(err)
            }
        })
    }

Can you please confirm from where did you get this sample code?

above code is already in nodejs sdk under node module > apiService > apiService.js > FyersApi > exit_position

as it describe in doc. https://myapi.fyers.in/docsv3#tag/Other-Transactions/paths/~1OtherTrans/options

as above example extended with following
“This is an added functionality for exit position API”
and https://myapi.fyers.in/docsv3#tag/Other-Transactions/paths/~1OtherTrans/get

Thank you, @114802498256746539803 for confirming. In the request parameter, please try passing pending_orders_cancel: 1

I had try with following code, but it’s only exit current position but it’s not cancelling pending order of it. for example NSE:SBIN-EQ order triggered and position is running, i had also placed 1 order for target and 1 order for stoploss these target and stoploss order is pending (working), but when i want to exit NSE:SBIN-EQ-INTRADAY it should also cancel my pending order of target and stoploss. but it’s exit my running position but not cancelling both pending orders.. so my concern is why it’s not cancelling pending orders?

const exitReqBody = { id: “NSE:SBIN-EQ-INTRADAY”, pending_orders_cancel: 1, };
const response = await fyers.exit_position(exitReqBody);

so the question is how to exit my current position and cancel all it’s pending orders..

Sorry for the inconvenience. We will get this checked internally and get back to you, @114802498256746539803

Hi @114802498256746539803,

Upon checking internally, it’s working as expected. We need to connect with you to debug this issue further. Please DM your client ID

Client ID: FAF09197
Whenever we call exit api it’s create new multiple order and not being close current open positions, can you tell how it’s close positions when we click on exit button in fyers dashboard? i need same functionality for exit my current open positions.

right now we are using following api example:
const exitReqBody = { id: “NSE:SBIN-EQ-INTRADAY”, pending_orders_cancel: 1, };
const response = await fyers.exit_position(exitReqBody);

Thanks for sharing the client ID @114802498256746539803
We will connect with you and clarify this.