【问题标题】:How can I closing my position with using Market order via Bybit API?如何通过 Bybit API 使用市价单平仓?
【发布时间】:2022-06-27 19:20:28
【问题描述】:

我尝试使用带有 Bybit API 的 python 创建一个示例交易机器人。它在我创建头寸时工作,但是当我尝试关闭头寸时它向我发送错误,因为当我关闭头寸时,我无法使用“市场”订单。我正在搜索 Bybit 文档,但看不到这一点。

让我解释一下:

这是我的开仓代码

open_position = client.place_active_order(symbol="BTCUSDT",
                side="Buy",
                order_type="Market",
                qty= 0.001,
                time_in_force="GoodTillCancel",
                reduce_only=False,
                close_on_trigger=False,)

我尝试使用以下代码平仓:

last_price = client.latest_information_for_symbol(symbol="BTCUSDT")["result"][0]["last_price"]

# close position
close_position = client.set_trading_stop(
                 symbol="BTCUSDT",
                 side="Buy",
                 take_profit=last_price)

此代码在某些情况下有效,但通常我会收到此错误:

InvalidRequestError(
pybit.exceptions.InvalidRequestError: Takeprofit:445890000 set for buy position should be higher 
than base_price:445895000??lastprice (ErrCode: 130027)

我认为,当我向take_profit = last_price运行订单时,last_price 不等于 BTCUSDT 平价

例如,我的last_price 代码获得 BTCUSDT= 40000 但当我的代码下订单时,BTCUSDT 上升了 40100。

那么,如何使用“市价”订单平仓?

Bybit 文档在这里Bybit Api documantation

谢谢。

【问题讨论】:

    标签: python api bybit


    【解决方案1】:

    我一直在努力解决同样的问题,直到我意识到在 ByBit 上关闭交易的实际方法是使用 reduce_only 选项来打开交易的另一侧:

    client.place_active_order(symbol="BTCUSDT",
                    side="Sell",
                    order_type="Market",
                    qty= 0.001,
                    time_in_force="GoodTillCancel",
                    reduce_only=True
                    close_on_trigger=False,)
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2021-11-03
      • 2022-11-18
      • 2020-11-12
      相关资源
      最近更新 更多