Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI 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 ''

    df['EMA5'] = df['Close'].ewm(span=5, adjust=False).mean()
    df['EMA20'] = df['Close'].ewm(span=20, adjust=False).mean()
    
    ema_analysis = []
    candle_analysis = []
    
    if (
        df['EMA5'].iloc[-1] > df['EMA20'].iloc[-1] and
        df['EMA5'].iloc[-2] < df['EMA20'].iloc[-2]
    ):
        ema_analysis.append('golden_cross')
    
    elif (
        df['EMA5'].iloc[-1] < df['EMA20'].iloc[-1] and
        df['EMA5'].iloc[-2] > df['EMA20'].iloc[-2]
    ):
        ema_analysis.append('death_cross')
    
    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):
        candle_analysis.append('sell')
    
        #Bullish pattern
    elif (open<close and
    previous_open>previous_close and
    close>previous_open and
    open<=previous_close):
        candle_analysis.append('buy')
    
    if ('golden_cross' in ema_analysis and 'buy' in candle_analysis):
        return 'buy'
    elif ('death_cross' in ema_analysis and 'sell' in candle_analysis):
        return 'sell'
    else:
        return ''
    

    But it giveing me right signals , please tell me what I need to change in my code for I can solve this problem ?

  2. Assistant

    It's unclear what the specific issue or problem you are experiencing with the code. However, I can help you optimize the code by suggesting a few improvements:

    1. Instead of using repeated indexing for the last two values of a column, you can use iloc to get the last two rows and access the required columns more efficiently. For example:
    ema5_current = df['EMA5'].iloc[-1]
    ema5_previous = df['EMA5'].iloc[-2]
    
    1. Instead of using multiple conditional statements and appending to lists, you can use a single if-elif-else statement to determine the analysis result. This can simplify the code and make it easier to read. For example:
    if ema5_current > ema20_current and ema5_previous < ema20_previous:
        ema_analysis = 'golden_cross'
    elif ema5_current < ema20_current and ema5_previous > ema20_previous:
        ema_analysis = 'death_cross'
    else:
        ema_analysis = ''
    
    if open > close and previous_open < previous_close and close < previous_open and open >= previous_close:
        candle_analysis = 'sell'
    elif open < close and previous_open > previous_close and close > previous_open and open <= previous_close:
        candle_analysis = 'buy'
    else:
        candle_analysis = ''
    
    if ema_analysis == 'golden_cross' and candle_analysis == 'buy':
        return 'buy'
    elif ema_analysis == 'death_cross' and candle_analysis == 'sell':
        return 'sell'
    else:
        return ''
    

    By making these changes, the code will be more concise and easier to understand. If you are still facing issues or have specific requirements, please provide more details so that I can assist you further.

  3. User

    But you dodn;t change any thing in my code

  4. Assistant

    Apologies for the confusion. I misunderstood your question. Looking at your code again, I don't see any apparent issues that would prevent it from giving correct signals. The code appears to be correctly checking for the presence of a "golden_cross" or "death_cross" in the EMA analysis and a "buy" or "sell" in the candlestick analysis.

    If the code is not giving the desired signals or you suspect an issue, please provide more information about the problem you are facing or the specific signals that are incorrect. Additionally, ensure that your input DataFrame (df) is correctly formatted and contains the necessary columns (e.g., 'Close', 'Open') for the calculations to work properly.