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 ''
signals = [] 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.0buy_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: signals.append('Bullish') elif sell_qty > buy_qty: signals.append('Bearish')
if 'Bearish' in signals: signal.append('sell') elif 'Bullish' in signals: signal.append('buy') else: return ''
return signal Yes it giving me right signals but time to time it giving me signals to buy and sell every second , and commission fee is getting all profit , I need code for long term positions 1 hour till 2 hours
- Assistant
To generate signals for long-term positions, you can introduce a delay between consecutive signals using the
timemodule. Additionally, you can implement a timer to reset the signals after a certain period, such as 1 to 2 hours. Here's an updated version of the code that incorporates these changes:import time def signal_generator(df): if df is None or len(df) < 2: return '' signals = [] signal = [] signal_timer = time.time() + 3600 # Timer for 1 hour duration while True: # 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: signals.append('Bullish') elif sell_qty > buy_qty: signals.append('Bearish') if time.time() > signal_timer: if 'Bearish' in signals: signal.append('sell') elif 'Bullish' in signals: signal.append('buy') else: signal.append('hold') # Reset signals and timer signals = [] signal_timer = time.time() + 7200 # Reset timer for 2 hours duration if signal: return signal time.sleep(1) # Delay between consecutive checks - User
You didn't understand me ! You need to give me code whoch will doesn't change positions every second
- Assistant
Apologies for the misunderstanding. To avoid changing positions every second, you can introduce a flag that keeps track of the current position. Once a signal is generated, you can check if it matches the current position, and only update the position if it has changed after a certain time duration. Here's an updated version of the code with this logic:
import time def signal_generator(df): if df is None or len(df) < 2: return '' signals = [] position = '' # Current position position_timer = time.time() + 3600 # Timer for 1 hour duration while True: # 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: signals.append('Bullish') elif sell_qty > buy_qty: signals.append('Bearish') if time.time() > position_timer: if 'Bearish' in signals: new_position = 'sell' elif 'Bullish' in signals: new_position = 'buy' else: new_position = 'hold' if new_position != position: position = new_position signals = [] # Reset signals # Reset timer position_timer = time.time() + 3600 # Reset timer for 1 hour duration if position: return position time.sleep(1) # Delay between consecutive checks