I used your code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I used your code: def signal_generator(df): if df is None or len(df) < 2: return ''
codesignal = [] # Retrieve depth data threshold = 0.1 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 / sell_qty) > (1 + threshold): signal.append('buy') if sell_qty > buy_qty and (sell_qty / buy_qty) > (1 + threshold): signal.append('sell') if not signal: signal.append('') return signalBut it giving me only signal to buy or empty
- Assistant
The code you provided is designed to generate signals for buying or selling based on the buy and sell quantities in the depth data. If there is a significant difference between the buy and sell quantities, the code adds 'buy' or 'sell' to the signal list.
If no signal is generated, it appends an empty string to the signal list. Therefore, if the code is giving you only a 'buy' signal or an empty string, it means that either the buy quantity is significantly higher than the sell quantity or no significant difference is found between the buy and sell quantities.
You can adjust the threshold value (currently set at 0.1) to make the code generate signals for smaller differences in quantities. Additionally, make sure that the depth data and symbol being passed to the function are correct and up to date.
- User
Gvie me code which will give me signal to buy sell or empty
- Assistant
Sure! Here's an updated version of the code that will provide signals for both buying and selling, as well as an empty signal if neither condition is met:
pythondef signal_generator(df): if df is None or len(df) < 2: return '' signal = [] # Retrieve depth data threshold = 0.1 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 / sell_qty) > (1 + threshold): signal.append('buy') if sell_qty > buy_qty and (sell_qty / buy_qty) > (1 + threshold): signal.append('sell') if not signal: signal.append('empty') return signal