I sued this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I sued 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 (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 final_signal == ['buy']: active_signal = 'buy' elif final_signal == ['sell']: active_signal = 'sell' else: active_signal = None return final_signalYou need to add in my code those params: 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('')
- Assistant
- User
Will this code return me buy_exit and sell_exit ?
- Assistant
Yes, the added code logic will check if the
active_signalis 'buy' or 'sell' and if the conditions((buy_result < sell_qty) and (buy_price < mark_price))or((sell_result < buy_qty) and (sell_price > mark_price))are satisfied respectively. If they are satisfied, it will append 'buy_exit' or 'sell_exit' to thefinal_signallist, otherwise it will append an empty string. Therefore, this code will return 'buy_exit' or 'sell_exit' depending on the conditions being met. - User
- Assistant
Yes, the code you provided should work as expected.
The
exit_generatorfunction calls thesignal_generatorfunction to get thesignalvalue. Then it checks thesignalvalue and the corresponding conditions to determine if an exit signal should be generated. If the conditions are met, it appends 'buy_exit' or 'sell_exit' to theexit_final_signallist. Finally, it returns theexit_final_signal.Make sure you have the necessary libraries imported and the appropriate values assigned to the
symbolvariable before executing the code.