Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

I used this code: def signalgenerator(df): if df is None or len(df) < 2: return ''…

  1. User

    I used this code: def signal_generator(df): if df is None or len(df) < 2: return ''

    signal = []
    
    # Retrieve depth data
    threshold = 0.15
    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)
    
    sell_qty_ratio = sell_qty / buy_qty
    buy_qty_ratio = buy_qty / sell_qty
    
    if sell_qty > buy_qty and sell_qty > (buy_qty * (1 + threshold)):
        signal.append('sell')
    elif buy_qty > sell_qty and buy_qty > (sell_qty * (1 + threshold)):
        signal.append('buy')
    else:
        signal.append('')
    
    return signal
    

    But it doesn't give me signal to sell

  2. Assistant

    Based on the code provided, it seems that the signal to sell is only added to the signal list if the condition sell_qty > buy_qty and sell_qty > (buy_qty * (1 + threshold)) is satisfied.

    If you are not receiving a signal to sell, it could be due to the fact that this condition is not being met in your particular data or scenario.

    To help troubleshoot further, you could print out the values of sell_qty, buy_qty, and (buy_qty * (1 + threshold)) to verify if the condition is not being met as expected. Additionally, you could check if the API data being used (depth_data, bid_depth, ask_depth) is providing accurate and up-to-date information.

    It's also worth mentioning that the code provided is incomplete, as it references a client object and a symbol variable, which are not defined within this code snippet. Make sure you have properly initialized client and assigned a valid value to symbol before using this code.

  3. User

    Give me code which will give me signal to sell too

  4. Assistant

    To include the signal to sell as well, you can modify the existing code by adding an additional condition. Here's an updated version of the code that will provide signals for both buying and selling:

    def signal_generator(df):
        if df is None or len(df) < 2:
            return ''
    
        signal = []
    
        # Retrieve depth data
        threshold = 0.15
        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)
    
        sell_qty_ratio = sell_qty / buy_qty
        buy_qty_ratio = buy_qty / sell_qty
    
        if sell_qty > buy_qty and sell_qty > (buy_qty * (1 + threshold)):
            signal.append('sell')
        elif sell_qty < buy_qty and buy_qty > (sell_qty * (1 + threshold)):
            signal.append('buy')
        else:
            signal.append('')
    
        return signal
    

    With this updated code, if the condition sell_qty > buy_qty and sell_qty > (buy_qty * (1 + threshold)) is not satisfied, it will check for the opposite condition sell_qty < buy_qty and buy_qty > (sell_qty * (1 + threshold)) to determine if a signal to buy should be generated.

    Make sure to initialize the client object and assign a valid value to symbol before using this code.