I used this code : client = UMFutures(key = apikey, secret = apisecret) df =…
- User
I used this code : client = UMFutures(key = api_key, secret = api_secret)
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) 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) > buy_qty) and (sell_price < mark_price - (pth + 0.08)): signal.append('sell') elif (buy_result > (1 + th) > 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
There could be a few reasons why the code is not generating any signals:
-
Data inconsistency: Make sure that the data retrieved from the API is accurate and up to date. Check if you are getting the correct depth data, ticker price, and 24-hour price change data. It is possible that the data returned is empty or not being retrieved correctly.
-
Threshold values: Check the values of
thandpthvariables. These thresholds determine the conditions for generating buy or sell signals. If the values are not appropriate for the current market conditions, the signals may not be triggered. -
Signal conditions: Review the conditions for generating buy or sell signals. If the market conditions or the order book depth do not meet the specified conditions, no signals will be generated. Ensure that the conditions are logical and appropriate for your trading strategy.
-