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 badge2 Answers
Reset to default 0The 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
版权声明:本文标题:python - How close position in Binance by API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307961a1933493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论