I used your signalgenerator code: def signalgenerator(df): Calculate EMA and MA lines…
- User
I used your 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 current_price = df.Close.iloc[-1] previous_close = df.Close.iloc[-2]
# Initialize analysis variables ema_analysis = '' candle_analysis = '' pressure_analysis = '' # EMA crossover - buy signal if df.EMA10.iloc[-1] > df.EMA50.iloc[-1] and current_price > min(df[['EMA10', 'EMA50']].iloc[-1]): ema_analysis = 'buy' # EMA crossover - sell signal elif df.EMA10.iloc[-1] < df.EMA50.iloc[-1] and current_price < max(df[['EMA10', 'EMA50']].iloc[-1]): ema_analysis = 'sell' # EMA crossover - buy signal if df.EMA20.iloc[-1] > df.EMA200.iloc[-1] and current_price > min(df[['EMA20', 'EMA200']].iloc[-1]): ema_analysis = 'buy' # EMA crossover - sell signal elif df.EMA20.iloc[-1] < df.EMA200.iloc[-1] and current_price < max(df[['EMA20', 'EMA200']].iloc[-1]): ema_analysis = 'sell' # Check for bullish trends elif current_price > max(df[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1]): ema_analysis = 'buy' # Check for bearish trends elif current_price < min(df[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1]): ema_analysis = 'sell' # Check for bullish candlestick patterns elif df.Close.iloc[-1] > df.Open.iloc[-1] and df.Low.iloc[-1] > min(df[['Open', 'Close']].iloc[-1]): candle_analysis = 'buy' # Check for bearish candlestick patterns elif df.Close.iloc[-1] < df.Open.iloc[-1] and df.High.iloc[-1] < max(df[['Open', 'Close']].iloc[-1]): candle_analysis = 'sell' # Check if the current price is higher than the previous close, indicating buying pressure if current_price > previous_close: pressure_analysis = 'buy' # Check if the current price is lower than the previous close, indicating selling pressure elif current_price < previous_close: pressure_analysis = 'sell' # Combine all analysis into final signal if ema_analysis == 'buy' and candle_analysis == 'buy' and pressure_analysis == 'buy': return 'buy' elif ema_analysis == 'sell' and candle_analysis == 'sell' and pressure_analysis == 'sell': return 'sell' else: return ''But it doesn't give me any signal to buy or sell , please give me right code
- Assistant
The code you provided seems to be correct, but it might not be capturing the specific conditions for generating buy or sell signals based on your data. Here are a few things you can try to improve the functionality of the code:
-
Confirm that your DataFrame (
df) contains the necessary columns'Open','Close','Low', and'High'. -
Verify that the values in the DataFrame are in the correct format. If the values are stored as strings or objects instead of numerical values, they might not be compared accurately.
-
Ensure that your DataFrame is sorted in chronological order, with the most recent data at the end. You can use the
sort_values()function to ensure the correct order. -
Check if the values of your EMA and MA lines are performing as expected by printing them. You can add
print(df[['EMA5', 'EMA10', 'EMA20', 'EMA50', 'EMA100', 'EMA200']])after the initial calculation of EMA and MA lines to inspect their values.
-
- User
Give me just code
- Assistant