Files
TR_Agent/PaPP v2/Analisi/scripts/plot_trajectories.py
T
Pietro Giacobazzi 19c2e0ac72 PaPP v2: cartella Analisi (script, risultati, metodologia incroci)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-06-17 07:40:41 +00:00

44 lines
1.7 KiB
Python

"""Grafico delle traiettorie di extra-rendimento (giorno per giorno) dopo l'incrocio.
Disegna l'extra-rendimento cumulato (cret_1..20 - baseline stesso regime) per un
insieme di pattern (default: i piu' consistenti legati alla MA121/MA365).
Uso:
python plot_trajectories.py [SYMBOL] [YEAR_MIN]
Output:
../results/trajectories_<SYM>_<YEAR>.png
"""
import os, sys, numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from _common import load_pair, add_regime, regime_edges, HERE
SYM = sys.argv[1] if len(sys.argv) > 1 else "EURUSD"
YEAR = int(sys.argv[2]) if len(sys.argv) > 2 else 1999
RESULTS = os.path.normpath(os.path.join(HERE, "..", "results"))
HZ = list(range(1, 21))
cr, ba = load_pair(SYM, YEAR)
edges = regime_edges(ba)
cr = add_regime(cr, edges); ba = add_regime(ba, edges)
bmean = {h: ba.groupby("regime")[f"cret_{h}"].mean() for h in HZ}
bglob = {h: ba[f"cret_{h}"].mean() for h in HZ}
sel = [("MA121xMA7", 1), ("MEDxMA121", -1), ("PRICExMA121", -1), ("MA121xMA3", 1), ("MA365xMA7", 1)]
plt.figure(figsize=(11, 6))
for pair, d in sel:
g = cr[(cr.pair == pair) & (cr.dir == d)]
if len(g) < 20:
continue
exc = [(g[f"cret_{h}"] - g["regime"].map(bmean[h]).fillna(bglob[h])).mean() for h in HZ]
plt.plot(HZ, exc, marker="o", ms=3, label=f"{pair} dir={d:+d} (n={len(g)})")
plt.axhline(0, color="k", lw=.8)
plt.title(f"{SYM} ({YEAR}+) - extra-rendimento medio vs baseline (stesso regime)")
plt.xlabel("giorni dopo l'incrocio"); plt.ylabel("extra-rendimento cumulato (%)")
plt.legend(); plt.grid(alpha=.3); plt.tight_layout()
os.makedirs(RESULTS, exist_ok=True)
outp = os.path.join(RESULTS, f"trajectories_{SYM}_{YEAR}.png")
plt.savefig(outp, dpi=130)
print("salvato:", outp)