I used this signal generator code : def signalgenerator(df): Calculate EMA and MA lines…
- User
I used this signal generator code : def signal_generator(df): # Calculate EMA and MA lines df['EMA10'] = ta.EMA(df['Close'], timeperiod=10) df['EMA50'] = ta.EMA(df['Close'], timeperiod=50) df['EMA100'] = ta.EMA(df['Close'], timeperiod=100) df['EMA200'] = ta.EMA(df['Close'], timeperiod=200) df['MA10'] = ta.MA(df['Close'], timeperiod=10) df['MA50'] = ta.MA(df['Close'], timeperiod=50) df['MA100'] = ta.MA(df['Close'], timeperiod=100) df['MA200'] = ta.MA(df['Close'], timeperiod=200)
# Calculate the last candlestick last_candle = df.iloc[-1] # Check for bullish signals if last_candle['Close'] > last_candle['Open']: if last_candle['Close'] > last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min(): if last_candle[['EMA10', 'MA50', 'EMA100', 'EMA200']].iloc[-1].min() > last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].min(): return 'buy' # Check for bearish signals elif last_candle['Close'] < last_candle['Open']: if last_candle['Close'] < last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max(): if last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max() < last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].max(): return 'sell' # If no signal found, return an empty string return ''But I getting ERROR: BTC/USDT found in the market Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 294, in
signal = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 169, in signal_generator df['EMA10'] = ta.EMA(df['Close'], timeperiod=10) ^^^^^^ AttributeError: module 'ta' has no attribute 'EMA' - Assistant
It seems like the 'ta' library you are using does not have the 'EMA' function. You may want to check if you have imported the library correctly and have installed the proper version.
Alternatively, you can use a different library such as 'pandas_ta' which provides EMA function. Here's an example using 'pandas_ta':
import pandas_ta as ta
def signal_generator(df): # Calculate EMA and MA lines df['EMA10'] = ta.ema(df['Close'], length=10) df['EMA50'] = ta.ema(df['Close'], length=50) df['EMA100'] = ta.ema(df['Close'], length=100) df['EMA200'] = ta.ema(df['Close'], length=200) df['MA10'] = ta.sma(df['Close'], length=10) df['MA50'] = ta.sma(df['Close'], length=50) df['MA100'] = ta.sma(df['Close'], length=100) df['MA200'] = ta.sma(df['Close'], length=200)
# Calculate the last candlestick last_candle = df.iloc[-1] # Check for bullish signals if last_candle['Close'] > last_candle['Open']: if last_candle['Close'] > last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min(): if last_candle[['EMA10', 'MA50', 'EMA100', 'EMA200']].iloc[-1].min() > last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].min(): return 'buy' # Check for bearish signals elif last_candle['Close'] < last_candle['Open']: if last_candle['Close'] < last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max(): if last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max() < last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].max(): return 'sell' # If no signal found, return an empty string return '' - User
ERROR: 06/18/2023 10:24:37 BTC/USDT found in the market Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 294, in
signal = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 169, in signal_generator df['EMA10'] = ta.ema(df['Close'], length=10) ^^^^^^ AttributeError: module 'ta' has no attribute 'ema' - Assistant
It seems like the 'ta' library you are using does not have the 'ema' function. Make sure you have installed the latest version of 'pandas_ta' library that has the 'ema' function.
Alternatively, you can use the 'talib' library which has an 'EMA' function. Here's an example using 'talib':
import talib
def signal_generator(df): # Calculate EMA and MA lines df['EMA10'] = talib.EMA(df['Close'], timeperiod=10) df['EMA50'] = talib.EMA(df['Close'], timeperiod=50) df['EMA100'] = talib.EMA(df['Close'], timeperiod=100) df['EMA200'] = talib.EMA(df['Close'], timeperiod=200) df['MA10'] = talib.SMA(df['Close'], timeperiod=10) df['MA50'] = talib.SMA(df['Close'], timeperiod=50) df['MA100'] = talib.SMA(df['Close'], timeperiod=100) df['MA200'] = talib.SMA(df['Close'], timeperiod=200)
# Calculate the last candlestick last_candle = df.iloc[-1] # Check for bullish signals if last_candle['Close'] > last_candle['Open']: if last_candle['Close'] > last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min(): if last_candle[['EMA10', 'MA50', 'EMA100', 'EMA200']].iloc[-1].min() > last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].min(): return 'buy' # Check for bearish signals elif last_candle['Close'] < last_candle['Open']: if last_candle['Close'] < last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max(): if last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max() < last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].max(): return 'sell' # If no signal found, return an empty string return '' - User
Can you wright this code without talib
- Assistant
Yes, you can use 'pandas' library to calculate the EMA and MA lines instead of 'talib'. Here's the modified code:
import pandas as pd
def signal_generator(df): # Calculate EMA and MA 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() df['MA10'] = df['Close'].rolling(window=10).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean() df['MA200'] = df['Close'].rolling(window=200).mean()
# Calculate the last candlestick last_candle = df.iloc[-1] # Check for bullish signals if last_candle['Close'] > last_candle['Open']: if last_candle['Close'] > last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].min(): if last_candle[['EMA10', 'MA50', 'EMA100', 'EMA200']].iloc[-1].min() > last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].min(): return 'buy' # Check for bearish signals elif last_candle['Close'] < last_candle['Open']: if last_candle['Close'] < last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max(): if last_candle[['EMA10', 'EMA50', 'EMA100', 'EMA200']].iloc[-1].max() < last_candle[['MA10', 'MA50', 'MA100', 'MA200']].iloc[-1].max(): return 'sell' # If no signal found, return an empty string return ''
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.