I used your code: def signalgenerator(df): if df is None: return '' candleanalysis = []…
- User
I used your code: def signal_generator(df): if df is None: return ''
candle_analysis = [] ema_analysis = [] # EMA strategy df['EMA5'] = df['Close'].ewm(span=5, adjust=False).mean() df['EMA20'] = df['Close'].ewm(span=20, adjust=False).mean() # Identify trading signals if df['EMA5'].iloc[-1] > df['EMA20'].iloc[-1]: ema_analysis.append('golden_cross') # 5-period EMA crossed above 20-period EMA, bullish signal elif df['EMA5'].iloc[-1] < df['EMA20'].iloc[-1]: ema_analysis.append('death_cross') # 5-period EMA crossed below 20-period EMA, bearish signal for i in range(len(df)): open_price = df['Open'].iloc[i] high_price = df['High'].iloc[i] low_price = df['Low'].iloc[i] close_price = df['Close'].iloc[i] body = close_price - open_price range_ = high_price - low_price upper_shadow = high_price - max(close_price, open_price) lower_shadow = min(close_price, open_price) - low_price if body > 0 and body < 0 and open_price < df['Close']: candle_analysis.append('bullish_engulfing') elif body < 0 and body > 0 and open_price > df['Close']: candle_analysis.append('bearish_engulfing') elif upper_shadow >= 2 * body and lower_shadow <= 0.1 * body: candle_analysis.append('hammer') elif upper_shadow >= 2 * body and lower_shadow >= 2 * body: candle_analysis.append('hanging_man') else: candle_analysis.append('') if ('golden_cross' in ema_analysis and 'bullish_engulfing' in candle_analysis): return 'buy' elif ('death_cross' in ema_analysis and 'bearish_engulfing' in candle_analysis): return 'sell' else: return ''But it doesn't give me any signals
- Assistant
There could be multiple reasons why the code is not giving any signals. Here are a few possible issues to check:
-
Input data: Make sure the input dataframe (
df) contains the necessary columns (Open,High,Low,Close). Check if the data is correctly loaded or if there are any missing values. -
Indexing: Ensure that the index of the dataframe is set correctly and corresponds to the time series data. If the index is not in the correct format (e.g., not datetime objects), it can affect the calculations and signal generation.
-
Data range: Verify if the data range in the dataframe is sufficient to generate signals. The code is written to analyze the last data point (), so make sure there are enough rows in the dataframe to capture the required time frame for the analysis.
-
- User
Give me code which wil give me signals
- Assistant