impulse_ok = self.check_impulse_rules(waves) corrective_ok = self.check_corrective_rules(waves)
if impulse_ok: pattern_type = 'impulse_5wave' elif corrective_ok: pattern_type = 'corrective_abc' else: pattern_type = 'unclear'
# 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']
# Rule 3: Wave 4 price overlap with Wave 1? # For uptrend impulse: w1 up, w2 down, w3 up, w4 down, w5 up # Overlap means low of w4 < high of w1 if w1['direction'] == 'up': wave1_high = max(w1['start_price'], w1['end_price']) wave4_low = min(w4['start_price'], w4['end_price']) if wave4_low <= wave1_high: return False else: # downtrend impulse wave1_low = min(w1['start_price'], w1['end_price']) wave4_high = max(w4['start_price'], w4['end_price']) if wave4_high >= wave1_low: return False elliott wave python code
# 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)
return True
# Plotting plt.figure(figsize=(14, 6)) plt.plot(price_series, label='Price', color='black', alpha=0.6) impulse_ok = self
def check_corrective_rules(self, waves: List[Dict]) -> bool: """Check 3-wave corrective pattern (A,B,C).""" if len(waves) < 3: return False
swings = sorted(swings, key=lambda x: x['index']) return pd.DataFrame(swings)
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 [] """ if len(swings_df) < 2: return [] return
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
# Annotate wave numbers (first 5 waves if exist) waves = result['waves'] for i, wave in enumerate(waves[:5]): mid_idx = (wave['start_idx'] + wave['end_idx']) // 2 mid_price = (wave['start_price'] + wave['end_price']) / 2 plt.text(mid_idx, mid_price, str(i+1), fontsize=12, fontweight='bold', bbox=dict(facecolor='yellow', alpha=0.7))
waves = [] for i in range(len(swings_df) - 1): start = swings_df.iloc[i] end = swings_df.iloc[i+1] wave = { 'start_idx': start['index'], 'end_idx': end['index'], 'start_price': start['price'], 'end_price': end['price'], 'direction': 'up' if end['price'] > start['price'] else 'down', 'magnitude': abs(end['price'] - start['price']), 'start_type': start['type'], 'end_type': end['type'], } waves.append(wave) return waves