I used this code: # Define function to calculate EMA and SMA def EMA(close, period): ema…
- User
I used this code: # Define function to calculate EMA and SMA def EMA(close, period): ema = close.ewm(span=period, adjust=False).mean() return ema
def SMA(close, period): sma = close.rolling(window=period).mean() return sma
Define function to calculate Dragonfly Doji and Hammer patterns
def CDLDRAGONFLYDOJI(open, high, low, close): return (high - low) <= 2*(open - close) and high - close <= 0.25*(high - low) and high - open <= 0.25*(high - low)
def CDLHAMMER(open, high, low, close): return (high - low) > 3*(open - close) and (close - low) / (0.001 + high - low) > 0.6 and (open - low) / (0.001 + high - low) > 0.6
Define function to generate signals
def signal_generator(df): close = df.Close.iloc[-1] previous_close = df.Close.iloc[-2]
code# Calculate EMA and MA indicators ema_10 = EMA(df['Close'], 10) ema_50 = EMA(df['Close'], 50) ema_200 = EMA(df['Close'], 200) ma_20 = SMA(df['Close'], 20) # Identify Bullish/Bearish Candlestick Patterns bullish_candles = CDLDRAGONFLYDOJI(df['Open'], df['High'], df['Low'], df['Close']) bearish_candles = CDLHAMMER(df['Open'], df['High'], df['Low'], df['Close']) # Identify Trend trend = "" if ema_10.iloc[-1] > ema_50.iloc[-1] and ema_50.iloc[-1] > ema_200.iloc[-1]: trend = "Bullish" elif ema_10.iloc[-1] < ema_50.iloc[-1] and ema_50.iloc[-1] < ema_200.iloc[-1]: trend = "Bearish" else: trend = "Sideways" # Identify Signal signal = "" if close > previous_close or bullish_candles: signal = "Buy" elif close < previous_close or bearish_candles: signal = "Sell" else: signal = "Hold" return (trend, signal)But I getting ERROR: 06/17/2023 12:47:46 BTC/USDT found in the market Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 316, in signal = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 194, in signal_generator bullish_candles = CDLDRAGONFLYDOJI(df['Open'], df['High'], df['Low'], df['Close']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 177, in CDLDRAGONFLYDOJI return (high - low) <= 2*(open - close) and high - close <= 0.25*(high - low) and high - open <= 0.25*(high - low) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\pandas\core\generic.py", line 1466, in raise ValueError( ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
- Assistant
The error message suggests that the function CDLDRAGONFLYDOJI is returning a pandas Series object, which cannot be used as a boolean value in the if statement in the signal_generator function. To fix this, you can modify the CDLDRAGONFLYDOJI and CDLHAMMER functions to return a boolean value instead of a pandas Series. For example:
def CDLDRAGONFLYDOJI(open, high, low, close): return ((high - low) <= 2*(open - close)) & ((high - close) <= 0.25*(high - low)) & ((high - open) <= 0.25*(high - low))
def CDLHAMMER(open, high, low, close): return ((high - low) > 3*(open - close)) & (((close - low) / (0.001 + high - low)) > 0.6) & (((open - low) / (0.001 + high - low)) > 0.6)
Notice that the bitwise "and" operator (&) is used instead of the logical "and" operator (and), and parentheses are added to group the conditions properly. This should ensure that the functions return a single boolean value instead of a Series.
- User
I used your code, 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 316, in signal = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 208, in signal_generator if close > previous_close or bullish_candles: ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\pandas\core\generic.py", line 1466, in nonzero
raise ValueError( ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). - Assistant
The error message suggests that the issue is still with the truth value of the Series object returned by the
CDLDRAGONFLYDOJIfunction. Thebullish_candlesvariable in thesignal_generatorfunction is still a Series, which creates ambiguity in the boolean condition. To fix this, you can modify the function to return a scalar value (True or False) for each row in the DataFrame: