import numpy as np import pandas as pd def identify_patterns(df): """Identify candlestick patterns in the data using basic calculations""" patterns = pd.DataFrame(index=df.index) # Calculate basic candlestick properties body = df['Close'] - df['Open'] body_abs = abs(body) upper_shadow = df['High'] - df[['Open', 'Close']].max(axis=1) lower_shadow = df[['Open', 'Close']].min(axis=1) - df['Low'] # 1. Hammer Pattern patterns['HAMMER'] = np.where( (lower_shadow > 2 * body_abs) & # Long lower shadow (upper_shadow <= 0.1 * body_abs) & # Minimal upper shadow (body > 0), # Bullish close 1, 0 ) # 2. Inverted Hammer Pattern patterns['INVERTED_HAMMER'] = np.where( (upper_shadow > 2 * body_abs) & # Long upper shadow (lower_shadow <= 0.1 * body_abs) & # Minimal lower shadow (body > 0), # Bullish close 1, 0 ) # 3. Piercing Line Pattern patterns['PIERCING_LINE'] = np.where( (body.shift(1) < 0) & # Previous candle bearish (body > 0) & # Current candle bullish (df['Open'] < df['Close'].shift(1)) & # Opens below previous close (df['Close'] > (df['Open'].shift(1) + df['Close'].shift(1)) / 2), # Closes above midpoint 1, 0 ) # 4. Bullish Engulfing Pattern patterns['BULLISH_ENGULFING'] = np.where( (body.shift(1) < 0) & # Previous candle bearish (body > 0) & # Current candle bullish (df['Open'] < df['Close'].shift(1)) & # Opens below previous close (df['Close'] > df['Open'].shift(1)), # Closes above previous open 1, 0 ) # 5. Morning Star Pattern patterns['MORNING_STAR'] = np.where( (body.shift(2) < 0) & # First candle bearish (abs(body.shift(1)) < abs(body.shift(2)) * 0.3) & # Second candle small (body > 0) & # Third candle bullish (df['Close'] > df['Close'].shift(2) * 0.5), # Closes above midpoint 1, 0 ) # 6. Three White Soldiers patterns['THREE_WHITE_SOLDIERS'] = np.where( (body > 0) & # Current candle bullish (body.shift(1) > 0) & # Previous candle bullish (body.shift(2) > 0) & # Two candles ago bullish (df['Close'] > df['Close'].shift(1)) & # Each closes higher (df['Close'].shift(1) > df['Close'].shift(2)), 1, 0 ) # 7. Bullish Harami patterns['BULLISH_HARAMI'] = np.where( (body.shift(1) < 0) & # Previous candle bearish (body > 0) & # Current candle bullish (df['Open'] > df['Close'].shift(1)) & # Opens inside previous body (df['Close'] < df['Open'].shift(1)), # Closes inside previous body 1, 0 ) # 8. Hanging Man patterns['HANGING_MAN'] = np.where( (lower_shadow > 2 * body_abs) & # Long lower shadow (upper_shadow <= 0.1 * body_abs) & # Minimal upper shadow (body < 0), # Bearish close 1, 0 ) # 9. Dark Cloud Cover patterns['DARK_CLOUD_COVER'] = np.where( (body.shift(1) > 0) & # Previous candle bullish (body < 0) & # Current candle bearish (df['Open'] > df['High'].shift(1)) & # Opens above previous high (df['Close'] < (df['Open'].shift(1) + df['Close'].shift(1)) / 2), # Closes below midpoint 1, 0 ) # 10. Bearish Engulfing patterns['BEARISH_ENGULFING'] = np.where( (body.shift(1) > 0) & # Previous candle bullish (body < 0) & # Current candle bearish (df['Open'] > df['Close'].shift(1)) & # Opens above previous close (df['Close'] < df['Open'].shift(1)), # Closes below previous open 1, 0 ) # 11. Evening Star patterns['EVENING_STAR'] = np.where( (body.shift(2) > 0) & # First candle bullish (abs(body.shift(1)) < abs(body.shift(2)) * 0.3) & # Second candle small (body < 0) & # Third candle bearish (df['Close'] < df['Close'].shift(2) * 0.5), # Closes below midpoint 1, 0 ) # 12. Three Black Crows patterns['THREE_BLACK_CROWS'] = np.where( (body < 0) & # Current candle bearish (body.shift(1) < 0) & # Previous candle bearish (body.shift(2) < 0) & # Two candles ago bearish (df['Close'] < df['Close'].shift(1)) & # Each closes lower (df['Close'].shift(1) < df['Close'].shift(2)), 1, 0 ) # 13. Shooting Star patterns['SHOOTING_STAR'] = np.where( (upper_shadow > 2 * body_abs) & # Long upper shadow (lower_shadow <= 0.1 * body_abs) & # Minimal lower shadow (body < 0), # Bearish close 1, 0 ) # 14. Doji Patterns patterns['DOJI'] = np.where( abs(body) <= 0.1 * (df['High'] - df['Low']), # Very small body 1, 0 ) # 15. Dragonfly Doji patterns['DRAGONFLY_DOJI'] = np.where( (abs(body) <= 0.1 * (df['High'] - df['Low'])) & # Doji body (upper_shadow <= 0.1 * (df['High'] - df['Low'])) & # Minimal upper shadow (lower_shadow >= 0.7 * (df['High'] - df['Low'])), # Long lower shadow 1, 0 ) # 16. Gravestone Doji patterns['GRAVESTONE_DOJI'] = np.where( (abs(body) <= 0.1 * (df['High'] - df['Low'])) & # Doji body (lower_shadow <= 0.1 * (df['High'] - df['Low'])) & # Minimal lower shadow (upper_shadow >= 0.7 * (df['High'] - df['Low'])), # Long upper shadow 1, 0 ) return patterns def calculate_technical_indicators(df): """Calculate technical indicators for analysis""" # RSI delta = df['Close'].diff() gain = (delta.where(delta > 0, 0)).rolling(window=14).mean() loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean() rs = gain / loss df['RSI'] = 100 - (100 / (1 + rs)) # MACD exp1 = df['Close'].ewm(span=12, adjust=False).mean() exp2 = df['Close'].ewm(span=26, adjust=False).mean() df['MACD'] = exp1 - exp2 df['MACD_Signal'] = df['MACD'].ewm(span=9, adjust=False).mean() df['MACD_Hist'] = df['MACD'] - df['MACD_Signal'] # Moving Averages df['SMA_20'] = df['Close'].rolling(window=20).mean() df['SMA_50'] = df['Close'].rolling(window=50).mean() return df