admin管理员组

文章数量:1122832

I am looking for possibility of closing position regardless of whether at a profit or loss via API.

I tried something like that:

stop_order = self.client.futures_create_order(
            symbol=symbol,
            side=stop_side, #SELL if I have long position or BUY if short
            type=FUTURE_ORDER_TYPE_STOP_MARKET,
            closePosition=True,
            stopPrice=stop_price, #it is required but I would prefer "Market" price in this case
            test=True
        )

but I'm receiving error like that: "Error canceling order: APIError(code=-2021): Order would immediately trigger."

or if I change to FUTURE_ORDER_TYPE_MARKET: "Error canceling order: APIError(code=-4136): Target strategy invalid for orderType MARKET,closePosition true"

it's seems like binance API doesn't allow what I want.. but via page we can simply click "Market" and position would be close...

I am looking for possibility of closing position regardless of whether at a profit or loss via API.

I tried something like that:

stop_order = self.client.futures_create_order(
            symbol=symbol,
            side=stop_side, #SELL if I have long position or BUY if short
            type=FUTURE_ORDER_TYPE_STOP_MARKET,
            closePosition=True,
            stopPrice=stop_price, #it is required but I would prefer "Market" price in this case
            test=True
        )

but I'm receiving error like that: "Error canceling order: APIError(code=-2021): Order would immediately trigger."

or if I change to FUTURE_ORDER_TYPE_MARKET: "Error canceling order: APIError(code=-4136): Target strategy invalid for orderType MARKET,closePosition true"

it's seems like binance API doesn't allow what I want.. but via page we can simply click "Market" and position would be close...

Share Improve this question asked Nov 21, 2024 at 18:47 Marek BiolikMarek Biolik 31 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

The error messages you’re encountering arise because Binance’s API handles closing positions differently than the web interface.

Order would immediately trigger (-2021) happens because FUTURE_ORDER_TYPE_STOP_MARKET is a stop order, which requires a stopPrice. However, the stop price cannot be immediately triggered by the current market price.

Target strategy invalid (-4136) occurs because FUTURE_ORDER_TYPE_MARKET does not support closePosition=True. That parameter is only valid for stop orders or take-profit orders.

You can use FUTURE_ORDER_TYPE_MARKET without closePosition. You can create a market order with the correct quantity and side to close the position. Here’s how to do it-

positions = self.client.futures_account()['positions']
position = next((p for p in positions if p['symbol'] == symbol), None)

if position and float(position['positionAmt']) != 0:
    side = 'SELL' if float(position['positionAmt']) > 0 else 'BUY'  
    quantity = abs(float(position['positionAmt']))  

    close_order = self.client.futures_create_order(
        symbol=symbol,
        side=side,
        type='MARKET',
        quantity=quantity
    )
    print(f"Position closed: {close_order}")
else:
    print(f"No position found for {symbol}")

You're seeing that issue because the API is different. To close a position on Binance via API you need to use a MARKET order without the closePosition=True parameter. Check the open positions for the symbol, determine the side (BUY or SELL based on whether your position is short or long), and place a market order to close it. Here is a super useful tutorial.

    if position and float(position['positionAmt']) != 0:
    side = 'SELL' if float(position['positionAmt']) > 0 else 'BUY'
    quantity = abs(float(position['positionAmt']))
    client.futures_create_order(
        symbol='BTCUSDT',
        side=side,
        type='MARKET',
        quantity=quantity
    )
    print("Position closed successfully!")
else:
    print("No open position to close.")

本文标签: pythonHow close position in Binance by APIStack Overflow