gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
I used thi scode: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I used thi scode: def signal_generator(df): if df is None or len(df) < 2: return ''
signal = [] final_signal = [] exit = [] sell_result = 0.0 buy_result = 0.0 active_signal = None # Retrieve depth data depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] bid_prices = [float(bid[0]) for bid in bids] highest_bid_price = max(bid_prices) lowest_bid_price = min(bid_prices) b_h = highest_bid_price b_l = lowest_bid_price asks = depth_data['asks'] ask_prices = [float(ask[0]) for ask in asks] highest_ask_price = max(ask_prices) lowest_ask_price = min(ask_prices) s_h = highest_ask_price s_l = lowest_ask_price y_qty = round(sum(float(bid[1]) for bid in bids), 0) l_qty = round(sum(float(ask[1]) for ask in asks), 0) for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy < mark_price: buy_qty = y_qty - qty_buy for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty = l_qty - qty_sell mark_price_data = client.ticker_price(symbol=symbol) mp = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0 mark_price_data_change = client.ticker_24hr_price_change(symbol=symbol) mark_price_percent = float(mark_price_data_change['priceChangePercent']) if 'priceChangePercent' in mark_price_data_change else 0.0 # Calculate the threshold for order price deviation if mark_price_percent > 2: pth = 7 / 100 elif mark_price_percent < -2: pth = 7 / 100 else: pth = 7 / 100 # Subtract quantity if bid price is the same as mark price for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy == mark_price: buy_result = buy_qty - qty_buy br = buy_result # Subtract quantity if bid price is the same as mark price for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell == mark_price: sell_result = sell_qty - qty_sell sr = sell_result if mark_price_percent > 2: th = 20000 elif mark_price_percent < -2: th = 20000 else: th = 13000 #Main Signal if (((buy_qty > th) > (sell_qty < th)) and (b_l > mp + pth)): signal.append('buy') elif (((sell_qty > th) > (buy_qty < th)) and (s_h < mp - pth)): signal.append('sell') else: signal.append('') #Signal confirmation if signal == ['buy'] and not (((br < th) < sell_qty) and (b_l < mp + pth)): final_signal.append('buy') elif signal == ['sell'] and not (((sr < th) < buy_qty) and (s_h > mp - pth)): final_signal.append('sell') else: final_signal.append('') #Exit if final_signal == ['buy'] and (((br < th) < sell_qty) and (b_h < mp + pth)): exit.append('buy_exit') elif final_signal == ['sell'] and (((sr < th) < buy_qty) and (s_l > mp - pth)): exit.append('sell_exit') else: exit.append('') samerkh = exit and final_signal return samerkhYou need to add system for every bid and for every ask that if price_buy = float(bid[0]) < mark_price + (mark_price / 100) don;t count this order and order qty
- Assistant
Here's the modified code with the added system to exclude certain orders based on the condition you mentioned:
def signal_generator(df): if df is None or len(df) < 2: return '' signal = [] final_signal = [] exit = [] sell_result = 0.0 buy_result = 0.0 active_signal = None # Retrieve depth data depth_data = client.depth(symbol=symbol) bids = depth_data['bids'] bid_prices = [float(bid[0]) for bid in bids] highest_bid_price = max(bid_prices) lowest_bid_price = min(bid_prices) b_h = highest_bid_price b_l = lowest_bid_price asks = depth_data['asks'] ask_prices = [float(ask[0]) for ask in asks] highest_ask_price = max(ask_prices) lowest_ask_price = min(ask_prices) s_h = highest_ask_price s_l = lowest_ask_price y_qty = round(sum(float(bid[1]) for bid in bids), 0) l_qty = round(sum(float(ask[1]) for ask in asks), 0) for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy < mark_price and price_buy >= mark_price + (mark_price / 100): buy_qty = y_qty - qty_buy for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price and price_sell <= mark_price + (mark_price / 100): sell_qty = l_qty - qty_sell mark_price_data = client.ticker_price(symbol=symbol) mp = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0 mark_price_data_change = client.ticker_24hr_price_change(symbol=symbol) mark_price_percent = float(mark_price_data_change['priceChangePercent']) if 'priceChangePercent' in mark_price_data_change else 0.0 # Calculate the threshold for order price deviation if mark_price_percent > 2: pth = 7 / 100 elif mark_price_percent < -2: pth = 7 / 100 else: pth = 7 / 100 # Subtract quantity if bid price is the same as mark price for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy == mark_price: buy_result = buy_qty - qty_buy br = buy_result # Subtract quantity if bid price is the same as mark price for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell == mark_price: sell_result = sell_qty - qty_sell sr = sell_result if mark_price_percent > 2: th = 20000 elif mark_price_percent < -2: th = 20000 else: th = 13000 #Main Signal if (((buy_qty > th) > (sell_qty < th)) and (b_l > mp + pth)): signal.append('buy') elif (((sell_qty > th) > (buy_qty < th)) and (s_h < mp - pth)): signal.append('sell') else: signal.append('') #Signal confirmation if signal == ['buy'] and not (((br < th) < sell_qty) and (b_l < mp + pth)): final_signal.append('buy') elif signal == ['sell'] and not (((sr < th) < buy_qty) and (s_h > mp - pth)): final_signal.append('sell') else: final_signal.append('') #Exit if final_signal == ['buy'] and (((br < th) < sell_qty) and (b_h < mp + pth)): exit.append('buy_exit') elif final_signal == ['sell'] and (((sr < th) < buy_qty) and (s_l > mp - pth)): exit.append('sell_exit') else: exit.append('') samerkh = exit and final_signal return samerkhNote: It's important to ensure that the variables
symbol,mark_price, andclientare defined and accessible within the function for it to work correctly.