I have this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I have this 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 signalCan you add in my strategy those params : If buy_qty is higher than sell_qty more than 5% signal.append('buy') if sell_qty is higher than buy_qty more than 5% signal.append('sell')
- Assistant
Certainly! Here's how you can modify your code to incorporate the additional conditions:
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 and buy_qty >= 1.05 * sell_qty: signal.append('buy') elif sell_qty > buy_qty and sell_qty >= 1.05 * buy_qty: signal.append('sell') return signalIn this modified code, the conditions
buy_qty > sell_qtyand are still checked. However, an additional condition is added to check if the difference between buy_qty and sell_qty is at least 5% ( and , respectively). If this condition is satisfied, the corresponding signal ('buy' or 'sell') will be appended to the list. - User
Tell me please if buy qty is 55% and sel_Qty is 45% what your code shall do ?
- Assistant
Apologies for the confusion in my previous response. I misunderstood your requirement. In your case, if the buy quantity is 55% (0.55) and the sell quantity is 45% (0.45), and you want to trigger the 'buy' signal if the buy quantity is higher than the sell quantity by more than 5%, the code would be as follows:
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 and buy_qty >= 1.05 * sell_qty: signal.append('buy') elif sell_qty > buy_qty and sell_qty >= 1.05 * buy_qty: signal.append('sell') return signal - User
It wrong !Set in code if the buy_qty is more than 55% return buy and if sell_qty is more than 55% return sell
- Assistant
Apologies for the confusion. If you want to check if the buy quantity is more than 55% and return 'buy', and if the sell quantity is more than 55% and return 'sell', you can modify the code as follows:
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 > 0.55: signal.append('buy') elif sell_qty > 0.55: signal.append('sell') return signalIn this modified code, the conditions
buy_qty > 0.55andsell_qty > 0.55are checked to see if the buy quantity and sell quantity are more than 55% respectively. If the buy quantity is more than 55%, the 'buy' signal will be appended to the list. Similarly, if the sell quantity is more than 55%, the 'sell' signal will be appended to the list. - User
Set your algorithm in my code: my code: if buy_qty > sell_qty: signal.append('buy') elif sell_qty > buy_qty: signal.append('sell') else: signal.append('')
return signal - Assistant
Certainly! Here's the modified code with the algorithm included:
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 and buy_qty >= 1.05 * sell_qty: signal.append('buy') elif sell_qty > buy_qty and sell_qty >= 1.05 * buy_qty: signal.append('sell') elif buy_qty > 0.55: signal.append('buy') elif sell_qty > 0.55: signal.append('sell') else: signal.append('') return signal