I used your code: def signalgenerator(df): if df is None: return ‘’ df[‘EMA5’] =…
- User
I used your code: def signal_generator(df): if df is None: return ‘’
df[‘EMA5’] = df[‘Close’].ewm(span=5, adjust=False).mean() df[‘EMA10’] = df[‘Close’].ewm(span=10, adjust=False).mean() df[‘EMA5’] = df[‘Close’].ewm(span=5, adjust=False).mean() df[‘EMA12’] = df[‘Close’].ewm(span=12, adjust=False).mean() df[‘EMA25’] = df[‘Close’].ewm(span=25, 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()
ema_analysis = [] candle_analysis = [] volume_analysis = []
EMA strategy - buy signal
if df[‘EMA5’].iloc[-1] > df[‘EMA12’].iloc[-1] > df[‘EMA25’].iloc[-1]: ema_analysis.append(‘buy’)
EMA strategy - sell signal
elif df[‘EMA5’].iloc[-1] < df[‘EMA12’].iloc[-1] < df[‘EMA25’].iloc[-1]: ema_analysis.append(‘sell’)
Candlestick analysis
df[‘Body’] = df[‘Close’] - df[‘Open’] df[‘Range’] = df[‘High’] - df[‘Low’] df[‘UpperShadow’] = df[‘High’] - df[[‘Close’, ‘Open’]].max(axis=1) df[‘LowerShadow’] = df[[‘Close’, ‘Open’]].min(axis=1) - df[‘Low’]
df[‘BullishEngulfing’] = (df[‘Body’] > 0) & (df[‘Body’].shift() < 0) & (df[‘Open’] < df[‘Close’].shift()) df[‘BearishEngulfing’] = (df[‘Body’] < 0) & (df[‘Body’].shift() > 0) & (df[‘Open’] > df[‘Close’].shift()) df[‘Hammer’] = (df[‘UpperShadow’] >= 2 * df[‘Body’]) & (df[‘LowerShadow’] <= 0.1 * df[‘Body’]) df[‘HangingMan’] = (df[‘UpperShadow’] >= 2 * df[‘Body’]) & (df[‘LowerShadow’] >= 2 * df[‘Body’])
for i in range(len(df)): if df[‘BullishEngulfing’].iloc[i]: candle_analysis.append(‘bullish_engulfing’) elif df[‘BearishEngulfing’].iloc[i]: candle_analysis.append(‘bearish_engulfing’) elif df[‘Hammer’].iloc[i]: candle_analysis.append(‘hammer’) elif df[‘HangingMan’].iloc[i]: candle_analysis.append(‘hanging_man’) else: candle_analysis.append(‘’)
Volume strategy
df[‘VolMA20’] = df[‘Volume’].rolling(window=20).mean() df[‘VolMA50’] = df[‘Volume’].rolling(window=50).mean()
if df[‘VolMA20’].iloc[-1] > df[‘VolMA50’].iloc[-1]: volume_analysis.append(‘buy’) elif df[‘VolMA20’].iloc[-1] < df[‘VolMA50’].iloc[-1]: volume_analysis.append(‘sell’) else: volume_analysis.append(‘’)
if (‘buy’ in ema_analysis and ‘bullish_engulfing’ in candle_analysis and ‘buy’ in volume_analysis): return ‘buy’ elif (‘sell’ in ema_analysis and ‘bearish_engulfing’ in candle_analysis and ‘sell’ in volume_analysis): return ‘sell’ else: return ‘’ But it gave me loss
- Assistant
I'm sorry to hear that you experienced a loss using the code provided. The code you provided defines a signal generator function that uses various technical indicators and candlestick patterns to generate trading signals. However, without additional context or information about your specific trading strategy or the data you used, it's difficult to determine the exact cause of the loss.
Here are a few things you may consider in order to troubleshoot and improve your trading strategy:
-
Validate the data: Ensure that the input data is accurate and reliable. Check for any missing or incorrect values in the dataset.
-
- User
OKay, how my code need to seem with your advices ?
- Assistant
- User
Can you give me code with more better EMA strategy please
- Assistant
- User
But if you have more good strategy than this please give me them
- Assistant
Certainly! Here are a few additional strategies that you can consider incorporating into your trading code:
- RSI Strategy:
# RSI strategy df['RSI'] = ta.RSI(df['Close'], timeperiod=14) df['RSI_oversold'] = df['RSI'] < 30 df['RSI_overbought'] = df['RSI'] > 70 # RSI buy signal rsi_buy_signal = df['RSI'].iloc[-1] > 30 and df['RSI'].iloc[-2] < 30 # RSI sell signal rsi_sell_signal = df['RSI'].iloc[-1] < 70 and df['RSI'].iloc[-2] > 70 if rsi_buy_signal and 'bullish_engulfing' in candle_analysis and volume_buy_signal: return 'buy' elif rsi_sell_signal and 'bearish_engulfing' in candle_analysis and volume_sell_signal: return 'sell' - User
But how you EMA strategy with MACD strategy need to seem in my code ?
- Assistant
To incorporate both the EMA strategy and the MACD strategy into your code, you can combine the conditions for both strategies in the
ifstatements. Here's an updated version of your code with both strategies included:def signal_generator(df): if df is None: return '' df['EMA5'] = df['Close'].ewm(span=5, adjust=False).mean() df['EMA10'] = df['Close'].ewm(span=10, adjust=False).mean() df['EMA12'] = df['Close'].ewm(span=12, adjust=False).mean() df['EMA25'] = df['Close'].ewm(span=25, 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() ema_buy_signal = df['EMA5'].iloc[-1] >
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.