I used this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I used this code:
def signal_generator(df): if df is None or len(df) < 2: return ''
signal = [] final_signal = [] exit = [] # Retrieve depth data depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] buy_qty = round(sum(float(bid[1]) for bid in bids), 0) highest_bid_price = float(bids[0][0]) lowest_bid_price = float(bids[1][0]) buy_price = highest_bid_price asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(asks[0][0]) lowest_ask_price = float(asks[1][0]) sell_price = lowest_ask_price mark_price_data = client.ticker_price(symbol=symbol) mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0 mark_price_data_change = client.ticker_24hr_price_change(symbol=symbol) mark_price_percent = float(mark_price_data_change['priceChangePercent']) if 'priceChangePercent' in mark_price_data_change else 0.0 # Calculate the threshold for order price deviation if mark_price_percent > 2: pth = 7 / 100 elif mark_price_percent < -2: pth = 7 / 100 else: pth = 7 / 100 if mark_price_percent > 2: th = 25000 elif mark_price_percent < -2: th = 25000 else: th = 15000 # Subtract quantity if bid price is the same as mark price for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy == mark_price: buy_result = buy_qty - qty_buy buy_result_1 = buy_result # Subtract quantity if bid price is the same as mark price for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell == mark_price: sell_result = sell_qty - qty_sell sell_result_1 = sell_result if ((buy_price > mark_price + pth) and ((buy_qty > th) > (sell_qty < th))): signal.append('buy') elif ((sell_price < mark_price - pth) and ((sell_qty > th) > (buy_qty < th))): signal.append('sell') else: signal.append('') if signal == ['buy'] and not (((buy_qty - buy_result_1) < sell_qty) and (buy_price < mark_price - pth)): final_signal.append('buy') elif signal == ['sell'] and not (((sell_qty - sell_result_1) < buy_qty)and (sell_price > mark_price + pth)): final_signal.append('sell') else: final_signal.append('') if (((buy_qty - buy_result_1) < sell_qty) and (buy_price < mark_price)): exit.append('buy_exit') elif (((sell_qty - sell_result_1) < buy_qty) and (sell_price > mark_price)): exit.append('sell_exit') else: exit.append('') samekh = exit and final_signal return samekhBut I getting ERROR: Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 208, in
signals = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 135, in signal_generator sell_result_1 = sell_result ^^^^^^^^^^^ UnboundLocalError: cannot access local variable 'sell_result' where it is not associated with a value - Assistant
The error message indicates that the variable
sell_resultis being accessed before it is assigned a value. This error occurs because the assignment ofsell_resultwithin theforloop is conditional based on the value ofprice_sell, and if the condition is not met,sell_resultis not assigned a value.To fix this error, you can initialize
sell_resultbefore theforloop with a default value, such as0.0. This way, if the condition is not met within the loop,sell_resultwill still have a value assigned to it before it is accessed later in the code. Here's an example of how you can modify the code:# Initialize sell_result with a default value sell_result = 0.0 # Subtract quantity if bid price is the same as mark price for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell == mark_price: sell_result = sell_qty - qty_sell # Rest of the code...By assigning a default value to
sell_resultbefore the loop, you ensure that it will have a value even if the condition is not met, avoiding theUnboundLocalError.