How to send OCO order to binance

Viewed 13750

I would like to ask you for help. I´m trying to change python code from sending limit/market order to OCO order through api to binance. I can make limit order, market order, stop loss limit order. I can´t figure out how to place OCO order...

When I used limit order I was sending order_type=ORDER_TYPE_LIMIT then I used order = client.create_order() and it was working. When I wanted to send market order I used order_type=ORDER_TYPE_MARKET but when I want to make OCO order the only option that I found that should work is: order = client.create_oco_order() without order_type but here I´m getting error 1013 Stop loss is not supported for this symbol...

I checked https://api.binance.com/api/v1/exchangeInfo

and there is this "orderTypes":["LIMIT","LIMIT_MAKER","MARKET","STOP_LOSS_LIMIT","TAKE_PROFIT_LIMIT"],"icebergAllowed":true,"ocoAllowed":true,

So I can´t use order_type. There is not ORDER_TYPE_OCO and ocoAllowed is true so I should be able to send oco order. But I´m getting "error 1013: Stop loss orders are not supported for this symbol. order failed".

What I want is to set "price" as limit sell order to take profit highier when price will get there and to set stop loss "stopPrice" lower if price will go down...at once. This is how OCO should works.

Can somebody please give me an advice how to do it? I´m not python guru I´m just changing one code that I found and what I understand is if oco is allowed stop loss should be allowed too. Thanks

1 Answers

so that all interested have a precise answer about the solution of this problem, I include the code with the comments.

I'll use an OCO sell order as an example in BTCUSDT.

Imagine I have 1 BTC. The current price is at 30157.85 and I want to sell 1 BTC much higher at 32000.07

But the price does not rise and begins to fall, so I place my stopPrice at 29283.03 where the limit sell order will be opened at the price 29000.00

This means that I will sell at 32000.07 or 29000.00 USDT. The order is written as follows:

order= client.order_oco_sell(
    symbol= 'BTCUSDT',                                            
    quantity= 1.00000,                                            
    price= '32000.07',                                            
    stopPrice= '29283.03',                                            
    stopLimitPrice= '29000.00',                                            
    stopLimitTimeInForce= 'FOK')

Note that the OCO order requires the stopLimitTimeInForce parameter. I have used the value 'FOK' but I leave you here the description of the different values that you can use: https://help.bybit.com/hc/en-us/articles/360039749233-What-are-time-in-force-TIF-GTC-IOC-FOK-

Note well that the price, stopPrice, stopLimitPrice and stopLimitTimeInForce parameters are strings and not float or decimal.

Related