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 = [] final_signal = [] exit = [] sell_result = 0.0 buy_result = 0.0 active_signal = None sell_qty = 0.0 buy_qty = 0.0 # 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 for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy < mark_price: buy_qty = buy_qty - qty_buy for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty = sell_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 print(buy_qty) print(sell_qty) #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 samerkhBut buy_qty and sell_qty are equal : -14222.251999999982 -14360.349999999999
It wrong , they need to be positive numbers
- Assistant
To fix the issue where
buy_qtyandsell_qtyare negative, you can modify the code in the following ways:-
In the first loop where you calculate
buy_qty, changebuy_qty = buy_qty - qty_buytobuy_qty = buy_qty + qty_buy. This will add the quantity instead of subtracting it. -
In the second loop where you calculate
sell_qty, changesell_qty = sell_qty - qty_selltosell_qty = sell_qty + qty_sell. Similarly, this will add the quantity instead of subtracting it.
Here's the updated code:
for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy < mark_price: buy_qty = buy_qty + qty_buy for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty = sell_qty + qty_sellWith these changes,
buy_qtyandsell_qtyshould have positive values. -
- User
But I need to subtract qty_buy from buy_qty
- Assistant
My apologies for the confusion. If you need to subtract
qty_buyfrombuy_qtyand have positive values for bothbuy_qtyandsell_qty, you can use the absolute value functionabs()to ensure positive values.Here's the modified code snippet:
for bid in bids: price_buy = float(bid[0]) qty_buy = float(bid[1]) if price_buy < mark_price: buy_qty = buy_qty - abs(qty_buy) for ask in asks: price_sell = float(ask[0]) qty_sell = float(ask[1]) if price_sell > mark_price: sell_qty = sell_qty - abs(qty_sell)By using
abs(), the values ofqty_buyandqty_sellwill be treated as positive quantities when subtracting frombuy_qtyandsell_qty, respectively, resulting in positive values for both variables.