You didn;t continue your cdoe, Code: Here’s an example updated binance_futures.fapiPrivatePostOrder() method call:

response = binance_futures.fapiPrivatePostOrder(
data={
‘symbol’: ‘BTCUSDT’,
‘side’: ‘SELL’ if current_position[‘positionSide’] == ‘LONG’ else ‘BUY’,
‘type’: ‘MARKET’,
‘quantity’: abs(float(current_position[‘positionAmt’])),
‘positionSide’: current_position[‘positionSide’],
‘reduceOnly’: True
}
)

Replace ‘BTCUSDT’ with your desired symbol.

Regarding the long position not executing when you receive a buy signal, it’s possible that there is an issue with your signal generation logic or order execution logic. You can try adding print statements or logs to see what steps are being executed and whether there are any errors that are occurring.
Give me just right code of order_execution
Here’s an updated order_execution function with the changes:

def order_execution(symbol, signal, step_size, leverage, order_type):
# Close any existing positions
current_position = None
positions = binance_futures.fapiPrivateGetPositionRisk()
for position in positions:
if position[“symbol”] == symbol:
current_position = position
if current_position is not None and current_position[“positionAmt”] != 0:
response = binance_futures.fapiPrivatePostOrder(
data={
‘symbol’: symbol,
‘side’: ‘SELL’ if current_position[‘positionSide’] == ‘LONG’ else ‘BUY’,
‘type’: ‘MARKET’,
‘quantity’: abs(float(current_position[‘positionAmt’])),
‘positionSide’: current_position[‘positionSide’],
‘reduceOnly’: True
}
)

if ‘orderId’ in response:
print(f’Closed position: {response}‘)
else:
print(f’Error closing position: {response}’)
time.sleep(1)

# Calculate appropriate order quantity and price based on signal
opposite_position = None
quantity = step_size
position_side = None #initialise to None
price = None

# Set default take profit price
take_profit_price = None
stop_loss_price = None

if signal == ‘buy’:
position_side = ‘BOTH’
opposite_position = current_position if current_position and current_position[‘positionSide’] == ‘SHORT’ else None
order_type = ‘TAKE_PROFIT_MARKET’
ticker = binance_futures.fetch_ticker(symbol)
price = 0 # default price
if ‘ask’ in ticker:
price = ticker[‘ask’]
# perform rounding and other operations on price
else:
# handle the case where the key is missing (e.g. raise an exception, skip this signal, etc.)
take_profit_percentage = TAKE_PROFIT_PERCENTAGE
stop_loss_percentage = STOP_LOSS_PERCENTAGE

elif signal == ‘sell’:
position_side = ‘BOTH’
opposite_position = current_position if current_position and current_position[‘positionSide’] == ‘LONG’ else None
order_type = ‘STOP_MARKET’
ticker = binance_futures.fetch_ticker(symbol)
price = 0 # default price
if ‘bid’ in ticker:
price = ticker[‘bid’]
# perform rounding and other operations on price
else:
# handle the case where the key is missing (e.g. raise an exception, skip this signal, etc.)
take_profit_percentage = TAKE_PROFIT_PERCENTAGE
stop_loss_percentage = STOP_LOSS_PERCENTAGE

# Set stop loss price
stop_loss_price = None
if price is not None:
try:
price = round_step_size(price, step_size=step_size)
if signal == ‘buy’:
# Calculate take profit and stop loss prices for a buy signal
take_profit_price = round_step_size(price * (1 + TAKE_PROFIT_PERCENTAGE / 100), step_size=step_size)
stop_loss_price = round_step_size(price * (1 - STOP_LOSS_PERCENTAGE / 100), step_size=step_size)
elif signal == ‘sell’:
# Calculate take profit and stop loss prices for a sell signal