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 = [] sell_result = 0.0 buy_result = 0.0 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) b_l = lowest_bid_price > mark_price + (mark_price / 100) b_h = highest_bid_price > mark_price + ((mark_price / 100) * 2) 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) s_l = lowest_ask_price < mark_price - ((mark_price / 100) * 2) s_h = highest_ask_price < mark_price - (mark_price / 100) y_qty = round(sum(float(bid[1]) for bid in bids), 0) l_qty = round(sum(float(ask[1]) for ask in asks), 0) #Quantity for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if not price_buy < mark_price + (mark_price / 100): buy_qty = y_qty - qty_buy for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if not price_sell > mark_price - (mark_price / 100): sell_qty = l_qty - qty_sell # 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 # 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 br = 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 sr = sell_result if mark_price_percent > 2: th = 20000 elif mark_price_percent < -2: th = 20000 else: th = 15000 #Main Signal if (((buy_qty > th) > (sell_qty < th)) and (b_l > mp + pth)): signal.append('buy') elif (((sell_qty > th) > (buy_qty < th)) and (s_h < mp - pth)): signal.append('sell') else: signal.append('') #Signal confirmation if signal == ['buy'] and not (((br < th) < sell_qty) and (b_l < mp + pth)): final_signal.append('buy') elif signal == ['sell'] and not (((sr < th) < buy_qty) and (s_h > mp - pth)): final_signal.append('sell') else: final_signal.append('') #Exit if final_signal == ['buy'] and (((br < th) < sell_qty) and (b_h < mp + pth)): exit.append('buy_exit') elif final_signal == ['sell'] and (((sr < th) < buy_qty) and (s_l > mp - pth)): exit.append('sell_exit') else: exit.append('') samerkh = exit and final_signal return samerkhBut terminal returned me: Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 230, in signals = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 138, in signal_generator buy_result = buy_qty - qty_buy ^^^^^^^ UnboundLocalError: cannot access local variable 'buy_qty' where it is not associated with a value
- Assistant
The error message is indicating that the variable 'buy_qty' is being used before it has been assigned a value.
Looking at your code, it seems that the variable 'buy_qty' is being assigned a value in this line:
buy_qty = y_qty - qty_buyHowever, this statement is inside a loop and it may not execute if the condition
if not price_buy < mark_price + (mark_price / 100)is never satisfied. In that case, the variable 'buy_qty' will not be assigned a value, causing the error when you try to use it later on.To fix this issue, you can initialize the 'buy_qty' variable before the loop with a default value, so that it is always assigned a value:
buy_qty = 0.0 for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if not price_buy < mark_price + (mark_price / 100): buy_qty = y_qty - qty_buyMake sure to do the same for the 'sell_qty' variable as well if it is necessary for your code logic.
- User
I need code for bid orders which will count quantity only of bids which price higher than mark_price
- Assistant
To count the quantity of bids that have a price higher than
mark_price, you can modify your code as follows:buy_qty = 0.0 for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: buy_qty += qty_buyIn this code, the variable
buy_qtyis initialized before the loop starts, and each time a bid with a price higher thanmark_priceis found, the quantity (qty_buy) is added tobuy_qty. This way,buy_qtywill count the total quantity of bids with a higher price thanmark_price. - User
But it can not be 0.0
- Assistant
If you don't want the initial value of
buy_qtyto be 0.0, you can assign it a value that fits your specific use case.For example, if you want to set
buy_qtytoNoneinitially, you can do the following:buy_qty = None for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: if buy_qty is None: buy_qty = qty_buy else: buy_qty += qty_buyIn this code,
buy_qtyis initialized asNone. If a bid with a price higher thanmark_priceis found,qty_buyis added tobuy_qty. Ifbuy_qtyis stillNone, it means no qualifying bids have been found yet, sobuy_qtyis set to the currentqty_buyvalue. Ifbuy_qtyis notNone, the currentqty_buyvalue is added to it. This way,buy_qtywill accumulate the total quantity of qualifying bids. - User
It need to print me bids quantity which price is higher than mark_price
- Assistant
To print the quantities of bids that have a price higher than
mark_price, you can modify the code as follows:for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print(qty_buy)In this code, the loop iterates over each bid, checks if the
price_buyis higher thanmark_price, and if it is, it prints theqty_buy. This will print the quantity of each bid that meets the condition. - User
OKay, give me code which will rpint me bids which price is higher than mark_price
- Assistant
Here's the modified code to print the bids with a price higher than
mark_price:for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print(bid)In this code, the loop iterates over each bid, and if the
price_buyis higher thanmark_price, it prints the entirebidobject. This will output the bids that have a price higher thanmark_price. - User
It need to print me bids which [rice is higher than mark_price and print their quantity
- Assistant
To print the bids with a price higher than
mark_priceand their corresponding quantities, you can modify the code as follows:for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print("Bid Price:", price_buy, "Quantity:", qty_buy)In this code, the loop iterates over each bid, and if the
price_buyis higher thanmark_price, it prints the bid's price and quantity. This will output the bids that have a price higher thanmark_pricealong with their respective quantities. - User
And I should need to use it in othr codes ,can you make their qty is a constant
- Assistant
If you want to make the quantity a constant value for all the qualifying bids, you can set a constant variable and use it in the
printstatement. Here's an example:constant_qty = 10 for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print("Bid Price:", price_buy, "Quantity:", constant_qty)In this code,
constant_qtyis set to 10 (you can change it to your desired constant value). Inside the loop, for every bid with a price higher thanmark_price, it will print the bid's price along with theconstant_qty. This ensures that the quantity remains constant for all qualifying bids. - User
But it taking qty only of one order and I need of every which price is higher than mark_price
- Assistant
Apologies for the confusion. If you want to print the quantity for each bid separately, you can modify the code as follows:
for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy > mark_price: print("Bid Price:", price_buy, "Quantity:", qty_buy)In this updated code, the loop iterates over each bid. If the
price_buyis higher thanmark_price, it will print the bid's price and its corresponding quantity (qty_buy). This will output the price and quantity for each bid separately, but only for the bids that have a price higher thanmark_price. - User
OKay, give me code which will print every bid wihch price is higher than mark_price
- Assistant
Certainly! Here's the modified code to print every bid with a price higher than
mark_price:for bid in bids: price_buy = float(bid[0]) if price_buy > mark_price: print(bid)In this code, the loop iterates over each bid. If the
price_buyis higher thanmark_price, it prints the entirebidobject. This will output each bid that has a price higher thanmark_price. - User
But I need evry bid
- Assistant
Apologies for the confusion. If you want to print every bid, regardless of its price, you can use the following code:
for bid in bids: print(bid)This code will iterate over each bid and print the entire
bidobject. It will output every bid, regardless of the price.