Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Futures order with TAKE_PROFIT_MARKET same time #614

Open
ne0c0de opened this issue Mar 16, 2021 · 21 comments
Open

Futures order with TAKE_PROFIT_MARKET same time #614

ne0c0de opened this issue Mar 16, 2021 · 21 comments

Comments

@ne0c0de
Copy link

ne0c0de commented Mar 16, 2021

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
Copy link

zackexplosion commented Mar 30, 2021

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
Copy link
Author

ne0c0de commented Apr 1, 2021

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

@greypeg
Copy link

greypeg commented May 18, 2021

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
Copy link
Author

ne0c0de commented May 18, 2021

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
Copy link

laukikk commented May 24, 2021

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
Copy link

robyle commented May 25, 2021

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
Copy link

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
Copy link

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
Copy link
Contributor

jorisw commented Jul 3, 2021

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

@nikhilch86
Copy link

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

Yes, websockets is the way. Thank you.

@robyle
Copy link

robyle commented Jul 8, 2021

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
Copy link

fuvser commented Jul 10, 2021

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
Copy link
Contributor

jorisw commented Jul 10, 2021

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

@fuvser
Copy link

fuvser commented Jul 14, 2021

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
Copy link
Contributor

jorisw commented Jul 15, 2021

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
Copy link

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
Copy link
Author

ne0c0de commented Dec 16, 2021

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
Copy link

alessandromotaa commented Dec 28, 2021

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
Copy link

KingIthra commented Dec 29, 2021 via email

@laukikk
Copy link

laukikk commented Feb 20, 2022

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
Copy link

mhgutier commented Jun 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

13 participants