Order's position side does not match user's setting.

I am getting APIError(code=-4061): Order’s position side does not match user’s setting. Hedge mode is enabled, does anyone have an idea how to get rid of this problem? market order gets placed and stop loss too. Take profit doesnt work tho and ends up with the error…

import pandas as pd
import time
import math
from binance_f import RequestClient
from binance_f.model import OrderSide, OrderType, PositionSide, WorkingType, TimeInForce
from binance.client import Client
from decimal import Decimal
from binance.helpers import round_step_size

request_client = RequestClient(api_key=‘277940a7e7cb624e0bbb4a4cdcdf1a552b473ae3c2d0350291f11ae1db73bdcc’, secret_key=‘19796332330a09e7074067a57f0c17f10e0a2425dbc9504adcd49a2422184a6c’)

Initialize the Binance client

client = Client(‘277940a7e7cb624e0bbb4a4cdcdf1a552b473ae3c2d0350291f11ae1db73bdcc’, ‘19796332330a09e7074067a57f0c17f10e0a2425dbc9504adcd49a2422184a6c’, testnet=True)

symbol=‘BTCUSDT’

Get the current market price for the ‘BTCUSDT’ trading pair

ticker = client.get_ticker(symbol=‘BTCUSDT’)
price = float(ticker[‘lastPrice’])

def get_tick_size(symbol: str) → float:
info = client.futures_exchange_info()

for symbol_info in info['symbols']:
    if symbol_info['symbol'] == symbol:
        for symbol_filter in symbol_info['filters']:
            if symbol_filter['filterType'] == 'PRICE_FILTER':
                return float(symbol_filter['tickSize'])

def get_rounded_price(symbol: str, price: float) → float:
return round_step_size(price, get_tick_size(symbol))

price = get_rounded_price(symbol, price)

print(price)

Define the percentage for take profit and stop loss

tp_percentage = 0.006 # 0.6%
sl_percentage = 0.003 # 0.3%

tp=price * (1 + tp_percentage)
sl=price * (1 - sl_percentage)

tickSize = float(client.get_symbol_info(symbol=symbol)[‘filters’][0][‘tickSize’])
rounded_tp = round_step_size(float(tp), tickSize)
rounded_sl = round_step_size(float(sl), tickSize)

print(rounded_tp)
print(rounded_sl)

Check if sl and tp prices are valid

if rounded_sl >= price:
print(“Error: Stop loss price is higher than or equal to the current market price.”)
exit()

if rounded_tp <= price:
print(“Error: Take profit price is lower than or equal to the current market price.”)

Specify the quantity for the limit order

quantity = 0.05

Create a limit order

side = ‘BUY’
positionSide = ‘LONG’
tp_position_side = ‘LONG’
sl_position_side = ‘LONG’

Create a market order

client.futures_create_order(
symbol=‘BTCUSDT’,
side=side,
positionSide=positionSide,
type=‘MARKET’,
quantity=quantity
)

Stop loss order

client.futures_create_order(
symbol=symbol,
side=side, #‘SELL’ or ‘BUY’
type =‘LIMIT’,
timeInForce=‘GTC’,
price = price,
quantity = quantity,
#isolated=True,
#stopPrice=stop_price,
workingType=‘CONTRACT_PRICE’, #or MARK PRICE
positionSide=‘LONG’
)
#TAKE PROFIT ORDER
client.futures_create_order(
symbol=symbol,
side=‘SELL’, #‘SELL’ or ‘BUY’
type =‘TAKE_PROFIT’,
timeInForce=‘GTC’,
price = price,
reduceOnly= True,
quantity = quantity,
#isolated=True,
stopPrice=rounded_tp,
workingType=‘CONTRACT_PRICE’ #or MARK PRICE
)

Can you double check that you’re in hedge mode?

If hedge mode is set and you are trying to send a One-way order you will get this error. Or vice-versa.