I used this code: df = client.depth(symbol=symbol) def signalgenerator(df): if df is…
- User
I used this code: df = client.depth(symbol=symbol)
def signal_generator(df): if df is None or len(df) < 2: return ''
codesignal = [] final_signal = [] # Retrieve depth data th = 0.35 depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] buy_qty = sum(float(bid[1]) for bid in bids) bids = depth_data['bids'] highest_bid_price = float(bids[0][0]) lowest_bid_price = float(bids[-1][0]) buy_price = (highest_bid_price + lowest_bid_price) / 2 asks = depth_data['asks'] sell_qty = sum(float(ask[1]) for ask in asks) highest_ask_price = float(asks[0][0]) lowest_ask_price = float(asks[-1][0]) sell_price = (highest_ask_price + lowest_ask_price) / 2 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 executed_orders_buy = 0.0 for order in bids: if float(order[0]) == mark_price: executed_orders_buy += float(order[1]) else: break buy_qty = sum(float(bid[1]) for bid in bids) buy_result = buy_qty - executed_orders_buy executed_orders_sell = 0.0 for order in asks: if float(order[0]) == mark_price: executed_orders_sell += float(order[1]) else: break sell_qty = sum(float(ask[1]) for ask in asks) sell_result = sell_qty - executed_orders_sell if (sell_result > (1 + th) > float(buy_qty)) and (sell_price < mark_price - (pth + 0.08)): signal.append('sell') elif (buy_result > (1 + th) > float(sell_qty)) and (buy_price > mark_price + (pth + 0.08)): signal.append('buy') else: signal.append('') if signal == ['buy'] and not ((buy_result < sell_qty) and (buy_price < mark_price)): final_signal.append('buy') elif signal == ['sell'] and not ((sell_result < buy_qty) and (sell_price > mark_price)): final_signal.append('sell') else: final_signal.append('') return final_signal - Assistant
- User
But you didn't change any thing
- Assistant
Apologies for the confusion. It seems that I misunderstood your question.
Looking at the code again, I can see that
buy_qty,sell_qty,buy_price, andsell_priceare indeed calculated correctly within the function. Thedepth_datais obtained fromclient.depth(symbol=symbol)at the beginning of the function, and then the relevant values are extracted frombidsandasks.If you are observing that these variables are not changing, it might be because the function
signal_generatoris being called repeatedly with the samedfparameter, which in turn fetches the samedepth_dataeach time. To update the values, you need to ensure thatdfanddepth_dataare updated with fresh data before callingsignal_generator.I apologize for the confusion caused by my initial response. If you can provide more information or clarify your question, I'll be happy to assist you further.
- User
But they are doesn't updating
- Assistant
- User
OKay , here is depth data : 'bids': [['237.48', '3.122'], ['237.47', '0.047'], ['237.46', '1.137'], ['237.45', '10.373']
What is middle price for this algorithm and what is buy_qty ?
- Assistant
Based on the provided depth data:
'bids': [['237.48', '3.122'], ['237.47', '0.047'], ['237.46', '1.137'], ['237.45', '10.373']]
The middle price for this algorithm is calculated as the average of the highest bid price and the lowest bid price.
In this case, the highest bid price is 237.48 and the lowest bid price is 237.45.
Therefore, the middle price would be (237.48 + 237.45) / 2 = 237.465.
Regarding the buy_qty, it is calculated as the total quantity of all the bid orders.
In this case, the buy_qty would be the sum of the quantities from the provided depth data:
3.122 + 0.047 + 1.137 + 10.373 = 14.679.
So, the buy_qty would be 14.679.