"""Combined grid over the scout-winning levers -> rank by min-asset HOLD-OUT (gate minFull>=0.5)."""
import sys, itertools
from dataclasses import replace
sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/skyhook")
import skyhooklib as sk
from src.strategies.skyhook import SkyhookParams

base = SkyhookParams()
def quick(p):
    rs = {a: sk.run_asset(a, p, sk.FEE_RT) for a in sk.CERTIFIED}
    return (min(rs[a]["full"]["sharpe"] for a in rs),
            min(rs[a]["holdout"]["sharpe"] for a in rs),
            min(rs[a]["full"]["n_trades"] for a in rs),
            round(sum(rs[a]["full"]["maxdd"] for a in rs)/2,3))

rows=[]
for ptn_n,(sl,tp),vol_lo,(vlo,vhi) in itertools.product(
        (8,21,55), ((2.0,5.0),(2.5,6.0),(3.0,8.0)), (0.0,40.0,50.0), ((35.0,95.0),(25.0,95.0))):
    p=replace(base, ptn_n=ptn_n, sl_atr=sl, tp_atr=tp, vol_lo=vol_lo, vola_lo=vlo, vola_hi=vhi)
    mf,mh,mt,dd=quick(p)
    rows.append((mh,mf,mt,dd,ptn_n,sl,tp,vol_lo,vlo,vhi))
rows.sort(reverse=True)
print(f"{'minH':>6s}{'minF':>6s}{'tr':>5s}{'dd':>5s}  ptn  sl   tp  vlo vola")
for mh,mf,mt,dd,ptn_n,sl,tp,vol_lo,vlo,vhi in rows[:18]:
    gate = "PASS" if (mf>=0.5 and mh>=0.2 and mt>=20) else ""
    print(f"{mh:>+6.2f}{mf:>+6.2f}{mt:>5d}{dd*100:>4.0f}%  {ptn_n:>3d} {sl:>3.1f} {tp:>4.1f} {vol_lo:>4.0f} [{vlo:.0f},{vhi:.0f}] {gate}")
