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 = [] # 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 print("Total quantity of all ask orders:", total_quantity) print("Total quantity of all bid orders:", total_quantity_buy) # 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_signalBut depth data is doesn't updating
- Assistant
Based on the provided code, it seems that the depth data is not being updated within the
signal_generatorfunction. It appears that theclient.depthfunction is called outside of the function and thedepth_datavariable is not being updated inside the function.To ensure that the depth data is updated within the function, you can modify the code as follows:
-
Pass the
symbolas a parameter to the function, assuming that the variable is defined outside the function.
-
- User
Too doesn't updating
- Assistant
If the depth data is not updating even after modifying the code, there might be other factors causing the issue. Here are a few possible reasons:
-
The
clientobject: Make sure that theclientobject is properly initialized and connected to the correct API source. Verify that theclientobject is correctly instantiated and authenticated before using thedepthfunction. -
Symbol parameter: Ensure that the correct symbol is passed as a parameter to the
signal_generatorfunction. Double-check that the symbol is correctly specified as a string representation of the trading pair (e.g., 'BTCUSDT'). -
API rate limits: Check if your API provider has rate limits in place. If you exceed the API rate limits, you may not receive updated data. Consider checking the documentation or contacting your API provider to verify the rate limits.
-