def signal_generator(df):
if df is None or len(df) < 2:
return ''
code
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_israel = float(bid[1])
if price_buy > mark_price:
buy_qty = qty_buy_israel
buy_qty = qty_buy_israel
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
if price_sell > mark_price:
sell_qty = qty_sell_israel
sell_qty = qty_sell_israel
# 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
print(buy_qty)
print(sell_qty)
print(b_l)
print(s_h)
print(mark_price + pth)
buy_qty and sell_qty are printing me only one order ,but I need qty of every order
Assistant
In the code provided, the variables buy_qty and sell_qty are being overwritten in each iteration of the respective for loops. As a result, only the values from the last iteration are stored in those variables. If you need to store the quantities of every order, you can use a list to store the values.
Here's an example of how you can modify the code to store the quantities of every order:
python
buy_qty = []
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
if price_buy > mark_price:
buy_qty.append(qty_buy_israel)
sell_qty = []
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
if price_sell > mark_price:
sell_qty.append(qty_sell_israel)
With this modification, buy_qty and sell_qty will be lists that contain the quantities of each order that meets the condition.
Remember to update the code and variable usage accordingly for the rest of your code logic.
User
I used thi scode: #Quantity
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
print(qty_buy_israel)
if price_buy > mark_price:
buy_qty = qty_buy_israel
buy_qty = qty_buy_israel
code
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
print(qty_sell_israel)
if price_sell > mark_price:
sell_qty = qty_sell_israel
sell_qty = qty_sell_israel
But it giveing me this in terminal: 11.501
0.163
1.343
1.111
7.845
134.487
5.425
45.134
0.726
46.705
17.083
111.8
91.356
313.54
135.26
52.35
134.434
2.246
92.217
13.7
53.553
15.787
89.701
90.688
63.319
45.552
188.566
42.805
18.926
0.117
6.39
89.68
57.86
4.338
47.163
89.687
0.22
Assistant
Based on the modified code you provided, it seems that the printing of qty_buy_israel and qty_sell_israel is working correctly within the loop.
However, the issue is that buy_qty and sell_qty are being overwritten in each iteration of the loop, so they will only store the value from the last iteration. If you want to store all the quantities, you can use lists instead.
Here's an example of how you can modify your code to store all the quantities:
python
buy_qty = []
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
print(qty_buy_israel)
if price_buy > mark_price:
buy_qty.append(qty_buy_israel)
sell_qty = []
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
print(qty_sell_israel)
if price_sell > mark_price:
sell_qty.append(qty_sell_israel)
With this modification, buy_qty and sell_qty will be lists that contain all the quantities that meet the condition.
Make sure to update the code and variable usage accordingly for the rest of your logic.
User
I mean it giving me every order separately , but i need it will fold them
Assistant
If you want to calculate the total quantity of all the orders that meet the condition, you can use the sum() function to sum up the quantities in the respective lists buy_qty and sell_qty.
By using the sum() function, you can calculate the total quantity by summing up all the individual quantities in the buy_qty and sell_qty lists.
Remember to place the above code after the for loops that populate the buy_qty and sell_qty lists.
Let me know if this is what you were looking for or if you have any further questions!
User
I have a problem: Traceback (most recent call last):
File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 235, in
signals = signal_generator(df)
^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 118, in signal_generator
buy_qty = sum(qty_buy_israel)
^^^^^^^^^^^^^^^^^^^
TypeError: 'float' object is not iterable
Assistant
The error message indicates that you're trying to iterate over a float object in the line buy_qty = sum(qty_buy_israel). However, the sum() function expects an iterable, such as a list, as its argument.
To fix this issue, you need to store the individual quantities in the buy_qty list instead of assigning them directly to a float variable (qty_buy_israel).
Here's the modified code:
python
buy_qty = []
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
print(qty_buy_israel)
if price_buy > mark_price:
buy_qty.append(qty_buy_israel)
total_buy_quantity = sum(buy_qty)
print(total_buy_quantity)
Similarly, you can modify the code for the sell_qty calculation.
Make sure to update the code accordingly for the rest of your logic.
Let me know if you have any further questions!
User
Still getting same result
Assistant
I apologize for the confusion. It seems that I missed an important detail in your previous messages.
If you want to calculate the total quantity of orders, regardless of whether they meet the condition or not, you can modify the code as follows:
python
buy_qty = 0
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
print(qty_buy_israel)
buy_qty += qty_buy_israel
sell_qty = 0
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
print(qty_sell_israel)
sell_qty += qty_sell_israel
print(buy_qty)
print(sell_qty)
This code accumulates (+=) the quantities of every bid and ask order, regardless of whether the price condition is met or not.
Let me know if this resolves the issue for you, or if you have any further questions!
User
But it shoulld be too wrong
Assistant
I apologize for the confusion. If you want to calculate the total quantity of orders that meet a specific condition, you can modify the code as follows:
python
buy_qty = 0
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
if price_buy > mark_price:
buy_qty += qty_buy_israel
sell_qty = 0
for ask in asks:
price_sell = float(ask[0])
qty_sell_israel = float(ask[1])
if price_sell > mark_price:
sell_qty += qty_sell_israel
print(buy_qty)
print(sell_qty)
This code calculates the total quantity of buy and sell orders that meet the condition (price_buy > mark_price and price_sell > mark_price, respectively).
Please ensure that the variable mark_price is defined correctly and has the appropriate value before using it in comparisons.
Let me know if this resolves the issue or if you have any further questions!
User
But buy = 0
Assistant
Apologies for the misunderstanding. If the buy_qty is still printing as 0, it could indicate that none of the bid orders in the bids list meet the condition price_buy > mark_price.
To troubleshoot this further, you can add some additional print statements to check the values of price_buy and mark_price. For example:
python
buy_qty = 0
for bid in bids:
price_buy = float(bid[0])
qty_buy_israel = float(bid[1])
print(price_buy, mark_price) # Add this line for debugging purposes
if price_buy > mark_price:
buy_qty += qty_buy_israel
print(buy_qty)
By printing the values of price_buy and mark_price, you can compare them and identify any discrepancies. Make sure that mark_price has the correct value before the loop and that the price_buy values in the bids list are as expected.
If you're still facing issues, please provide more information about the values of price_buy, mark_price, and the structure of the bids list so that I can help you further.