I hace this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I hace this code: 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'] buy_qty = round(sum(float(bid[1]) for bid in bids), 0) highest_bid_price = float(bids[0][0]) lowest_bid_price = float(bids[1][0]) b_h = highest_bid_price b_l = lowest_bid_price asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(asks[0][0]) lowest_ask_price = float(asks[1][0]) s_h = highest_ask_price s_l = lowest_ask_price 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 print(f"Low {lowest_ask_price}") print(f"High {highest_ask_price}") print(asks) # 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 change constants highest_ask_price and lowest_ask_price , they need to get lowest price of all order book
- Assistant
To change the constants
highest_ask_priceandlowest_ask_priceto get the lowest price of all order book, you can modify the code as follows:- Replace the lines:
highest_ask_price = float(asks[0][0]) lowest_ask_price = float(asks[1][0])with:
highest_ask_price = float(min(ask[0] for ask in asks)) lowest_ask_price = float(max(ask[0] for ask in asks))This will calculate the minimum and maximum ask prices from the
askslist and assign them to the variableshighest_ask_priceandlowest_ask_pricerespectively. - User
Give me code which will subtract qty of orders which price is higher than mark_price
This is code of qty : asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(min(ask[0] for ask in asks)) lowest_ask_price = float(max(ask[0] for ask in asks)) s_h = highest_ask_price s_l = lowest_ask_price
- Assistant
To subtract the quantity of orders which price is higher than
mark_price, you can modify the code as follows:asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(min(ask[0] for ask in asks)) lowest_ask_price = float(max(ask[0] for ask in asks)) s_h = highest_ask_price s_l = lowest_ask_price # Subtract quantity if ask price is higher than mark price sell_qty_subtracted = 0.0 for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty_subtracted += qty_sell sell_qty -= sell_qty_subtractedIn this code, after calculating the
sell_qtyas before, a new variablesell_qty_subtractedis introduced to keep track of the subtracted quantity. Then, a loop is used to iterate through theaskslist, and if an ask price is higher than themark_price, the corresponding quantity is added tosell_qty_subtracted. Finally, the subtracted quantity is subtracted from the originalsell_qty. - User
And now system wich will print me lowest and highest price
- Assistant
To print the lowest and highest prices, you can add the following lines to your code:
print(f"Lowest Price: {lowest_ask_price}") print(f"Highest Price: {highest_ask_price}")This will print the lowest price (stored in the
lowest_ask_pricevariable) and the highest price (stored in thehighest_ask_pricevariable) to the console.Here's the updated code:
asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(min(ask[0] for ask in asks)) lowest_ask_price = float(max(ask[0] for ask in asks)) s_h = highest_ask_price s_l = lowest_ask_price print(f"Lowest Price: {lowest_ask_price}") print(f"Highest Price: {highest_ask_price}") # Subtract quantity if ask price is higher than mark price sell_qty_subtracted = 0.0 for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty_subtracted += qty_sell sell_qty -= sell_qty_subtractedNow, when you run the
signal_generatorfunction, it will calculate the lowest and highest prices and print them to the console. - User
I'm tolking about subtracted
- Assistant
Apologies for the confusion. To print the subtracted quantity, you can add the following line after subtracting the quantity:
print(f"Subtracted Quantity: {sell_qty_subtracted}")This will print the subtracted quantity (stored in the
sell_qty_subtractedvariable) to the console.Here's the updated code:
asks = depth_data['asks'] sell_qty = round(sum(float(ask[1]) for ask in asks), 0) highest_ask_price = float(min(ask[0] for ask in asks)) lowest_ask_price = float(max(ask[0] for ask in asks)) s_h = highest_ask_price s_l = lowest_ask_price # Subtract quantity if ask price is higher than mark price sell_qty_subtracted = 0.0 for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty_subtracted += qty_sell sell_qty -= sell_qty_subtracted print(f"Subtracted Quantity: {sell_qty_subtracted}")Now, when you run the
signal_generatorfunction, it will calculate the subtracted quantity and print it to the console.