Skip to content

Elliott Wave Python: Code

w1, w2, w3, w4, w5 = waves[:5]

# Rule 2: Wave 3 not shortest if w3['magnitude'] <= w1['magnitude'] or w3['magnitude'] <= w5['magnitude']: if w3['magnitude'] < w1['magnitude'] and w3['magnitude'] < w5['magnitude']: return False

def check_corrective_rules(self, waves: List[Dict]) -> bool: """Check 3-wave corrective pattern (A,B,C).""" if len(waves) < 3: return False

# Rule 1: Wave 2 retrace < 100% of Wave 1 if w2['magnitude'] >= w1['magnitude']: return False elliott wave python code

detector = ElliottWaveDetector(swing_window=8) result = detector.detect_elliott_waves(price_series)

def fibonacci_ratios(self, wave: Dict) -> Dict: """Calculate Fibonacci retracements/extensions for a wave.""" mag = wave['magnitude'] return { '0.382': mag * 0.382, '0.5': mag * 0.5, '0.618': mag * 0.618, '1.0': mag, '1.272': mag * 1.272, '1.618': mag * 1.618, }

def find_swing_points(self, prices: np.ndarray) -> pd.DataFrame: """Identify swing highs and lows.""" highs = argrelextrema(prices, np.greater, order=self.swing_window)[0] lows = argrelextrema(prices, np.less, order=self.swing_window)[0] w1, w2, w3, w4, w5 = waves[:5] #

def detect_elliott_waves(self, prices: np.ndarray) -> Dict: """ Main function: returns detected wave structure and validation. """ swings_df = self.find_swing_points(prices) waves = self.label_swing_waves(swings_df)

if impulse_ok: pattern_type = 'impulse_5wave' elif corrective_ok: pattern_type = 'corrective_abc' else: pattern_type = 'unclear'

price_series = np.concatenate([wave1[:100], wave2[100:200], wave3[200:300], wave4[300:400], wave5[400:500]]) = w1['magnitude'] or w3['magnitude'] &lt

A, B, C = waves[:3] # Typical rule: B retraces 0.382 to 0.886 of A retrace_ratio = B['magnitude'] / A['magnitude'] if A['magnitude'] != 0 else 0 if 0.382 <= retrace_ratio <= 0.886: # C often equals A in length (1.0 or 1.618) c_ratio = C['magnitude'] / A['magnitude'] if 0.618 <= c_ratio <= 1.618: return True return False

class ElliottWaveDetector: def (self, swing_window: int = 5): """ Parameters: ----------- swing_window : int Window size for identifying local extrema (swing highs/lows). """ self.swing_window = swing_window self.waves = []

swings = sorted(swings, key=lambda x: x['index']) return pd.DataFrame(swings)

# Add Fibonacci ratio estimates for key waves fibs = {} if len(waves) >= 3: fibs['wave3_extension'] = self.fibonacci_ratios(waves[2]) # wave 3 if len(waves) >= 5: fibs['wave5_target'] = self.fibonacci_ratios(waves[4])['1.618']

return True