restbinancebinance-api-client

how to cancel stop loss and take profit order when position close on binance futures with rest api


I'm using Binance futures rest API for algorithmic trading. after creating a buy or sell order, I'm also creating "take profit" and "stop-loss" orders, when I look at the Binance app. it looks like regular SL/TP orders but when I close positions manually, or when any SL/TP orders executed SL/TP orders still waiting in my open orders.

But when I create SL/TP orders with the Binance app and close position (for any reason) open orders also close for the same symbol.

Here is the endpoint and parameters for creating SL/TP orders;

https://fapi.binance.com/fapi/v1/order?symbol=ETHUSDT&side=BUY&type=TAKE_PROFIT_MARKET&timestamp=12123123&closePosition=true&stopPrice=4100&workingType=MARK_PRICE&priceProtect=true

this one create a TP order for the ETHUSDT symbol but I don't know why that order doesn't cancel when the position closed.

is there any missing parameter for creating SL/TP orders?


Solution

  • I'm have a related issue. For your specific problem what I have noticed is that when you submit, for example, a market long position. You can follow up with a TP and SL order by setting them as TAKE_PROFIT_MARKET and STOP_MARKET respectively.

    For this to work you must by in 'one-way' mode (as opposed to 'hedge' mode).

    Then set the value of 'timeInForce' to 'GTE_GTC' - I couldnt see this value in the documentation but I did see that when you set an order via the UI with a TP/SL this is what is shown. Also set 'reduceOnly' to True.

    Then when you close the original market order both these 'pending' orders will be removed.

    Just tested that you can in fact submit all these orders in a batch (list of json) to:

    POST /fapi/v1/batchOrders
    
    batch_payload = [
            {
                'newClientOrderId': '467fba09-a286-43c3-a79a-32efec4be80e',
                'symbol': 'ETHUSDT',
                'type': 'MARKET',
                'quantity': '9.059',
                'side': 'SELL'
            },
            {
                'newClientOrderId': '6925e0cb-2d86-42af-875c-877da7b5fda5',
                'symbol': 'ETHUSDT',
                'type': 'STOP_MARKET',
                'quantity': '9.059',
                'side': 'BUY',
                'stopPrice': '3037.9',
                'timeInForce': 'GTE_GTC',
                'reduceOnly': 'True'
            },
            {
                'newClientOrderId': '121637a9-e15a-4f44-b62d-d424fb4870e0',
                'symbol': 'ETHUSDT',
                'type': 'TAKE_PROFIT_MARKET',
                'quantity': '9.059',
                'side': 'BUY',
                'stopPrice': '2748.58',
                'timeInForce': 'GTE_GTC',
                'reduceOnly': 'True'
            }
        ]