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.57k stars 767 forks source link

Futures order with TAKE_PROFIT_MARKET same time #614

Open ne0c0de opened 3 years ago

ne0c0de commented 3 years ago

Here's my achievement that's what I'm trying to make on API:

Create an order for BTCUSDT from 53000 price with 0.005 quantity and set TP value as markprice=54000 when order executed. This is what I can do from app as like this:

image

I tried with this code block:

await binance.futuresOrder( 'BUY', 'BTCUSDT', 0.005, 53000, { type: 'TAKE_PROFIT', stopPrice: 54000 } )

Which will send the request with these parameters:

{
  type: 'TAKE_PROFIT',
  stopPrice: 54000,
  symbol: 'BTCUSDT',
  side: 'BUY',
  quantity: 0.005,
  price: 53000,
  timeInForce: 'GTX'
}

But order opening as wrong way like this:

image

However when I place the order from app it shows like this:

image

When I click on View under TP/SL it shows a condition like this:

image

How can I achieve this via API?

zackexplosion commented 3 years ago

I am working on the same thing, Check this.

https://dev.binance.vision/t/how-to-implement-otoco-tp-sl-orders-using-api/1622

It seems we must do it by ourself.

ne0c0de commented 3 years ago

I applied a workaroung with creating a Trailing Stop Loss 2 second after creating market order.

greypeg commented 3 years ago

Hello guys, I have backtest my project and now I want to make sure everything will work fine. It seems that I have the same problem with you. So, there is not a direct way to pass TAKE_PROFIT or STOP_LOSS as parameters in futures , right? So for instance, lets say I buy and then I want to take profit at 2%, I have to keep track in my program the price change and place a sell order when 2% change is reached correct???

ne0c0de commented 3 years ago

exactly, if you're placing market buy order then you can directly create a limit sell order with reduce only option as true right after market buy order.

This is how i'm currently doing my auto trades

laukikk commented 3 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

EDIT: Now after using it I've come across problems that if for example even if the Stop Loss is triggered then the Take Profit order is still there and it interferes with my later orders. For this then I had to write down another logic to get the order Id of my stop losses and take profits and cancel them every time either one is triggered. The need for a single command now is axiomatic.

robyle commented 3 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

I meet the same problems.How do I post in one order,just like APP submit.

amiroveisi commented 3 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

I meet the same problems. How do I post in one order, just like APP submit.

The app does not do this with a single order. if you trace the network requests of the app, it creates 3 orders just like @robyle's code sample. So I guess we have to do the same and create 3 different orders to achieve what we need.

nikhilch86 commented 3 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

EDIT: Now after using it I've come across problems that if for example even if the Stop Loss is triggered then the Take Profit order is still there and it interferes with my later orders. For this then I had to write down another logic to get the order Id of my stop losses and take profits and cancel them every time either one is triggered. The need for a single command now is axiomatic.

Hi,

How do you get to know that either of the stop loss or take profits order has been triggered? Is there any callback or listener?

jorisw commented 3 years ago

You poll your orders, and/or you subscribe to Websocket updates.

nikhilch86 commented 3 years ago

You poll your orders, and/or you subscribe to Websocket updates.

Yes, websockets is the way. Thank you.

robyle commented 2 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

I meet the same problems. How do I post in one order, just like APP submit.

The app does not do this with a single order. if you trace the network requests of the app, it creates 3 orders just like @robyle's code sample. So I guess we have to do the same and create 3 different orders to achieve what we need.

actually, I trance the binance desktop make order, it's indecated make a OTC order,but api was not surpported.

fuvser commented 2 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired.

EDIT: Now after using it I've come across problems that if for example even if the Stop Loss is triggered then the Take Profit order is still there and it interferes with my later orders. For this then I had to write down another logic to get the order Id of my stop losses and take profits and cancel them every time either one is triggered. The need for a single command now is axiomatic.

Hello sir can you please explain how did you manage to cancel the sl and tp orders if one is triggered?

jorisw commented 2 years ago

Simply poll your orders or subscribe to websockets to see if one was filled, then cancel the other.

fuvser commented 2 years ago

Simply poll your orders or subscribe to websockets to see if one was filled, then cancel the other.

I'm sorry sir, but I'm very new to programming, can you please explain to me how can I do that?

jorisw commented 2 years ago

Polling orders means, asking the Binance API every so-many seconds or minutes, for your list of orders and their status.

Websockets means, subscribing to Binance's live feed of changes to orders relevant to you.

To explain how to code this, would mean to write an entire blog post. The documentation for this particular API client, node-binance-api, has examples on how to fetch order status info from Binance.

I would encourage you to learn a little bit more about programming before starting to work with automated trading.

KingIthra commented 2 years ago

I applied a workaroung with creating a Trailing Stop Loss 2 second after creating market order.

