You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

币安期货交易机器人API错误-2021:订单将立即触发问题

期货交易机器人APIError -2021问题排查

错误信息

APIError(code=-2021): Order would immediately trigger

问题背景

我开发了一个用于保证金交易的期货账户交易机器人,其中开仓并设置止盈止损的代码片段仅正常运行过一次,之后持续抛出上述错误。但把这段代码单独复制到新文件运行时完全正常,放到机器人环境里就报错,找不到问题根源。

原代码片段

if currentdiffer > 0:
                
    buy_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='BUY',
                                            type ='MARKET',
                                            quantity = qty, 
                                            positionSide='LONG')
   
    poisiton = "LONG"
    
    stopprice = currentprice - (value*1.2) 
    takeprofit = currentprice + (value*1.2) 
    
    stopprice = round(stopprice,2)
    takeprofit = round(takeprofit,2)
    
    print("Take profit : ", takeprofit)
    print("Stop price : ", stopprice)
    
    tp_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='SELL',
                                            positionSide='LONG',
                                            type ='TAKE_PROFIT_MARKET',
                                            timeInForce='GTE_GTC',
                                            quantity = qty,
                                            stopPrice=stopprice,
                                            workingType='MARK_PRICE'
                                            )

    sl_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='SELL',
                                            positionSide='LONG',
                                            type ='STOP_MARKET',
                                            timeInForce='GTE_GTC',
                                            quantity = qty,
                                            stopPrice=takeprofit,
                                            workingType='MARK_PRICE'
                                            )

问题根源

代码里的止盈、止损订单参数完全写反了:

  • 创建TAKE_PROFIT_MARKET(止盈市价单)时,stopPrice应该设置为盈利目标价takeprofit,但你填成了止损价stopprice
  • 创建STOP_MARKET(止损市价单)时,stopPrice应该设置为止损价stopprice,但你填成了盈利目标价takeprofit

这种参数错误会导致:

  • 止盈单的触发价被设成了低于当前市价的止损价,提交后立即触发,触发API的-2021错误(订单会立即执行)
  • 单独运行时可能刚好遇到价格波动极小的情况,偶然没触发,但机器人运行时价格变化会直接触发错误

修正后的代码

if currentdiffer > 0:
                
    buy_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='BUY',
                                            type ='MARKET',
                                            quantity = qty, 
                                            positionSide='LONG')
   
    poisiton = "LONG"
    
    stopprice = currentprice - (value*1.2) 
    takeprofit = currentprice + (value*1.2) 
    
    stopprice = round(stopprice,2)
    takeprofit = round(takeprofit,2)
    
    print("Take profit : ", takeprofit)
    print("Stop price : ", stopprice)
    
    # 修正止盈订单参数:stopPrice设为takeprofit
    tp_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='SELL',
                                            positionSide='LONG',
                                            type ='TAKE_PROFIT_MARKET',
                                            timeInForce='GTE_GTC',
                                            quantity = qty,
                                            stopPrice=takeprofit,
                                            workingType='MARK_PRICE'
                                            )

    # 修正止损订单参数:stopPrice设为stopprice
    sl_order = client.futures_create_order(symbol='SOLBUSD',
                                            side='SELL',
                                            positionSide='LONG',
                                            type ='STOP_MARKET',
                                            timeInForce='GTE_GTC',
                                            quantity = qty,
                                            stopPrice=stopprice,
                                            workingType='MARK_PRICE'
                                            )

内容的提问来源于stack exchange,提问作者Hamit Kaan Serifoglu Student

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 13:48:22