#!/usr/bin/env python3 """Nano multi-strat: operate like a hedge fund. Combine uncorrelated premia (equity, bond, gold, commodity, trend, crypto) risk-parity-weighted + vol-targeted; show the correlation matrix (the diversification engine), combined Sharpe vs each piece alone, and how leverage scales the return. Not market-neutral — it's the diversified-premia + leverage (All-Weather/alt-risk-premia) model. The point: each stream is modest, but uncorrelated streams combine to a higher Sharpe, then leverage turns modest Sharpe into real return. Risk management = the engine's actual job.""" import math import os import sys import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from signal_sweep import load_panel, build_returns # noqa: E402 import pit_sweep # noqa: E402 TV = 0.10 def vt(r, tv=TV, win=63, cap=4.0): out = np.zeros_like(r) for t in range(win, len(r)): rv = np.std(r[t - win:t]) * math.sqrt(252) out[t] = r[t] * min(cap, tv / (rv + 1e-9)) return out def stats(r): r = r[np.isfinite(r)] if len(r) < 50 or r.std() == 0: return (float("nan"),) * 4 ann = r.mean() * 252; vol = r.std() * math.sqrt(252) eq = np.cumprod(1 + r); dd = float((eq / np.maximum.accumulate(eq) - 1).min()) return ann, vol, ann / vol, dd def run(streams, days, year, label): names = list(streams) M = np.column_stack([vt(streams[n]) for n in names]) # each vol-normalized to 10% T = M.shape[0] print(f"\n===== {label} ({T} days) =====") print(" standalone Sharpe: " + " ".join(f"{n}:{stats(streams[n])[2]:+.2f}" for n in names)) # correlation matrix of the (vol-normalized) streams C = np.corrcoef(np.nan_to_num(M).T) print(" correlation matrix:") print(" " + " ".join(f"{n[:5]:>6}" for n in names)) for i, n in enumerate(names): print(f" {n[:5]:>5} " + " ".join(f"{C[i, j]:>+6.2f}" for j in range(len(names)))) avg_corr = (C.sum() - len(names)) / (len(names) ** 2 - len(names)) # combined: equal-risk-weight then vol-target the bundle combo = vt(np.nanmean(M, axis=1)) a, v, sh, dd = stats(combo) best = max(stats(streams[n])[2] for n in names) print(f" avg pairwise corr: {avg_corr:+.2f} (low = diversification works)") print(f" COMBINED (risk-parity + vol-target 10%): Sharpe {sh:+.2f} ann {100*a:+.1f}% maxDD {100*dd:+.1f}%") print(f" vs best single stream Sharpe {best:+.2f} -> diversification lift {sh-best:+.2f}") print(f" per-year Sharpe: " + " ".join(f"{y}:{stats(combo[year == y])[2]:+.1f}" for y in sorted(set(year)) if (year == y).sum() > 100)) print(f" LEVERAGE scaling (same Sharpe {sh:+.2f}): " + " ".join(f"{lev}x->{100*a*lev:+.0f}%/yr@{int(100*v*lev)}%vol" for lev in (1, 2, 3))) return combo def main(): roots, days, close, op, inst = load_panel()[:5] lc, R, ov, intr = build_returns(close, op, inst) T, N = close.shape year = (1970 + days / 365.25).astype(int) def C(r): return roots.index(r) # trend: diversified long/short TS-momentum across all futures, vol-targeted vol63 = np.full_like(lc, np.nan) for t in range(63, T): vol63[t] = np.nanstd(R[t - 63:t], axis=0) iv = 1.0 / np.where(vol63 > 0, vol63, np.nan) tsig = np.full_like(lc, np.nan); tsig[252:] = np.sign(lc[252:] - lc[:-252]) avail = np.isfinite(close) & np.isfinite(vol63) & (vol63 > 0) & np.isfinite(tsig) w = np.where(avail, tsig * iv, 0.0); g = np.abs(w).sum(1, keepdims=True); g[g == 0] = 1; w = w / g trend = np.zeros(T); trend[1:] = np.sum(w[:-1] * R[1:], axis=1) fut = {"equity": R[:, C("ES")], "bond": R[:, C("ZN")], "gold": R[:, C("GC")], "commod": R[:, C("CL")], "trend": trend} run(fut, days, year, "TRADITIONAL 5-stream (2010-2026)") # +crypto: align BTC daily returns to futures days syms, cdays, cc, _, _ = pit_sweep.load() j = syms.index("BTCUSDT"); lcb = np.log(cc[:, j]) btc = {int(cdays[t]): (lcb[t] - lcb[t - 1]) for t in range(1, len(cdays)) if np.isfinite(lcb[t]) and np.isfinite(lcb[t - 1])} mask = np.array([int(d) in btc for d in days]) idx = np.where(mask)[0] if len(idx) > 300: sub = {k: v[idx] for k, v in fut.items()} sub["crypto"] = np.array([btc[int(days[i])] for i in idx]) run(sub, days[idx], (1970 + days[idx] / 365.25).astype(int), "6-stream +CRYPTO (2019-2026)") print("\nVERDICT: combined Sharpe > best single (diversification real) + leverage scales modest Sharpe") print("to real return = the hedge-fund operating model. Honest: this is alt-risk-premia (~0.5-1.0 live),") print("levered; NOT market-neutral (long beta falls in everything-down); leverage adds tail + financing.") if __name__ == "__main__": main()