The error message suggests that the variable 'executed_orders_sell' is not defined. It also suggests, "Did you mean: 'executed_orders_buy'?" This means that you may have mistyped the variable name or used it before defining it.
In your code, the variable 'executed_orders_sell' is defined inside the for loop but is used outside the loop. Therefore, if the loop does not execute or the if condition inside the loop is not satisfied, the variable will not be defined before usage.
To fix this issue, you can initialize the variable 'executed_orders_sell' before the loop to ensure it is defined even if the loop does not execute. Here is an updated version of the code with the variable properly defined:
elif 'sell' in signals and active_signal != 'sell':
try:
active_signal = 'sell'
sell_entry_price = mark_price # Record the sell entry price
print(f"Sell Entry Price: {sell_entry_price}")
client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
print("Short order executed!")
except binance.error.ClientError as e:
print(f"Error executing short order: ")
if buy_entry_price is not None and sell_entry_price is not None:
buy_exit_price = mark_price
difference_buy = calculate_percentage_difference_buy(buy_entry_price, buy_exit_price)
profit_buy = difference_buy
total_profit_buy = profit_buy
profit_buy_percent = total_profit_buy - 4
print(f"buy entry price {buy_entry_price}, buy exit price: {sell_entry_price}, Buy P&L: {round(total_profit_buy, 2)}% with fee {round(profit_buy_percent, 2)}%")
else:
print("Buy Entry price or Sell Entry price is not defined.")
jerusalem = []
executed_orders_sell = 0.0 # Initialize the variable
depth_data = client.depth(symbol=symbol)
bid_depth = depth_data['bids']
ask_depth = depth_data['asks']
buy_price = float(bid_depth[0][0]) if bid_depth else 0.0
sell_price = float(ask_depth[0][0]) if ask_depth else 0.0
buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0
sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
executed_orders_qty = 0.0
order_price = sell_price
executed_orders = order_price == mark_price
for sell_price, sell_qty in ask_depth:
if sell_price == mark_price:
order = sell_qty
executed_orders_sell = order
sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
executed_orders_sell = float(executed_orders_sell) if executed_orders_sell else 0.0
sell_result = sell_qty - executed_orders_sell
# Subtract executed order quantity from buy_qty
sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
buy_price = float(bid_depth[0][0]) if bid_depth else 0.0
sell_price = float(ask_depth[0][0]) if ask_depth else 0.0
if (sell_result < buy_qty - 0.01) and (buy_price > mark_price + 0.02):
active_signal = None
jerusalem.append("Short !!! EXIT !!!")
if sell_entry_price is not None and jerusalem is not None:
sell_exit_price = mark_price
difference_sell = calculate_percentage_difference_buy(sell_entry_price, sell_exit_price)
profit_sell = difference_sell
total_profit_sell = profit_sell
profit_sell_percent = total_profit_sell - 4
try:
client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
except binance.error.ClientError as e:
print(f"Short position successfully exited!")
print(" !!! EXIT !!! ")
print(f"Time was : {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} sell entry price {sell_entry_price}, sell exit price: {mark_price}, Sell P&L: {round(total_profit_sell, 2)}% with fee {round(profit_sell_percent, 2)}%")
sell_entry_price = None
else:
print("Buy Entry price or Sell Entry price is not defined.") # Print israel when condition is met
else:
jerusalem.append('')
With the above code, the variable 'executed_orders_sell' will be initialized before the for loop, ensuring it is defined even if the loop does not execute or if the condition inside the loop is not satisfied.