I have this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…
- User
I have this code: def signal_generator(df): if df is None or len(df) < 2: return ''
# 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: return 'buy' elif sell_qty > buy_qty: return ' sell'I need code with my order book strategy , but this new code need to catch every top and bottom
- Assistant
Sure! Here's an updated version of the code that includes a strategy to catch every top and bottom using the order book data:
def signal_generator(df, symbol): if df is None or len(df) < 2: return '' # Retrieve depth data depth_data = client.depth(symbol=symbol) bid_depth = depth_data['bids'] ask_depth = depth_data['asks'] # Check if bid and ask depths are not empty if not bid_depth or not ask_depth: return '' buy_price = float(bid_depth[0][0]) sell_price = float(ask_depth[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) # Find the top and bottom prices top_price = float(bid_depth[0][0]) bottom_price = float(ask_depth[0][0]) for bid in bid_depth: if float(bid[0]) > top_price: top_price = float(bid[0]) for ask in ask_depth: if float(ask[0]) < bottom_price: bottom_price = float(ask[0]) # Check if the current mark price is at the top or bottom if mark_price == top_price: return 'sell' elif mark_price == bottom_price: return 'buy' else: return '' - User
But where is my strategy ?
- Assistant
Apologies for the confusion. If you have a specific strategy in mind, please provide details, and I'll help incorporate it into the code. Without explicit information about your desired strategy, it's challenging to include it in the code. Please describe the strategy you want to implement to catch every top and bottom, and I'll assist you accordingly.
- User
My strategy in my code you need to use this strategy and through this strategy your new code need to catch evry price top and bottom
- Assistant
Apologies for the confusion. Based on your response, if your strategy involves identifying every price top and bottom, you can modify the code as follows:
def signal_generator(df, symbol): if df is None or len(df) < 2: return '' # Retrieve depth data depth_data = client.depth(symbol=symbol) bid_depth = depth_data['bids'] ask_depth = depth_data['asks'] # Check if bid and ask depths are not empty if not bid_depth or not ask_depth: return '' buy_price = float(bid_depth[0][0]) sell_price = float(ask_depth[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) # Find the top and bottom prices top_prices = [] bottom_prices = [] for bid in bid_depth: price = float(bid[0]) if price > sell_price: top_prices.append(price) for ask in ask_depth: price = float(ask[0]) if price < buy_price: bottom_prices.append(price) # Determine the current position if top_prices and mark_price >= max(top_prices): return 'sell' elif bottom_prices and mark_price <= min(bottom_prices): return 'buy' else: return '' - User
OKay, explain me your code how does it wrok ?
- Assistant
Certainly! Here's an explanation of the code:
-
The
signal_generatorfunction takes in a dataframe (df) and a symbol as input. -
It first checks if the dataframe is None or has fewer than 2 rows. If this condition is met, it returns an empty string.
-
It retrieves the bid-depth and ask-depth data using the
client.depth()function. -
It checks if the bid and ask depths are empty. If either one is empty, it returns an empty string.
-