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 = [] # Retrieve depth data threshold = 0.35 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 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 buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0 sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0 # Calculate the threshold for order price deviation if mark_price_percent > 2: price_threshold = 5 / 100 elif mark_price_percent < -2: price_threshold = 5 / 100 else: price_threshold = 4 / 100 executed_orders_buy = 0.0 for buy_price, buy_qty in bid_depth: if buy_price == mark_price: order_buy = buy_qty executed_orders_buy = order_buy # Subtract executed order quantity from buy_qty buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0 buy_result = buy_qty - executed_orders_buy 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 executed_orders_sell = 0.0 for sell_price, sell_qty in ask_depth: if sell_price == mark_price: order_sell = sell_qty executed_orders_sell = order_sell # Subtract executed order quantity from buy_qty buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0 sell_result = sell_qty - executed_orders_sell 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 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 if (sell_result > (1 + threshold) > float(buy_qty)) and (sell_price < mark_price - price_threshold): signal.append('sell') elif (buy_result > (1 + threshold) > float(sell_qty)) and (buy_price > mark_price + price_threshold): signal.append('buy') else: signal.append('') return signalBut I getting ERROR: Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 192, in
signals = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 134, in signal_generator sell_result = sell_qty - executed_orders_sell ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ TypeError: unsupported operand type(s) for -: 'str' and 'float' - Assistant
The error message indicates that you are trying to subtract a string from a float in the line
sell_result = sell_qty - executed_orders_sell. The problem likely arises from the fact thatsell_qtyis a string, not a float.To fix this issue, you need to convert
sell_qtyto a float before performing any arithmetic operations on it. You can do this by using thefloat()function. Here's the updated code:# Subtract executed order quantity from sell_qty sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0 sell_result = sell_qty - executed_orders_sellMake sure to apply the same modification to any other variables that may contain string values but are expected to be treated as floats in your calculations.