Python 币安检测对冲模式是否激活

Python Binance detect hedge mode active or not

我使用图书馆python-binance

当我连接我的 binance 客户端时,如果我尝试做空订单,如果对冲模式未激活 (more about Hedge mode activation),我会收到以下错误:

binance.exceptions.BinanceAPIException: APIError(code=-4061): Order's position side does not match user's setting.

代码:

from binance.client import Client

client = Client(
    api_key,
    api_secret,
    )

# Here : I want to get if client accoutn has hedge mode enable or not like : 

# hedge_mode = client.get_hedge_mode_active() <--- This method does not exists on library



order = client.futures_create_order(
    symbol = "XRPUSDT",
    side = SIDE_BUY,
    positionSide = "SHORT",
    type = ORDER_TYPE_MARKET,
    quantity = 500,
    )

positionSide 特别适用于对冲模式。激活它可以让您同时持有同一交易品种的多头和空头头寸。

所以,如果你想要单向模式(而不是对冲模式)那么你只需要:

response = client.futures_create_order(
    timeInForce="GTC",
    symbol="BTCUSDT",
    side="BUY",
    type="LIMIT",
    quantity=1,
    price=1.00
)

要检查对冲模式是否激活,只需使用:

client.futures_get_position_mode() #This in python-binance

回复:

{
    "dualSidePosition": true // "true": Hedge Mode mode; "false": One-way Mode
}

来自 binance 的文档 API:

https://binance-docs.github.io/apidocs/futures/en/#get-current-position-mode-user_data