I used your code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I used your code: def signal_generator(df): if df is None or len(df) < 2: return ''
signal = [] # Retrieve depth data 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 buy_qty > sell_qty: signal.append('buy') elif sell_qty > buy_qty: signal.append('sell') else: signal.append('') return signalBut time to time it giving me too much diference signals , its wrong solve it please
- Assistant
To resolve the issue of inconsistent signals, you can try adding a condition to check for a minimum threshold of difference between buy_qty and sell_qty before considering it as a valid signal. Here's an updated version of your code implementing this change:
def signal_generator(df): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data 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) min_qty_threshold = 100 # Adjust this threshold as per your requirement if buy_qty > sell_qty + min_qty_threshold: signal.append('buy') elif sell_qty > buy_qty + min_qty_threshold: signal.append('sell') else: signal.append('') return signal - User
But no it too wrong
- Assistant
Apologies for the incorrect suggestion. To address the issue of inconsistent signals, you can try using a moving average or a smoothing technique to reduce noise in the data. This can help in providing more stable and consistent signals. Here's an example of how you can modify your code to incorporate a moving average:
def signal_generator(df, window=10): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data 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) # Calculate moving averages buy_qty_ma = sum(float(bid[1]) for bid in bid_depth[:window]) / window sell_qty_ma = sum(float(ask[1]) for ask in ask_depth[:window]) / window if buy_qty_ma > sell_qty_ma: signal.append('buy') elif sell_qty_ma > buy_qty_ma: signal.append('sell') else: signal.append('') return signal - User
But how I seeing else doesn't working in this code
- Assistant
Apologies for the oversight. If the
elsecondition is not working as expected in your code, it might be due to the fact that theifandelifconditions are not mutually exclusive. In other words, if neitherbuy_qty_ma > sell_qty_manorsell_qty_ma > buy_qty_maare true, theelsecondition will not be executed because either of the previous conditions will always be true.To address this, you can modify the code to include a separate condition to handle cases where
buy_qty_maandsell_qty_maare equal:def signal_generator(df, window=10): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data 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) # Calculate moving averages buy_qty_ma = sum(float(bid[1]) for bid in bid_depth[:window]) / window sell_qty_ma = sum(float(ask[1]) for ask in ask_depth[:window]) / window if buy_qty_ma > sell_qty_ma: signal.append('buy') elif sell_qty_ma > buy_qty_ma: signal.append('sell') else: signal.append('neutral') return signal