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 ''
codesignal = [] # 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: signal.append('buy') elif sell_qty > buy_qty: signal.append('sell') else: signal.append('') return signalCan you add in my strategy new params : If sell_qty > buy_qty more than 5 % signal.append('sell') elif buy_qty > sell_qty more than 5% signal.append('buy')
- Assistant
Certainly! Here's the modified code with the additional parameters:
pythondef signal_generator(df): if df is None or len(df) < 2: return '' signal = [] # Retrieving depth data and other variables 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 sell_qty > buy_qty and (sell_qty - buy_qty) / sell_qty > 0.05: signal.append('sell') elif buy_qty > sell_qty and (buy_qty - sell_qty) / buy_qty > 0.05: signal.append('buy') else: signal.append('') return signal - User
Okay, explain me shortly how does working your code ?
- Assistant
Sure! Here's a brief explanation of how the modified code works:
- The
signal_generatorfunction takes a parameterdf, which I assume is a dataframe (although it is not used in the provided code). - Inside the function, the code checks if the dataframe
dfisNoneor has less than 2 rows. If either condition is true, an empty string is returned. - If the dataframe
dfmeets the requirements, the function proceeds to retrieve depth data, bid depth, ask depth, buy and sell prices, and mark price from the client API. - It then calculates the total buy quantity (
buy_qty) and the total sell quantity (sell_qty) by summing up the respective quantities from the bid and ask depths. - The modified code introduces two new conditions:
- The