I used this code: def signalgenerator(df): if df is None or len(df) < 2: return '' if…
- User
I used this code: def signal_generator(df): if df is None or len(df) < 2: return ''
if (mark_price_percent > 2) or (mark_price_percent < -2): signal = [] final_signal = [] # Retrieve depth data th = 0.35 depth_data = client.depth(symbol=symbol) bid_depth = depth_data['bids'] ask_depth = depth_data['asks'] buy_price = float(bid_depth[0][0]) if bid_depth else 0.0 sell_price = float(ask_depth[0][0]) if ask_depth else 0.0 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 buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0 sell_qty = float(ask_depth[0][1]) if ask_depth 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 bid_depth: if float(order[0]) == mark_price: executed_orders_buy += float(order[1]) else: break buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0 buy_result = buy_qty - executed_orders_buy executed_orders_sell = 0.0 for order in ask_depth: if float(order[0]) == mark_price: executed_orders_sell += float(order[1]) else: break sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0 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_signalWill this code return me signals or no ?
- Assistant
No, this code will not return any signals. There are several issues with the code that will prevent it from generating signals:
-
The variable
mark_price_percentis being used before it has been defined. It should be defined before the if condition that checks its value. -
The variables
symbolandclientare not defined in the function. You need to ensure that they are defined and passed to the function as arguments. -
The
signalandfinal_signallists are being defined inside the if condition, which means they will be empty if the condition is not met. You should define them outside the if condition, before the if statement. -
The conditions inside the if statements that check for buy and sell signals are complex and it's not clear how they are intended to work. Without knowing the specific requirements, it's difficult to determine if the conditions are correct.
-
The return statement is inside the if condition, which means it will only be executed if the condition is met. If the condition is not met, the function will not return anything.
-
- User
If you'll not count data input problem will or no ?
- Assistant
Assuming the input data is provided correctly and the necessary variables (
symbolandclient) are defined and passed to the function, there are still issues with the code that will prevent it from generating signals.The conditions for buy and sell signals in the if statements are complex and it's not clear how they are intended to work. Without knowing the specific requirements and logic behind these conditions, it is difficult to determine if they are correct.
Additionally, the logic for calculating
pthand the placement of the return statement can be improved for clarity.You would need to address these issues, clarify the conditions for buy and sell signals, and ensure the logic is correct in order to generate the desired signals.