Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

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

  1. User

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

    signal = []
    last_final_signal = []
    final_signal = []
    
    # Retrieve depth data
    th = 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:
        pth = 7 / 100
    elif mark_price_percent < -2:
        pth = 7 / 100
    else:
        pth = 7 / 100
    
    executed_orders_buy = 0.0
    
    for order in bid_depth:
        if float(order[0]) == mark_price:
            executed_orders_buy += float(order[1])
        else:
            break
    
    buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0
    buy_result = buy_qty - executed_orders_buy
    
    executed_orders_sell = 0.0
    
    for order in ask_depth:
        if float(order[0]) == mark_price:
            executed_orders_sell += float(order[1])
        else:
            break
    
    sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
    sell_result = sell_qty - executed_orders_sell
    
    if (sell_result > (1 + th) > float(buy_qty)) and (sell_price < mark_price - (pth + 0.08)):
        signal.append('sell')
    elif (buy_result > (1 + th) > float(sell_qty)) and (buy_price > mark_price + (pth + 0.08)):
        signal.append('buy')
    else:
        signal.append('')
    
    if signal == ['buy'] and not ((buy_result < sell_qty) and (buy_price < mark_price)):
        last_final_signal.append('buy')
    elif signal == ['sell'] and not ((sell_result < buy_qty) and (sell_price > mark_price)):
        last_final_signal.append('sell')
    else:
        last_final_signal.append('')
    
    if last_final_signal == ['buy']:
        active_signal = 'buy'
    elif last_final_signal == ['sell']:
        active_signal = 'sell'
    else:
        active_signal = None
    
    if active_signal == 'buy' and ((buy_result < sell_qty) and (buy_price < mark_price)):
        final_signal.append('buy_exit')
    elif active_signal == 'sell' and ((sell_result < buy_qty) and (sell_price > mark_price)):
        final_signal.append('sell_exit')
    else:
        final_signal.append('')
    
    signal_2 = final_signal and last_final_signal
    
    return signal_2
    

    Don't tell me about data unput problem , just tell me will it return me final_signal and last_final_signal ?

  2. Assistant

    Yes, the code will return final_signal and last_final_signal in the variable signal_2.

  3. User

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

    signal = []
    last_final_signal = []
    final_signal = []
    
    # Retrieve depth data
    th = 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:
        pth = 7 / 100
    elif mark_price_percent < -2:
        pth = 7 / 100
    else:
        pth = 7 / 100
    
    executed_orders_buy = 0.0
    
    for order in bid_depth:
        if float(order[0]) == mark_price:
            executed_orders_buy += float(order[1])
        else:
            break
    
    buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0
    buy_result = buy_qty - executed_orders_buy
    
    executed_orders_sell = 0.0
    
    for order in ask_depth:
        if float(order[0]) == mark_price:
            executed_orders_sell += float(order[1])
        else:
            break
    
    active_signal = None
    
    sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
    sell_result = sell_qty - executed_orders_sell
    
    if (sell_result > (1 + th) > float(buy_qty)) and (sell_price < mark_price - (pth + 0.08)):
        signal.append('sell')
    elif (buy_result > (1 + th) > float(sell_qty)) and (buy_price > mark_price + (pth + 0.08)):
        signal.append('buy')
    else:
        signal.append('')
    
    if signal == ['buy'] and not ((buy_result < sell_qty) and (buy_price < mark_price)):
        last_final_signal.append('buy')
    elif signal == ['sell'] and not ((sell_result < buy_qty) and (sell_price > mark_price)):
        last_final_signal.append('sell')
    else:
        last_final_signal.append('')
    
    if final_signal == ['buy_exit']:
        active_signal = None
    elif final_signal == ['sell_exit']:
        active_signal = None
    else:
        active_signal = last_final_signal
    
    if last_final_signal == ['buy']:
        active_signal = 'buy'
    elif last_final_signal == ['sell']:
        active_signal = 'sell'
    else:
        active_signal = None
    
    if active_signal == 'buy' and ((buy_result < sell_qty) and (buy_price < mark_price)):
        final_signal.append('buy_exit')
    elif active_signal == 'sell' and ((sell_result < buy_qty) and (sell_price > mark_price)):
        final_signal.append('sell_exit')
    else:
        final_signal.append('')
    
    signal_2 = final_signal and last_final_signal
    
    return signal_2
    
  4. Assistant

    There is an error in the code. The line if final_signal == ['buy_exit']: and elif final_signal == ['sell_exit']: are checking the value of final_signal instead of last_final_signal.

    To fix it, the condition should be changed to check the value of last_final_signal. Here is the corrected code:

    def signal_generator(df):
        if df is None or len(df) < 2:
            return ''
        
        signal = []
        last_final_signal = []
        final_signal = []
    
        # Retrieve depth data
        th = 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:
            pth = 7 / 100
        elif mark_price_percent < -2:
            pth = 7 / 100
        else:
            pth = 7 / 100
    
        executed_orders_buy = 0.0
    
        for order in bid_depth:
            if float(order[0]) == mark_price:
                executed_orders_buy += float(order[1])
            else:
                break
    
        buy_qty = float(bid_depth[0][1]) if bid_depth else 0.0
        buy_result = buy_qty - executed_orders_buy
    
        executed_orders_sell = 0.0
    
        for order in ask_depth:
            if float(order[0]) == mark_price:
                executed_orders_sell += float(order[1])
            else:
                break
    
        active_signal = None
    
        sell_qty = float(ask_depth[0][1]) if ask_depth else 0.0
        sell_result = sell_qty - executed_orders_sell
    
        if (sell_result > (1 + th) > float(buy_qty)) and (sell_price < mark_price - (pth + 0.08)):
            signal.append('sell')
        elif (buy_result > (1 + th) > float(sell_qty)) and (buy_price > mark_price + (pth + 0.08)):
            signal.append('buy')
        else:
            signal.append('')
    
        if signal == ['buy'] and not ((buy_result < sell_qty) and (buy_price < mark_price)):
            last_final_signal.append('buy')
        elif signal == ['sell'] and not ((sell_result < buy_qty) and (sell_price > mark_price)):
            last_final_signal.append('sell')
        else:
            last_final_signal.append('')
    
        if last_final_signal == ['buy']:
            active_signal = 'buy'
        elif last_final_signal == ['sell']:
            active_signal = 'sell'
        else:
            active_signal = None
    
        if active_signal == 'buy' and ((buy_result < sell_qty) and (buy_price < mark_price)):
            final_signal.append('buy_exit')
        elif active_signal == 'sell' and ((sell_result < buy_qty) and (sell_price > mark_price)):
            final_signal.append('sell_exit')
        else:
            final_signal.append('')
    
        signal_2 = final_signal and last_final_signal
    
        return signal_2