You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Bybit对冲模式平仓方法求助:reduceOnly参数报错问题

问题

在Bybit对冲模式下,同一账户同时持有多空头寸,尝试平仓其中一头寸时操作受阻,reduceOnly参数未生效。

原代码:

params = {
        # 'reduceOnly': True,   
        'positionIdx' : 2       # 0 : one-way mode position, 1 : Buy side of hedge-mode position, 2 : Sell side of hedge-mode position
    }

resp = bybit.create_order(
         symbol=coin2,
         type='limit',
         side='sell',
         price=price,
         amount=amount,  
         params=params   
)   

取消reduceOnly注释后触发错误:

"retCode":110017,"retMsg":"current position is zero, cannot fix reduce-only order qty"

需要明确对冲模式下正确的平仓操作方式。

解决方案
  • 匹配positionIdx与平仓方向
    平仓方向和仓位索引必须对应:平多头(持有多单)需要用side='sell'+positionIdx=1;平空头(持有空单)需要用side='buy'+positionIdx=2。你之前的代码里positionIdx=2对应空头仓位,但side='sell'是开空单而非平仓,方向完全错误,这是核心问题。

  • 正确配置参数的示例
    平多头仓位代码:

    params = {
        'reduceOnly': True,
        'positionIdx': 1  # 指定平多头仓位
    }
    
    resp = bybit.create_order(
        symbol=coin2,
        type='limit',
        side='sell',  # 平多头用卖单
        price=price,
        amount=amount,
        params=params
    )
    

    平空头仓位代码:

    params = {
        'reduceOnly': True,
        'positionIdx': 2  # 指定平空头仓位
    }
    
    resp = bybit.create_order(
        symbol=coin2,
        type='limit',
        side='buy',  # 平空头用买单
        price=price,
        amount=amount,
        params=params
    )
    
  • 错误原因解析
    你之前触发的错误,是因为positionIdx=2指向空头仓位,但side='sell'是开新空单的操作,加上reduceOnly=True后,系统检测到你指定的空头仓位数量为0(实际你想平的是多头,参数却设成了空头),所以抛出“current position is zero”的报错。

内容的提问来源于stack exchange,提问作者Shin Midas

火山引擎 最新活动