I sued this signalgenerator code: def signalgenerator(df): Calculate EMA and MA lines…
- User
I sued this signal_generator code: def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['Close'].ewm(span=5, 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() # new line df['MA10'] = df['Close'].rolling(window=10).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean() 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]
ema_analysis = '' candle_analysis = '' # Check for bullish trends if current_price > last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max(): ema_analysis = 'buy' # Check for bearish trends elif current_price < last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min(): ema_analysis = 'sell' # Check for bullish candlestick pattern if (open<close and previous_open>previous_close and close>previous_open and open<=previous_close): candle_analysis = 'buy' # Check for bearish candlestick pattern elif (open>close and previous_open<previous_close and close<previous_open and open>=previous_close): candle_analysis = 'sell' # Check if both analyses are the same if ema_analysis == candle_analysis and ema_analysis != '': return ema_analysis # If no signal is found, return an empty string return ''But it giveing me wrong signals, can you set them if price is riseing EMA and MA lines and after that price higher than EMA and MA lines signal = buy and if candle_analysis giveing signal to buy return buy elif if price is droping EMA and MA lines and after that price lower than EMA and MA lines signal = sell and if candle_analysis giveing signal to sell return sell
- Assistant
Here's the updated code that should give you the desired signals:
def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['Close'].ewm(span=5, 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() # new line df['MA10'] = df['Close'].rolling(window=10).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean()
- User
How does work this code ?
- Assistant
This updated code works by analyzing the current price of a financial instrument relative to its Exponential Moving Average (EMA) and Moving Average (MA) lines, as well as analyzing the current candlestick pattern.
The EMA and MA lines are calculated using the closing price of the financial instrument, with different time spans for each line (5, 20, 50, 100 and 200). The code checks whether the current price is above or below these lines, and compares it to the previous day's closing price.
If the current price is above all EMA and MA lines today and below them yesterday, it indicates a rising trend and the code outputs the 'buy' signal. Conversely, if the price is below all EMA and MA lines today and above them yesterday, it indicates a falling trend and the code outputs the 'sell' signal.
The code also analyzes the current and previous candlestick patterns to determine whether they are bullish or bearish, and checks for specific patterns to indicate a buy or sell signal.
If both the EMA/MA trend analysis and the candlestick pattern both give the same 'buy' or 'sell' signals, the code outputs the corresponding signal. If only one analysis gives a signal, the code outputs the signal from that analysis. If there is no signal, the code outputs an empty string.