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

I used this signalgenerator code: def signalgenerator(df): Calculate EMA and MA lines…

  1. User

    I used this signal_generator code: def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['Close'].ewm(span=5, adjust=False).mean() df['EMA20'] = df['Close'].ewm(span=20, adjust=False).mean() df['EMA50'] = df['Close'].ewm(span=50, adjust=False).mean() df['EMA100'] = df['Close'].ewm(span=100, adjust=False).mean() df['EMA200'] = df['Close'].ewm(span=200, adjust=False).mean() # new line df['MA10'] = df['Close'].rolling(window=10).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean() open = df.Open.iloc[-1] close = df.Close.iloc[-1] previous_open = df.Open.iloc[-2] previous_close = df.Close.iloc[-2] # Calculate the last candlestick last_candle = df.iloc[-1]

    ema_analysis = ''
    candle_analysis = ''
    
    # Check for bullish trends
    if current_price > last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max():
        ema_analysis = 'buy'
    # Check for bullish candlestick pattern
    if (open<close and
    previous_open>previous_close and
    close>previous_open and
    open<=previous_close):
        candle_analysis = 'buy'
    
    # Check for bearish trends
    elif current_price < last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min():
        ema_analysis = 'sell'
    # Check for bearish candlestick pattern
    if (open>close and 
    previous_open<previous_close and
    close<previous_open and 
    open>=previous_close):
        candle_analysis = 'sell'
    
    # Check if both analyses are the same
    if ema_analysis == candle_analysis and ema_analysis != '':
        return ema_analysis
    
    # If no signal is found, return an empty string
    return ''
    

    But it giveing me only signals to sell, what I need to change in my code ?

  2. Assistant

    There are a few things that could be causing the code to only generate sell signals:

    1. The current_price variable is not defined in the code provided. Make sure it is defined and initialized to the current price of the asset.

    2. The logic for checking bullish and bearish trends is reversed. In the current code, if the current price is lower than all of the EMA lines, it is considered a bearish trend, which triggers a sell signal. Similarly, if the current price is higher than all of the EMA lines, it is considered a bullish trend, which triggers a buy signal. This should be reversed so that a higher price triggers a sell signal and a lower price triggers a buy signal.

    3. The candlestick pattern check may not be detecting bullish patterns correctly. Check if there are any errors in the logic or if additional checks are needed to properly detect bullish candlestick patterns.

    To fix these issues, consider making the following changes to the code:

    • Define and initialize the current_price variable to the current price of the asset.
    • Reverse the logic for detecting bullish and bearish trends by changing the '>' and '<' operators to '<' and '>' respectively.
    • Check if the candlestick pattern logic is correct and adjust if necessary.