jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 768 forks source link

Futures position with stop loss and take profit at the same time not cancelling the other order #769

Open NivEz opened 2 years ago

NivEz commented 2 years ago

Hey, I am using the futures api. Is it possible to open a long / short position with take profit and stop loss orders in a way that if it reaches the stopPrice the other order will be cancelled? For example: Open long position on ADAUSDT while it's price is 1.4. binance.futuresMarketBuy('ADAUSDT', 5).then(res => {console.log(res)})

Open stop loss order on 1.3 price. binance.futuresSell('ADAUSDT', 5, false, {type: 'STOP_MARKET', stopPrice: 1.3}).then(res => {console.log(res)})

Open take profit order on 1.5 price. binance.futuresSell('ADAUSDT', 5, false, {type: 'TAKE_PROFIT', stopPrice: 1.5}).then(res => {console.log(res)})

If it reaches 1.3 it will sell my position, but it won't cancel the take profit order which will ruine my next bot executions. How can I make it to cancel the take profit order? Thanks

ordimans commented 2 years ago

Hum it's good question. I have the same problem with open order (LIMIT). And i found two solutions here :

futuresCountdownCancelAll or cancelOrder manually with a cron (or anything else), to cancel old order. In my case it's open order so i don't think i can set a expiration time. But about you, i don't know. I know SL is cancelled if TP is reache. But why reverse doesn't work. IT seems same by desktop app, you can try it.

So it's probably a OCO order you need but it's not possible in futures.

NivEz commented 2 years ago

Hum it's good question. I have the same problem with open order (LIMIT). And i found two solutions here :

futuresCountdownCancelAll or cancelOrder manually with a cron (or anything else), to cancel old order. In my case it's open order so i don't think i can set a expiration time. But about you, i don't know. I know SL is cancelled if TP is reache. But why reverse doesn't work. IT seems same by desktop app, you can try it.

So it's probably a OCO order you need but it's not possible in futures.

is futuresCountdownCancelAll an option in the futures API or I need to implement it on my own? And yes probably the way to achieve similar behavior of OCO is cron job or a "watcher" to listen to changes via socket.

ordimans commented 2 years ago

YEs it's in the futures API (i mean this API), just call binance. let res = await binance.futuresCountdownCancelAll(pair,delayMs)

It work for me , i did a test.

And so after i place this on some order to have a expiration date. And i have another method to cancel all order.

I called :


let orderToCancel = []
 let order = await binance.futuresOpenOrders()
  for (const element of order) {
   if(orderExpiredMin(element.time,60*4)) orderToCancel.push(element)
}

for (const order of orderToCancel) {
                let tmp = String(order.orderId)
                let res = await binance.futuresCancel(order.symbol, {orderId:order.orderId})
                pino.info(`Order cancelled ${order.orderId} / ${order.clientOrderId}`)
}

You can add a call to get Positions and check if you have still position, to don't cancel TP or SL. And use SIDE and POSITION_SIDE on order to distinct OPEN and TP.

Voila !