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

I used this signalgenerator code: def signalgenerator(df): open = df.Open.iloc[-1] close…

  1. User

    I used this signal_generator code: def signal_generator(df): open = df.Open.iloc[-1] close = df.Close.iloc[-1] previous_open = df.Open.iloc[-2] previous_close = df.Close.iloc[-2] # Calculate EMA indicators 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)

    # Calculate MA indicator
    ma = ta.trend.SMAIndicator(df['Close'], window=20)
    
    # Calculate EMA indicator values
    ema_10_value = ema_10.ema_indicator()[-1] #get the EMA 10 indicator value for the last value in the DataFrame
    ema_50_value = ema_50.ema_indicator()[-1] #get the EMA 50 indicator value for the last value in the DataFrame
    ema_100_value = ema_100.ema_indicator()[-1] #get the EMA 100 indicator value for the last value in the DataFrame
    ema_200_value = ema_200.ema_indicator()[-1] #get the EMA 200 indicator value for the last value in the DataFrame
    ma_value = ma.sma_indicator()[-1] #get the MA indicator value for the last value in the DataFrame
    
    #Bearish pattern with EMA and MA indicators
    if (open>close and
    previous_open<previous_close and
    close<previous_open and
    open>=previous_close and
    close<ema_10_value and close<ema_50_value and close<ema_100_value and close<ema_200_value and close<ma_value):
        return 'sell'
    
    #Bullish pattern with EMA and MA indicators
    elif (open<close and
    previous_open>previous_close and
    close>previous_open and
    open<=previous_close and
    close>ema_10_value and close>ema_50_value and close>ema_100_value and close>ema_200_value and close>ma_value):
        return 'buy'
    
    #No clear pattern with EMA and MA indicators
    else:
        return ''
    

    How I can add SMA indicator ?

  2. Assistant

    To add SMA (Simple Moving Average) indicator, you can modify the existing code as follows:

    1. Import the necessary library:

      import ta

    2. Calculate the SMA indicator:

      Calculate SMA indicator

      sma = ta.trend.SMAIndicator(df['Close'], window=20)

    3. Calculate the SMA indicator values:

      Calculate SMA indicator values

      sma_value = sma.sma_indicator()[-1] #get the SMA indicator value for the last value in the DataFrame

    4. Modify the conditions in the "if" and "elif" statements to include the SMA indicator:

      #Bearish pattern with EMA, MA and SMA indicators if (open>close and previous_open<previous_close and close<previous_open and open>=previous_close and close<ema_10_value and close<ema_50_value and close<ema_100_value and close<ema_200_value and close<ma_value and close<sma_value): return 'sell'

      #Bullish pattern with EMA, MA and SMA indicators elif (openprevious_close and close>previous_open and open<=previous_close and close>ema_10_value and close>ema_50_value and close>ema_100_value and close>ema_200_value and close>ma_value and close>sma_value): return 'buy'

    Note that in this modified code, the "sma_value" variable is used in the conditions for the "if" and "elif" statements, and the "sma" variable is calculated outside of these statements.