-5

I am coding with python and ccxt library. I am trying to make a bot for binance futures. My bot is supposed to run on Hedge mode, everything seemed to be working great until i changed my preference to Hedge mode from one-way mode. I searched and tried everything i could, to resolve the problem. This is the error I am having -:

    binance {"code":-4061,"msg":"Order's position side does not match user's setting."}

Bellow is all the code.

    import ccxt
    exchange = ccxt.binance({
        'base_url': 'https://testnet.binancefuture.com',
        'account_info': '/api/v3/account',
        'enableRateLimit': True,
        'apiKey': '...',
        'secret': '...',
        'hedgeMode': True,
        'options': {
            'defaultType': 'future',
       },
    })
    exchange.set_sandbox_mode(True)
    balance = exchange.fetch_balance()
    balance

The above code works just fine I have also changed my API credentials a bit for safety reasons although i am using a test net, apart from that everything is as it is.

My problem is with the code bellow

    symbol = 'ETH/USDT'

    typee = 'market'  # or 'market', other types aren't unified yet
    side1 = 'buy'
    side2 = 'sell'
    amount = 0.1  # your amount
    #price = 0.21  # your price
    # overrides
    params = {
    'position_side': 'LONG' and 'SHORT'
   #'stopPrice': ,# your stop price
    }  

    buy_market_order = exchange.create_order(symbol, typee, side1, amount, params) 
    #INCLUDE 'price' IF NEEDED
    sell_market_order = exchange.create_order(symbol, typee, side2, amount, params)
P i
  • 29,020
  • 36
  • 159
  • 267
  • 1
    Have you thought of checking your code thoroughly? What is this logic - `'position_side': 'LONG' and 'SHORT'` ? – Neeraj Jun 23 '22 at 00:28
  • You do realize you've posted your API key. I've edited it out, but you should change it. It's loose on the internet now. – P i Sep 21 '22 at 12:37

1 Answers1

3

I think you have to change your position mode to hedged before placing the order to open a hedged position:

    symbol = 'ETH/USDT'

    try:
        exchange.set_position_mode(hedged=True, symbol=symbol)  # ADD THIS
    except Exception as e:
        pass  # swallow the error if you're already in hedge mode
    
    typee = 'market'  # or 'market', other types aren't unified yet
    side1 = 'buy'
    side2 = 'sell'
    amount = 0.1  # your amount
    #price = 0.21  # your price
    # overrides
    params = {
        'positionSide': 'LONG'  # and 'SHORT'
        #'stopPrice': ,# your stop price
    }  

    buy_market_order = exchange.create_order(symbol, typee, side1, amount, params) 
    #INCLUDE 'price' IF NEEDED
    sell_market_order = exchange.create_order(symbol, typee, side2, amount, params)
Igor Kroitor
  • 1,548
  • 13
  • 16
  • I did as you said and I get a new error binance {"code":-4059,"msg":"No need to change position side."} – Daanish PM Jun 21 '22 at 18:31
  • and i have kept my "params" empty – Daanish PM Jun 21 '22 at 18:36
  • @kroitor please help me with this, i am not able to move ahead without solving this, my hedge order would just never execute, i am not able to get help from anywhere..... – Daanish PM Jun 22 '22 at 01:48
  • @DaanishPM if you are already in hedge mode, then you don't need to call `set_position_mode`, or you can wrap it in a try-catch as i've shown above (edited the code slightly). Also, I've noticed a typo, it should be `positionSide`, not `position_side` in your `params`. Let me know if that does not help. – Igor Kroitor Jun 22 '22 at 07:43