"""Fast inline lever-scout for Skyhook before the agent wave. One fee (0.10% RT), both assets,
min-asset FULL & HOLD-OUT Sharpe. Maps which knobs move the honest hold-out."""
import sys
from dataclasses import replace

sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/skyhook")
import skyhooklib as sk
from src.strategies.skyhook import SkyhookParams


def quick(p: SkyhookParams) -> dict:
    rs = {a: sk.run_asset(a, p, sk.FEE_RT) for a in sk.CERTIFIED}
    mf = min(rs[a]["full"]["sharpe"] for a in rs)
    mh = min(rs[a]["holdout"]["sharpe"] for a in rs)
    mt = min(rs[a]["full"]["n_trades"] for a in rs)
    avg_dd = sum(rs[a]["full"]["maxdd"] for a in rs) / 2
    return dict(minFull=mf, minHold=mh, minTr=mt, dd=round(avg_dd, 3),
                btc=rs["BTC"]["full"]["sharpe"], eth=rs["ETH"]["full"]["sharpe"],
                btcH=rs["BTC"]["holdout"]["sharpe"], ethH=rs["ETH"]["holdout"]["sharpe"])


base = SkyhookParams()
GRID = {
    "long_only": [dict(long_only=True), dict(long_only=False)],
    "ptn_n": [dict(ptn_n=n) for n in (8, 13, 20, 34, 55)],
    "RR(sl,tp)": [dict(sl_atr=s, tp_atr=t) for s, t in
                  ((1.5, 4.0), (2.0, 5.0), (2.5, 6.0), (3.0, 4.0), (2.0, 8.0), (3.0, 9.0))],
    "exitbars": [dict(uscitalong=l, uscitashort=s) for l, s in
                 ((12, 9), (24, 18), (36, 24), (48, 36))],
    "vola_band": [dict(vola_lo=lo, vola_hi=hi) for lo, hi in
                  ((0, 100), (35, 95), (50, 100), (50, 90), (20, 80))],
    "vol_band": [dict(vol_lo=lo, vol_hi=hi) for lo, hi in
                 ((0, 100), (50, 100), (60, 100), (40, 100), (50, 80))],
}

print(f"{'param':<14s} {'value':<28s} {'minF':>6s} {'minH':>6s} {'btc/eth F':>12s} {'btc/eth H':>12s} {'tr':>5s} {'dd':>5s}")
print(f"  BASELINE: {quick(base)}")
print("-" * 100)
for fam, variants in GRID.items():
    for v in variants:
        p = replace(base, **v)
        q = quick(p)
        tag = "PASS" if (q["minFull"] >= 0.5 and q["minHold"] >= 0.2) else ""
        print(f"{fam:<14s} {str(v):<28s} {q['minFull']:>+6.2f} {q['minHold']:>+6.2f} "
              f"{q['btc']:>+5.2f}/{q['eth']:>+5.2f} {q['btcH']:>+5.2f}/{q['ethH']:>+5.2f} "
              f"{q['minTr']:>5d} {q['dd']*100:>4.0f}% {tag}")
