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 = [] # Retrieve depth data threshold = 0.1 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 buy_qty = sum(float(bid[1]) for bid in bid_depth) sell_qty = sum(float(ask[1]) for ask in ask_depth) if sell_qty > buy_qty and (sell_qty / buy_qty) > (0.5 + threshold): signal.append('sell') elif buy_qty > sell_qty and (buy_qty / sell_qty) > (0.5 + threshold): signal.append('buy') else: signal.append('') return signalBut it giving me only signal to buy , but I need signals to buy sell and empty
- Assistant
Based on the provided code, it seems that the signal list is initialized within the function for each call. This means that it will always have only one element, whether it's 'buy', 'sell', or ''. To get multiple signals, you need to modify the code to store the signals in a list for each iteration.
Here's an updated version of the code that should give you a list of signals containing 'buy', 'sell', and '' (empty):
def signal_generator(df): if df is None or len(df) < 2: return [] signals = [] # Retrieve depth data threshold = 0.1 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 buy_qty = sum(float(bid[1]) for bid in bid_depth) sell_qty = sum(float(ask[1]) for ask in ask_depth) if sell_qty > buy_qty and (sell_qty / buy_qty) > (0.5 + threshold): signals.append('sell') if buy_qty > sell_qty and (buy_qty / sell_qty) > (0.5 + threshold): signals.append('buy') return signals - User
Where is empty ???
- Assistant
Apologies for the confusion. It seems I misunderstood your requirement. To include an empty signal in the list, you can modify the code as follows:
def signal_generator(df): if df is None or len(df) < 2: return [] signals = [] # Retrieve depth data threshold = 0.1 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 buy_qty = sum(float(bid[1]) for bid in bid_depth) sell_qty = sum(float(ask[1]) for ask in ask_depth) if sell_qty > buy_qty and (sell_qty / buy_qty) > (0.5 + threshold): signals.append('sell') if buy_qty > sell_qty and (buy_qty / sell_qty) > (0.5 + threshold): signals.append('buy') if len(signals) == 0: signals.append('') return signalsNow, if none of the conditions for 'buy' and 'sell' are met, the
signalslist will include an empty string ''.