I have this code: markpricedata = client.tickerprice(symbol=symbol) mp =…
- User
I have this code: mark_price_data = client.ticker_price(symbol=symbol) mp = 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
# Retrieve depth data depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] bid_prices = [float(bid[0]) for bid in bids] highest_bid_price = max(bid_prices) lowest_bid_price = min(bid_prices) buy_lowest_price = 0 for bid_price in bid_prices: if bid_price > mp + (mp / 100): buy_lowest_price = bid_price break if buy_lowest_price == 0: buy_lowest_price = lowest_bid_price > mp + (mp / 100) b_h = highest_bid_price b_l = buy_lowest_price asks = depth_data['asks'] ask_prices = [float(ask[0]) for ask in asks] highest_ask_price = max(ask_prices) lowest_ask_price = min(ask_prices) sell_highest_price = 0.0 for ask_price in ask_prices: if ask_price < mp - (mp / 100): sell_highest_price = ask_price break if sell_highest_price == 0: sell_highest_price = mp - (mp / 100) s_h = highest_ask_price s_l = lowest_ask_price #Quantity for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: buy_qty = (price_buy, qty_buy) print(buy_qty) for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell < mark_price: sell_qty = (price_sell, qty_sell) print(sell_qty) print(mark_price)But it returning me:Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 252, in signals = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 158, in signal_generator if price_buy == mark_price and buy_qty is not None: ^^^^^^^ UnboundLocalError: cannot access local variable 'buy_qty' where it is not associated with a value
- Assistant
The variable 'buy_qty' is only assigned a value inside the if statement
if price_buy > mark_price, but it seems like there is no fallback value assigned to it if the condition is not met. Therefore, when you try to printbuy_qtyoutside the loop, it throws an UnboundLocalError because the variable may not have been assigned a value.To fix this issue, you can initialize
buy_qtywith a default value before the loop, likebuy_qty = None. This way, if the if statement is not satisfied,buy_qtywill still have a value (None) that you can print later.Here's an updated version of your code with the fix:
... #Quantity buy_qty = None # Initialize with a default value for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: buy_qty = (price_buy, qty_buy) print(buy_qty) if buy_qty is not None: print(buy_qty) ...Make sure to do the same for the
sell_qtyvariable as well. - User
No buy_qty need to be only qty_buy
- Assistant
I apologize for the confusion. If you only need to print the
qty_buyvalue when the condition is met, you can modify the code accordingly. Here's an updated version:... # Quantity for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print(qty_buy) ...This code will print the
qty_buyvalue whenever theprice_buyis greater thanmark_price. If the condition is never satisfied, nothing will be printed. - User
And it need to be in one number
- Assistant
If you want to sum up all the
qty_buyvalues and print the total as one number, you can update the code to accumulate the quantities in a variable. Here's an updated version:... # Quantity total_qty_buy = 0.0 for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: total_qty_buy += qty_buy print(total_qty_buy) ...In this code, the
total_qty_buyvariable is initialized to 0.0 before the loop starts. Inside the loop, whenever the conditionprice_buy > mark_priceis satisfied, theqty_buyvalue is added to thetotal_qty_buyvariable. After the loop ends, the total quantity is printed as a single number.