I used tihs code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I used tihs code: def signal_generator(df): if df is None or len(df) < 2: return ''
signal = [] final_signal = [] # Retrieve depth data threshold = 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: price_threshold = 7 / 100 elif mark_price_percent < -2: price_threshold = 7 / 100 else: price_threshold = 6 / 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 + threshold) > float(buy_qty)) and (sell_price < mark_price - price_threshold): signal.append('sell') elif (buy_result > (1 + threshold) > float(sell_qty)) and (buy_price > mark_price + price_threshold): signal.append('buy') else: signal.append('') if signal == ['buy'] and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02): final_signal.append('') elif signal == ['sell'] and (sell_result < buy_result - 0.01) and (buy_price > mark_price + 0.02): final_signal.append('') else: final_signal.append(signal) return final_signalBut for example signal is buy and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02) is not maching will ir return me buy signal ?
- Assistant
If the signal is "buy" and the conditions in the if statement are not met: (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02), then it will append an empty string to the final_signal list. So, in that case, the final_signal will not be equal to ['buy'], it will be equal to [''].
- User
Okay, if it will not print me uy, how my code need to seem for it will print me buy if statement are not met ?
- Assistant
If you want the code to print "buy" when the conditions in the if statement are not met, you can modify the code as follows:
if signal == ['buy'] and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02): final_signal.append('') else: final_signal.append('buy')By removing the else condition and appending 'buy' directly to the final_signal list, it will ensure that "buy" is added to the list when the conditions are not met.
- User
Okay, if I'll use this code: def signal_generator(df): if df is None or len(df) < 2: return ''
signal = [] final_signal = [] # Retrieve depth data threshold = 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: price_threshold = 7 / 100 elif mark_price_percent < -2: price_threshold = 7 / 100 else: price_threshold = 6 / 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 + threshold) > float(buy_qty)) and (sell_price < mark_price - price_threshold): signal.append('sell') elif (buy_result > (1 + threshold) > float(sell_qty)) and (buy_price > mark_price + price_threshold): signal.append('buy') else: signal.append('') if signal == ['buy'] and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02): final_signal.append('') else: final_signal.append('buy') if signal == ['sell'] and (sell_result < buy_result - 0.01) and (buy_price > mark_price + 0.02): final_signal.append('') else: final_signal.append('sell') return final_signal - Assistant
In the code you provided, if the conditions are not met for either the "buy" or "sell" signals, it will append the corresponding signal to the final_signal list.
Therefore, if the conditions for the "buy" signal are not met, it will append "buy" to the final_signal list. Similarly, if the conditions for the "sell" signal are not met, it will append "sell" to the final_signal list.
So, regardless of whether the conditions are met or not, the final_signal list will always contain either "buy" or "sell" as the signal.
- User
Tell me shortly yes or no will it print me buy or sell if statment are not met ? If no give me code which will return me signal if statment are not met
- Assistant
No, with the provided code, it will not print "buy" or "sell" if the conditions are not met.
If you want it to return the signal even if the conditions are not met, you can modify the code as follows:
if signal == ['buy'] and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02): final_signal.append('') else: final_signal.append('buy' if signal == ['buy'] else 'sell')By using a ternary operator, it will append 'buy' to the final_signal list when the signal is 'buy' and the conditions are not met, and it will append 'sell' to the final_signal list when the signal is 'sell' and the conditions are not met.
- User
But it wrong
- Assistant
Apologies for the confusion. If you want to return the signal even if the conditions are not met, you can set the initial value of
final_signalto besignalinstead of an empty list. This way, if the conditions are not met, the original signal will be returned. Here's the modified code:def signal_generator(df): if df is None or len(df) < 2: return '' signal = [] final_signal = signal # Initialize final_signal with signal # Rest of the code... if signal == ['buy'] and (buy_result < 0.01 + sell_result) and (sell_price < mark_price - 0.02): final_signal.append('') return final_signalIn this case, if the conditions are not met, the function will return 'buy' as the signal, instead of an empty string.