I used this signalgenerator code: def signalgenerator(df): Calculate EMA and MA lines…
- 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['EMA10'] = df['Close'].ewm(span=10, 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() df['MA10'] = df['Close'].rolling(window=10).mean() df['MA20'] = df['Close'].rolling(window=20).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean() # Extract necessary prices from df 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] current_price = df.Close.iloc[-1] # Initialize analysis variables ema_analysis = '' candle_analysis = '' # EMA crossover - buy signal if df.EMA10.iloc[-1] > df.EMA50.iloc[-1] and current_price > last_candle[['EMA10', 'EMA50']].iloc[-1].min(): ema_analysis = 'buy' # EMA crossover - sell signal elif df.EMA10.iloc[-1] < df.EMA50.iloc[-1] and current_price < last_candle[['EMA10', 'EMA50']].iloc[-1].max(): ema_analysis = 'sell' # EMA crossover - buy signal if df.EMA10.iloc[-1] > df.EMA200.iloc[-1] and current_price > last_candle[['EMA20', 'EMA200']].iloc[-1].min(): ema_analysis = 'buy' # EMA crossover - sell signal elif df.EMA10.iloc[-1] < df.EMA200.iloc[-1] and current_price < last_candle[['EMA20', 'EMA200']].iloc[-1].max(): ema_analysis = 'sell' # Check for bullish candlestick patterns #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' # Combine all analysis into final signal if ema_analysis == 'buy' and candle_analysis == 'buy': return 'buy' elif ema_analysis == 'sell' and candle_analysis == 'sell': return 'sell' else: return ''But it giveing me wrong signals, please give me right code
- Assistant
- User
But what did you change ?
- Assistant
Apologies for not providing a detailed explanation of the changes made. Here is a breakdown of the modifications made to the original code:
- Updated the variable
candle_analysisto be initialized as an empty list instead of an empty string (''). - Replaced some of the
ifstatements withelifstatements for the EMA crossover signals. This change ensures that conflicting signals are not triggered simultaneously. - Instead of returning "buy" or "sell" immediately upon encountering a candlestick pattern, added the corresponding signals to the
candle_analysislist. This allows for the evaluation of multiple candlestick patterns. - Combined all analysis into the final signal by checking if the lists
ema_analysisandcandle_analysisare not empty. If both lists contain signals, the final signal is determined based on the presence of "buy" signals in both lists. Otherwise, the signal is based on the presence of "buy" signals in either list. If both lists are empty, an empty string () is returned.
- Updated the variable
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.