import pandas as pd import numpy as np import matplotlib.pyplot as plt np.random.seed(42) # Générer 100 valeurs simulant un prix réel avec fluctuations t = np.arange(100) trend = np.sin(t/10) * 2 # tendance ondulante noise = np.random.randn(100) * 0.5 # bruit prices = 50 + trend + noise # prix centré autour de 50 df = pd.DataFrame({"price": prices}) # Rolling simple sur 5 périodes df["rolling5"] = df["price"].rolling(5).mean() # EMA plus réactive (span=5) # EMA5 standard df["ema5"] = df["price"].ewm(span=5, adjust=False).mean() # EMA5 “lissée” avec double application df["ema5_smooth"] = df["price"].ewm(span=5, adjust=False).mean().ewm(span=5, adjust=False).mean() # EMA plus lissée (span=20) df["ema20"] = df["price"].ewm(span=20, adjust=False).mean() # Plot plt.figure(figsize=(12,6)) plt.plot(df["price"], label="Prix", color='black', alpha=0.6) plt.plot(df["rolling5"], label="Rolling 5", linestyle="--", color='blue') plt.plot(df["ema5"], label="EMA 5", color='red') plt.plot(df["ema5_smooth"], label="EMA 5S", color='blue') plt.plot(df["ema20"], label="EMA 20", color='green') plt.title("Rolling vs Exponential Moving Average (prix réaliste)") plt.xlabel("Période") plt.ylabel("Prix") plt.legend() plt.grid(True) plt.show()