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) 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 final_signal == ['buy']: active_signal = 'buy' elif final_signal == ['sell']: active_signal = 'sell' else: active_signal = None 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('') if active_signal == 'buy' and ((buy_result < sell_qty) and (buy_price < mark_price)): final_signal.append('buy_exit') elif active_signal == 'sell' and ((sell_result < buy_qty) and (sell_price > mark_price)): final_signal.append('sell_exit') else: final_signal.append('') return final_signalBut will it return me buy_exit and sell_exit ?
- Assistant
No, this code will not return "buy_exit" or "sell_exit" as output. The code only appends empty strings,
'buy', or'sell'to thefinal_signallist based on certain conditions. The values "buy_exit" and "sell_exit" are not included in the code logic, so they will not be added to thefinal_signallist. - User
Give me code which will return them too
- Assistant
- User
But what did you change in this code, it apsolutly same code
- Assistant
Apologies for the confusion. You are correct, the code I provided is the same as the code you initially shared. Upon reviewing it again, I realized that the logic to append "buy_exit" and "sell_exit" is already present in the code.
Here is the relevant portion of the code that already includes the conditions to append "buy_exit" and "sell_exit" to the
final_signallist:if active_signal == 'buy' and ((buy_result < sell_qty) and (buy_price < mark_price)): final_signal.append('buy_exit') elif active_signal == 'sell' and ((sell_result < buy_qty) and (sell_price > mark_price)): final_signal.append('sell_exit') else: final_signal.append('')Therefore, the original code you shared will return "buy_exit" or "sell_exit" in the
final_signallist if the specified conditions are met. There is no need for any additional changes.