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

I used this signalgenerator code: def signalgenerator(df): if df.empty: return '' ema10…

  1. User

    I used this signal_generator code: def signal_generator(df): if df.empty: return ''

    ema_10 = ta.trend.EMAIndicator(df['Close'], window=10)
    ema_50 = ta.trend.EMAIndicator(df['Close'], window=50)
    ema_100 = ta.trend.EMAIndicator(df['Close'], window=100)
    ema_200 = ta.trend.EMAIndicator(df['Close'], window=200)
    ma_20 = ta.trend.SMAIndicator(df['Close'], window=20)
    
    ema_10_value = ema_10.ema_indicator()[-1]
    ema_50_value = ema_50.ema_indicator()[-1]
    ema_100_value = ema_100.ema_indicator()[-1]
    ema_200_value = ema_200.ema_indicator()[-1]
    ma_20_value = ma_20.sma_indicator()[-1]
    
    if (df['Open'].iloc[-1] < ema_10_value) and (df['Close'].iloc[-1] < ema_10_value) and (df['Open'].iloc[-1] > df['Close'].iloc[-2]) and (df['Open'].iloc[-1] > df['Close'].iloc[-1]) and (df['Close'].iloc[-1] < ma_20_value) and (ema_50_value < ema_100_value < ema_200_value):
        return 'sell'
    
    elif (df['Open'].iloc[-1] > ema_10_value) and (df['Close'].iloc[-1] > ema_10_value) and (df['Open'].iloc[-1] < df['Close'].iloc[-2]) and (df['Open'].iloc[-1] < df['Close'].iloc[-1]) and (df['Close'].iloc[-1] > ma_20_value) and (ema_50_value > ema_100_value > ema_200_value):
        return 'buy'
    
    else:
        return ''
    

    But I want to add this code too , code: open = df.open.iloc[-1] close = df.close.iloc[-1] previous_open = df.open.iloc[-2] previous_close = df.close.iloc[-2]

        #Bearish pattern
    if (open>close and 
    previous_open<previous_close and
    close<previous_open and 
    open>=previous_close):
        return 'sell'
    
        #Bullish pattern
    elif (open<close and
    previous_open>previous_close and
    close>previous_open and
    open<=previous_close):
        return 'buy'
    
  2. Assistant

    You can add the second code segment to the first one by checking the bearish and bullish patterns before checking the EMA and SMA conditions. Here is the updated code:

    def signal_generator(df): if df.empty: return ''

    open = df.open.iloc[-1]
    close = df.close.iloc[-1]
    previous_open = df.open.iloc[-2]
    previous_close = df.close.iloc[-2]
    
    # Bearish pattern
    if (open > close and previous_open < previous_close and close < previous_open and open >= previous_close):
        return 'sell'
    
    # Bullish pattern
    elif (open < close and previous_open > previous_close and close > previous_open and open <= previous_close):
        return 'buy'
    
    ema_10 = ta.trend.EMAIndicator(df['Close'], window=10)
    ema_50 = ta.trend.EMAIndicator(df['Close'], window=50)
    ema_100 = ta.trend.EMAIndicator(df['Close'], window=100)
    ema_200 = ta.trend.EMAIndicator(df['Close'], window=200)
    ma_20 = ta.trend.SMAIndicator(df['Close'], window=20)
    
    ema_10_value = ema_10.ema_indicator()[-1]
    ema_50_value = ema_50.ema_indicator()[-1]
    ema_100_value = ema_100.ema_indicator()[-1]
    ema_200_value = ema_200.ema_indicator()[-1]
    ma_20_value = ma_20.sma_indicator()[-1]
    
    if (df['Open'].iloc[-1] < ema_10_value) and (df['Close'].iloc[-1] < ema_10_value) and (df['Open'].iloc[-1] > df['Close'].iloc[-2]) and (df['Open'].iloc[-1] > df['Close'].iloc[-1]) and (df['Close'].iloc[-1] < ma_20_value) and (ema_50_value < ema_100_value < ema_200_value):
        return 'sell'
    
    elif (df['Open'].iloc[-1] > ema_10_value) and (df['Close'].iloc[-1] > ema_10_value) and (df['Open'].iloc[-1] < df['Close'].iloc[-2]) and (df['Open'].iloc[-1] < df['Close'].iloc[-1]) and (df['Close'].iloc[-1] > ma_20_value) and (ema_50_value > ema_100_value > ema_200_value):
        return 'buy'
    
    else:
        return ''
    
  3. User

    How it will work , explain me please

  4. Assistant

    The updated code works by first checking for bearish and bullish patterns using the previous and current day's open and close prices. If a bearish pattern is found, the function returns 'sell' and if a bullish pattern is found, it returns 'buy'.

    If no bearish or bullish pattern is found, then the function continues to check the EMA and SMA conditions. If the conditions for a sell signal are met, it returns 'sell'. If the conditions for a buy signal are met, it returns 'buy'. If none of the conditions are met, it returns an empty string.

    By combining the bearish/bullish pattern detection with the EMA/SMA conditions, the signal generator can provide more nuanced signals that take into account both short-term price patterns and longer-term trends.