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>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""XS07b — Trend-quality (R^2) ranking.
|
||||
|
||||
IDEA: Score each asset by the R^2 of a linear fit of log-price over the last W bars,
|
||||
signed by the direction of the trend (positive slope = long candidate).
|
||||
Score = sign(slope) * R^2
|
||||
|
||||
High R^2 + upward slope -> strong smooth uptrend -> long.
|
||||
High R^2 + downward slope -> strong smooth downtrend -> short.
|
||||
Low R^2 -> noisy / not trending -> near-zero score.
|
||||
|
||||
W=60 canonical, but we try W=30 and W=90 too. The score is CAUSAL: for row i,
|
||||
we fit on close[i-W+1 .. i] (inclusive), using only past data.
|
||||
"""
|
||||
import sys
|
||||
sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/xsec")
|
||||
import xslib as xs
|
||||
import numpy as np
|
||||
|
||||
|
||||
def r2_trend_score(close, W=60):
|
||||
"""
|
||||
Per-asset, rolling R^2 of linear fit on log(close), signed by slope direction.
|
||||
Returns (n_days x n_assets) matrix. Causal: row i uses close[i-W+1..i].
|
||||
"""
|
||||
n, A = close.shape
|
||||
out = np.full((n, A), np.nan)
|
||||
x = np.arange(W, dtype=float)
|
||||
x -= x.mean() # center x for numerical stability
|
||||
xss = (x ** 2).sum()
|
||||
for i in range(W - 1, n):
|
||||
log_p = np.log(close[i - W + 1: i + 1, :]) # (W, A)
|
||||
# For each asset: fit log_p = a + b*x
|
||||
# b = cov(x, log_p) / var(x)
|
||||
mean_y = log_p.mean(axis=0) # (A,)
|
||||
b = (x[:, None] * (log_p - mean_y)).sum(axis=0) / xss # (A,)
|
||||
y_hat = x[:, None] * b + mean_y # (W, A)
|
||||
ss_res = ((log_p - y_hat) ** 2).sum(axis=0)
|
||||
ss_tot = ((log_p - mean_y) ** 2).sum(axis=0)
|
||||
r2 = np.where(ss_tot > 0, 1.0 - ss_res / ss_tot, 0.0)
|
||||
# Score = sign(slope) * R^2. Ranges in [-1, 1].
|
||||
out[i] = np.sign(b) * r2
|
||||
return out
|
||||
|
||||
|
||||
def make_score_fn(W=60):
|
||||
def score_fn(P):
|
||||
return r2_trend_score(P.close, W=W)
|
||||
return score_fn
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Grid: (W, universe, H, k, long_short)
|
||||
# Keep <= 5 backtests total
|
||||
configs = [
|
||||
# Canonical: W=60, all assets, H=10, k=5, LS
|
||||
dict(name="XS07b_W60_all_H10_k5_LS", W=60, universe="all", H=10, k=5, long_short=True),
|
||||
# Shorter trend window
|
||||
dict(name="XS07b_W30_all_H10_k5_LS", W=30, universe="all", H=10, k=5, long_short=True),
|
||||
# Longer trend window
|
||||
dict(name="XS07b_W90_all_H10_k5_LS", W=90, universe="all", H=10, k=5, long_short=True),
|
||||
# Majors only (less noisy universe)
|
||||
dict(name="XS07b_W60_maj_H10_k5_LS", W=60, universe="majors", H=10, k=5, long_short=True),
|
||||
# Long-only variant (majors)
|
||||
dict(name="XS07b_W60_maj_H10_k3_LO", W=60, universe="majors", H=10, k=3, long_short=False),
|
||||
]
|
||||
|
||||
best_rep = None
|
||||
best_key = (-999, -999, False) # (earns_slot, hold_sharpe, robust_oos)
|
||||
|
||||
for cfg in configs:
|
||||
W = cfg.pop("W")
|
||||
name = cfg.pop("name")
|
||||
rep = xs.study_xs(name, make_score_fn(W=W), **cfg)
|
||||
print(xs.fmt(rep))
|
||||
hold_sh = rep["holdout"].get("sharpe", -999)
|
||||
earns = int(rep["earns_slot"])
|
||||
robust = int(rep["marginal"].get("robust_oos", False))
|
||||
key = (earns, robust, hold_sh)
|
||||
if key > best_key:
|
||||
best_key = key
|
||||
best_rep = rep
|
||||
|
||||
print("\n=== BEST CONFIG ===")
|
||||
print(xs.fmt(best_rep))
|
||||
print("JSON:", xs.as_json(best_rep))
|
||||
Reference in New Issue
Block a user