Files
PythagorasGoal/scripts/research/xsec/runs/XR03.py
T
Adriano Dal Pastro 9612560479 research(xsec): sweep cross-sectional su Hyperliquid (43 script/257 config) + verifica avversariale
Nuova harness condivisa xslib.py (panel HL certificato, score per-asset causale, book
long-k/short-k vol-targeted leak-free) + 43 script in runs/ su 11 famiglie (MOM/REV/VOL/
DIST/LIQ/VAL/STRUCT/UNIV). Scoring = earns_slot (full>0 AND hold-out>0 AND marginal ADDS
al portafoglio live AND corr XS01<0.6, con jackknife drop-one-month).

Find: 42/257 config earns_slot=True, ma TUTTE con corr TP01 -0.2..-0.4 e PnL ~solo 2025.
Verify (verify_survivors.py, 3 scettici deterministici):
 - S1 redundancy: cluster low-vol = UNA scommessa (XV01=XU02=1.00, XV02/XV03 r 0.44-0.67);
   XM09/XL02/XS06b/XR02 distinti (corr media off-diag +0.20).
 - S2 short-beta: cluster low-vol carica 0.44-0.70 su short-market -> NON market-neutral,
   e' un tilt short-alt-beta di regime. XM09(0.08)/XR02(-0.21) NON short-beta.
 - S3 per-anno: cluster low-vol decade (XV01/XU02 2026 -0.09); XL02 morto (2025 -0.14,
   2026 -0.43); XM09 (0.82/0.50/0.74) e XR02 (0.84/0.40/2.68) positivi in tutti e 3 gli anni.

Esito: nessuna sleeve nuova. Cluster low-vol RIGETTATO (regime-bet), XL02 RIGETTATO (overfit).
2 LEAD genuini (XM09 trend-gated x-sec momentum, XR02 reversal vol-gated) -> forward-monitor,
non deployabili (panel 2.5y regime unico + STAT-MODE esecuzione). Portafoglio live invariato.

Incluso anche options_vrp_managed.py (A/B VRP01 hold-to-expiry vs gestione attiva del doc
credit-spread): la gestione attiva DISTRUGGE l'edge (combo FULL managed Sh -1.29 vs HtE +0.96,
il delta-exit taglia i vincenti) -> scartata, VRP01 resta hold-to-expiry.

Diari: 2026-06-20-xsec-strategies-sweep.md, 2026-06-20-vrp-active-management.md.
gitignore: data/paper_portfolio/ (stato runtime paper) + scripts/research/xsec/runs/out/ (output rigenerabile).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 21:36:57 +00:00

93 lines
3.0 KiB
Python

"""XR03 — Residual Short-Term Reversal
Score = -(sum of residual_return over last L days)
Idiosyncratic reversal: removes market beta before computing the short-term reversal signal.
L in {3, 5}; beta window fixed at 60d.
Grid (<= 5 study_xs calls):
1. L=3, majors, H=5, k=5, long_short=True
2. L=5, majors, H=5, k=5, long_short=True
3. L=3, all, H=5, k=5, long_short=True
4. L=5, all, H=5, k=5, long_short=True
5. Best-L from above, all, H=10, k=5, long_short=True
"""
import sys
sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/xsec")
import xslib as xs
import numpy as np
BETA_WIN = 60 # rolling beta window for residual computation
def score_xr03(P, L):
"""Causal residual reversal score.
residual[i] = ret[i] - beta_rolling[i] * market_ret[i]
score[i] = -sum(residual[i-L+1 .. i])
HIGHER score = more negative recent idio returns = long (reversal)
"""
res = xs.residual_return(P.ret, BETA_WIN) # (n_days, n_assets)
# rolling sum of last L residuals (causal: sum of rows [i-L+1..i])
res_sum = xs.roll_mean(res, L) * L # roll_mean * L = roll_sum
# reversal: negative of cumulative idio return
score = -res_sum
return score
# --- Grid ---
results = []
# 1. L=3, majors
r1 = xs.study_xs("XR03_L3_maj", lambda P: score_xr03(P, 3),
universe="majors", H=5, k=5, long_short=True)
results.append(r1)
print("=== XR03 L3 majors H5 ===")
print(xs.fmt(r1))
# 2. L=5, majors
r2 = xs.study_xs("XR03_L5_maj", lambda P: score_xr03(P, 5),
universe="majors", H=5, k=5, long_short=True)
results.append(r2)
print("=== XR03 L5 majors H5 ===")
print(xs.fmt(r2))
# 3. L=3, all
r3 = xs.study_xs("XR03_L3_all", lambda P: score_xr03(P, 3),
universe="all", H=5, k=5, long_short=True)
results.append(r3)
print("=== XR03 L3 all H5 ===")
print(xs.fmt(r3))
# 4. L=5, all
r4 = xs.study_xs("XR03_L5_all", lambda P: score_xr03(P, 5),
universe="all", H=5, k=5, long_short=True)
results.append(r4)
print("=== XR03 L5 all H5 ===")
print(xs.fmt(r4))
# 5. Best-L (by hold-out sharpe) with H=10
# pick best L from runs 1-4
best_so_far = max(results, key=lambda r: r["holdout"]["sharpe"])
best_L = 3 if "L3" in best_so_far["name"] else 5
best_univ = "all" if "all" in best_so_far["name"] else "majors"
r5 = xs.study_xs(f"XR03_L{best_L}_{best_univ}_H10",
lambda P: score_xr03(P, best_L),
universe=best_univ, H=10, k=5, long_short=True)
results.append(r5)
print(f"=== XR03 L{best_L} {best_univ} H10 ===")
print(xs.fmt(r5))
# --- Pick best overall by marginal robustness then hold-out ---
def score_key(r):
earns = r.get("earns_slot", False)
oos = r.get("marginal", {}).get("robust_oos", False)
verdict = r.get("marginal", {}).get("verdict", "")
adds = verdict == "ADDS"
hold = r["holdout"]["sharpe"]
full = r["full"]["sharpe"]
return (int(earns), int(adds), int(oos), hold, full)
best = max(results, key=score_key)
print("\n========== BEST CONFIG ==========")
print(xs.fmt(best))
print("JSON:", xs.as_json(best))