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.2 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) sell_qty_ratio = sell_qty / buy_qty buy_qty_ratio = buy_qty / sell_qty if sell_qty > buy_qty: signal.append('sell') elif buy_qty > sell_qty: signal.append('buy') else: signal.append('') return signalCan you add please in my code those params: if buy_qty is higher more than 10 % than sell_qty signal.append('buy') elif sell_qty is higher more than 10 % than buy_qty signa.append('sell') else signa.append('')
- Assistant
Certainly! Here's the modified code with the additional parameters:
def signal_generator(df): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data threshold = 0.2 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) sell_qty_ratio = sell_qty / buy_qty buy_qty_ratio = buy_qty / sell_qty if sell_qty > buy_qty and sell_qty_ratio > 1.1: signal.append('sell') elif buy_qty > sell_qty and buy_qty_ratio > 1.1: signal.append('buy') else: signal.append('') return signalIn this modified code, I've added the conditions to check if
buy_qtyis higher by more than 10% thansell_qtyand vice versa. If these conditions are met, the appropriate signal ( or ) will be appended to the list. Otherwise, an empty string will be appended. - User
You need to set
thresholdto 10 5 - Assistant
Apologies for the confusion. Here's the modified code with the correct
thresholdvalues set to 10 and 5 respectively:def signal_generator(df): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data threshold = 10 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) sell_qty_ratio = sell_qty / buy_qty buy_qty_ratio = buy_qty / sell_qty if sell_qty > buy_qty and sell_qty_ratio > threshold: signal.append('sell') elif buy_qty > sell_qty and buy_qty_ratio > threshold: signal.append('buy') else: signal.append('') return signalNow, the
thresholdparameter is set to 10, and the conditions to check ifbuy_qtyis higher by more than 10% thansell_qtyand vice versa have been updated accordingly. - User
Are you sure about it will give me signals?
- Assistant
Apologies for the oversight in the code. You're correct, the function as written will only return a single signal, even if there are multiple instances that meet the criteria. To capture multiple signals, you can modify the code to return a list of signals instead. Here's the updated code:
def signal_generator(df): if df is None or len(df) < 2: return [] signals = [] # Retrieve depth data threshold = 10 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) sell_qty_ratio = sell_qty / buy_qty buy_qty_ratio = buy_qty / sell_qty if sell_qty > buy_qty and sell_qty_ratio > threshold: signals.append('sell') elif buy_qty > sell_qty and buy_qty_ratio > threshold: signals.append('buy') return signals - User
Because it doesn't give me any signals
- Assistant
I apologize for the confusion. Upon reviewing the code again, I noticed that there was a mistake in the condition check for signals. Here's the updated code that corrects the issue:
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) > (1 + threshold): signals.append('sell') elif buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold): signals.append('buy') return signalsIn this updated code, I modified the conditions to check if the is higher than the by more than 10% ( set to 0.1). If this condition is met, the signal is appended to the list. Similarly, if the is higher than the by more than 10%, the signal is appended to the list.
- User
I used your 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) > (1 + threshold): signal.append('sell') elif buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold): signal.append('buy') else: signal.append('') return signalBut it deosn't give me empty in terminal
- Assistant
Apologies for the confusion. The updated code I provided will not return an empty string if there are no signals. Instead, it will return an empty list
[]when there are no signals. If you want to return an empty string''when there are no signals, you can modify the code like this:def signal_generator(df): if df is None or len(df) < 2: return '' # 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) > (1 + threshold): return 'sell' elif buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold): return 'buy' else: return ''Now, the modified code will return an empty string when there are no signals, and or when there is a signal based on the condition.
- 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) > (1 + threshold): signal.append('sell') if buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold): signal.append('buy') if None in signal: signal.append('') return signalBut it doesn't giving me any sell signal
- Assistant
Apologies for the confusion. Upon reviewing the code again, I found an issue that could be causing the problem. Instead of checking
Nonein thesignallist, you should check if thesignallist is empty. Here's the updated 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) > (1 + threshold): signal.append('sell') if buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold): signal.append('buy') if not signal: signal.append('') return signal - User
But it too will doesn't give me any signal to sell
- Assistant
Apologies for the confusion. Upon further examination, I noticed another issue in the code. The comparison
(sell_qty / buy_qty) > (1 + threshold)should be changed to(buy_qty / sell_qty) > (1 + threshold)for the sell signal. Here's the corrected 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 (buy_qty / sell_qty) > (1 + threshold): signal.append('sell') if buy_qty > sell_qty and (sell_qty / buy_qty) > (1 + threshold): signal.append('buy') if not signal: signal.append('') return signal