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
def check_impulse_rules(self, waves: List[Dict]) -> bool: """ Validate Elliott Wave impulse pattern (5 waves: 1,2,3,4,5). Rules: 1. Wave 2 cannot retrace more than 100% of Wave 1. 2. Wave 3 is never the shortest (in magnitude). 3. Wave 4 does not overlap Wave 1 (in price). 4. Wave 3 often shows 1.618 extension of Wave 1. """ if len(waves) < 5: return False
if impulse_ok: pattern_type = 'impulse_5wave' elif corrective_ok: pattern_type = 'corrective_abc' else: pattern_type = 'unclear'
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, } elliott wave python code
# 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
swings = [] for idx in highs: swings.append({'index': idx, 'price': prices[idx], 'type': 'high'}) for idx in lows: swings.append({'index': idx, 'price': prices[idx], 'type': 'low'})
def label_swing_waves(self, swings_df: pd.DataFrame) -> List[Dict]: """ Convert alternating swing points into wave segments. Returns list of waves with direction, length, and ratio info. """ if len(swings_df) < 2: return [] A, B, C = waves[:3] # Typical rule: B retraces 0
# Generate synthetic price data (uptrend with pullbacks) np.random.seed(42) t = np.linspace(0, 100, 500) # Simulated Elliott wave: 5 waves up wave1 = 100 + 10 * np.sin(t * 0.05) + 0.1 * t wave2 = wave1 - 4 * np.sin(t * 0.1) wave3 = wave2 + 15 * np.sin(t * 0.03) wave4 = wave3 - 6 * np.sin(t * 0.08) wave5 = wave4 + 8 * np.sin(t * 0.02)
w1, w2, w3, w4, w5 = waves[:5]
price_series = np.concatenate([wave1[:100], wave2[100:200], wave3[200:300], wave4[300:400], wave5[400:500]]) Wave 4 does not overlap Wave 1 (in price)
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 = []
print("=== Elliott Wave Analysis ===") print(f"Pattern detected: {result['pattern']}") print(f"Valid structure: {result['valid']}") if result['fibonacci_levels']: print(f"Fibonacci projections: {result['fibonacci_levels']}")
# Plotting plt.figure(figsize=(14, 6)) plt.plot(price_series, label='Price', color='black', alpha=0.6)
return { 'pattern': pattern_type, 'waves': waves, 'valid': impulse_ok or corrective_ok, 'fibonacci_levels': fibs, 'swing_points': swings_df } Example usage & visualization ------------------------------- if name == " main ": import matplotlib.pyplot as plt