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) > (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 why my code doesn't give me sell signal ?
- Assistant
There could be several reasons why your code does not give you a sell signal. Here are a few possibilities to consider:
-
Data Issue: Make sure that the
depth_dataandmark_price_datathat you retrieve from the client contain accurate and up-to-date information. You may want to print out these variables and check if they match your expectations. -
Threshold Value: The
thresholdvalue is set to 0.1 in your code. This means that the sell quantity must be at least 10% greater than the buy quantity for a sell signal to be generated. If the difference between the sell and buy quantities is lower than this threshold, no sell signal will be generated. You can try adjusting this threshold value to see if it affects the results. -
Calculation Issue: Make sure that you are correctly calculating the buy and sell quantities. Check that the bid and ask depths are being properly retrieved and that the quantities are being summed accurately.
-
Condition Evaluation: Double-check the logic in your conditional statements. Ensure that the conditions for generating a sell signal are evaluated correctly. You may want to print out the values of , , and the corresponding ratios to verify if the conditions are being met.
-
- User
Can you give me if elif and else code which will give me signal to buy sell or empty
- Assistant
Certainly! Here's the updated code with the if-elif-else block to generate signals for buy, sell, or empty:
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('empty') return signal - User
But it doesn't give me signal to sell
- Assistant
Apologies for the confusion. After reviewing the code, there seems to be an issue with the logic that checks for a sell signal. The condition
sell_qty > buy_qty and (sell_qty / buy_qty) > (1 + threshold)is incorrect for detecting a sell signal.Instead, the correct condition for a sell signal would be
buy_qty > sell_qty and (buy_qty / sell_qty) > (1 + threshold). This condition checks if the buy quantity is greater than the sell quantity by a certain threshold.Here's the updated code with the corrected condition for the sell signal:
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('empty') return signal