How did you implaement it? please explain how you did it in code

ne0c0de commented 2 years ago

I applied a workaroung with creating a Trailing Stop Loss 2 second after creating market order.

How did you implaement it? please explain how you did it in code

connected to websocket which will push all the account actions whenever I recevied an order that is filled, i created an TSL order on same symbol, reverse direction (if main order BUY then create SELL order or vice versa)

alessandromotaa commented 2 years ago

I was able to solve this problem by adding these arguments timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True. Remember that I'm using mode position = HEDGE, for this reason I have to inform the positionSide argument.

`coin = 'BTCUSDT' qtd = 0.001

SELL order

buy_limit = client.futures_create_order( symbol=coin, side='BUY', positionSide='LONG', type='MARKET', quantity=0.001 )

tp = round(45000,2) # TakeProfit = 45000

sl = round(50000,2) # StopLoss = 50000

TAKE_PROFIT_MARKET order

sell_gain_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='TAKE_PROFIT_MARKET', stopPrice=tp, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True )

STOP_MARKET order

sell_stop_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='STOP_MARKET', stopPrice=sl, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True ) `

KingIthra commented 2 years ago

😎 😎 Thank you very much sir. This is highly appreciated

On Tue, 28 Dec 2021, 16:14 Alessandro Mota, @.***> wrote:

I was able to solve this problem by adding these arguments timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True. Remember that I'm using mode position = HEDGE, for this reason I have to inform the positionSide argument.

`coin = 'BTCUSDT' qtd = 0.001 SELL order

buy_limit = client.futures_create_order( symbol=coin, side='BUY', positionSide='LONG', type='MARKET', quantity=0.001 )

tp = round(45000,2) # TakeProfit = 45000

sl = round(50000,2) # StopLoss = 50000 TAKE_PROFIT_MARKET order

sell_gain_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='TAKE_PROFIT_MARKET', stopPrice=tp, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True ) STOP_MARKET order

sell_stop_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='STOP_MARKET', stopPrice=sl, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True ) `

— Reply to this email directly, view it on GitHub https://github.com/jaggedsoft/node-binance-api/issues/614#issuecomment-1002125825, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL6XRBPL3HYLYD72NGDP3F3UTHA4VANCNFSM4ZIP3RNQ . You are receiving this because you commented.Message ID: @.***>

laukikk commented 2 years ago

Using the python-binance package I was able to achieve in putting the Stop Loss and Take Profit in 3 different actions:

order = client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_BUY, type=ORDER_TYPE_MARKET, quantity=quantity, isolated=True, positionSide=positionSide)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_STOP_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=stopLoss, timeInForce=TIME_IN_FORCE_GTC)
        client.futures_create_order(
            symbol='ETHUSDT', side=SIDE_SELL, type=FUTURE_ORDER_TYPE_TAKE_PROFIT_MARKET, quantity=quantity, positionSide=positionSide, stopPrice=takeProfit, timeInForce=TIME_IN_FORCE_GTC)

But still not able to execute the same order in the same line. Though for the time being it works as desired. EDIT: Now after using it I've come across problems that if for example even if the Stop Loss is triggered then the Take Profit order is still there and it interferes with my later orders. For this then I had to write down another logic to get the order Id of my stop losses and take profits and cancel them every time either one is triggered. The need for a single command now is axiomatic.

Hi,

How do you get to know that either of the stop loss or take profits order has been triggered? Is there any callback or listener?

Sorry for the late reply, I guess it might have been already solved but I'd tell you my solution. It is quite dumb and basic actually, but it does the job.😅 The WebSocket stream tells you if you still have a position in the market or not. If you don't then the code automatically starts executing the strategy. So the next time a new trade has to be made I just remove all the existing pending orders for my current token

mhgutier commented 2 years ago

I was able to solve this problem by adding these arguments timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True. Remember that I'm using mode position = HEDGE, for this reason I have to inform the positionSide argument.

`coin = 'BTCUSDT' qtd = 0.001

SELL order

buy_limit = client.futures_create_order( symbol=coin, side='BUY', positionSide='LONG', type='MARKET', quantity=0.001 )

tp = round(45000,2) # TakeProfit = 45000

sl = round(50000,2) # StopLoss = 50000

TAKE_PROFIT_MARKET order

sell_gain_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='TAKE_PROFIT_MARKET', stopPrice=tp, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True )

STOP_MARKET order

sell_stop_market = client.futures_create_order( symbol=coin, side='SELL', positionSide='LONG', type='STOP_MARKET', stopPrice=sl, closePosition=True, timeInForce='GTE_GTC', workingType='MARK_PRICE', priceProtect=True ) `

hi, is this applicable in one way mode? i dont see gte_gtc in the ui