You didn't continue your code: def signalgenerator(df): Calculate EMA lines df[‘EMA10’]…
- User
You didn't continue your code: def signal_generator(df): # Calculate EMA lines df[‘EMA10’] = df[‘Close’].ewm(span=10, 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()
# Calculate MACD df[‘EMA12’] = df[‘Close’].ewm(span=12, adjust=False).mean() df[‘EMA26’] = df[‘Close’].ewm(span=26, adjust=False).mean() df[‘MACD’] = df[‘EMA12’] - df[‘EMA26’] df[‘SignalLine’] = df[‘MACD’].ewm(span=9, adjust=False).mean() # Extract necessary prices from df close_price = df.Close.iloc[-1] previous_close = df.Close.iloc[-2] # Initialize analysis variables ema_analysis = [] macd_analysis = [] # EMA greatest line strategy - buy signal if df.EMA10.iloc[-1] > df.EMA50.iloc[-1] and df.EMA50.iloc[-1] > df.EMA100.iloc[-1] and df.EMA100.iloc[-1] > df.EMA200.iloc[-1] and close_price > max(df[‘EMA10’].iloc[-2], df[‘EMA50’].iloc[-2], df[‘EMA100’].iloc[-2], df[‘EMA200’].iloc[-2]): ema_analysis.append(‘buy’) # EMA greatest line strategy - sell signal elif df.EMA10.iloc[-1] < df.EMA50.iloc[-1] and df.EMA50.iloc[-1] < df.EMA100.iloc[-1] and df.EMA100.iloc[-1] < df.EMA200.iloc[-1] and close_price < min(df[‘EMA10’].iloc[-2], df[‘EMA50’].iloc[-2], df['EMA100 - Assistant
Apologies for the incomplete code. Here's the continuation:
# EMA greatest line strategy - sell signal elif df.EMA10.iloc[-1] < df.EMA50.iloc[-1] and df.EMA50.iloc[-1] < df.EMA100.iloc[-1] and df.EMA100.iloc[-1] < df.EMA200.iloc[-1] and close_price < min(df['EMA10'].iloc[-2], df['EMA50'].iloc[-2], df['EMA100'].iloc[-2], df['EMA200'].iloc[-2]): ema_analysis.append('sell') # Calculate MACD histogram df['MACD_Histogram'] = df['MACD'] - df['SignalLine'] # MACD histogram strategy - buy signal if df['MACD_Histogram'].iloc[-1] > 0 and df['MACD_Histogram'].iloc[-2] < 0: macd_analysis.append('buy') # MACD histogram strategy - sell signal elif df['MACD_Histogram'].iloc[-1] < 0 and df['MACD_Histogram'].iloc[-2] > 0: macd_analysis.append('sell') return ema_analysis, macd_analysisIn this continuation, I added the code for the MACD histogram strategy, which calculates the MACD histogram and generates buy and sell signals based on its values. The
ema_analysisandmacd_analysislists are used to store the generated signals for each strategy. The final return statement includes both lists to provide the analysis results. - User
Can you give me full code
- Assistant
- User
I used this 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 # Extract necessary prices from df # Extract necessary prices from df open_price = df.Open.iloc[-1] close_price = df.Close.iloc[-1] previous_open = df.Open.iloc[-2] previous_close = df.Close.iloc[-2]
- Assistant