Search code examples
pythonbinance

Python Binance detect hedge mode active or not


I use the library python-binance.

When I connect my binance client, if I try to make a SHORT order, If Hedge mode is not active (more about Hedge mode activation) I got the following error :

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

The code :

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,
    )

Solution

  • positionSide is for Hedge mode, specifically. Activating it allows you to hold both LONG and SHORT positions for the same symbol.

    So, if you want the one-way mode (not the hedge mode) then you just need:

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

    To check if hedge mode is activated or not, just use:

    client.futures_get_position_mode() #This in python-binance
    

    Response:

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

    Docs from binance API:

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