"""Calendar days per trade by ratio, assessment cells (target clipped, what section 26 interpolates)
against funded cells (target never clipped, what a funded trade is), from the cached replays.
Then the pace each gives a 40-pip and a 45-pip ratio-one stop, and the payout clock round(14 / d)."""
import json
from pathlib import Path

import numpy as np

from s03_broker_cache import parse, CACHE

RR = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 2.5, 3.0]
RA = [0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04, 0.045]
RF = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04]
STOP0 = 34.791728647093755
out = {}
for kind, risks in (("eval", RA), ("funded", RF)):
    pace = []
    for rr in RR:
        per = []
        for r in risks:
            res = json.loads((CACHE / f"{kind}_rr{rr:g}_ra{100*r:g}_rf{100*r:g}.json").read_text())
            vals = []
            for s in res["coin"].values():
                ev, fu = parse(s["lines"])
                acc = ev if kind == "eval" else fu
                n = sum(a["trades"] for a in acc)
                if n:
                    vals.append(sum(a["secs"] for a in acc) / 86400 / n)
            per.append(np.mean(vals))
        pace.append(float(np.nanmean(per)))
    out[kind] = pace
    print(kind, " ".join(f"RR{rr:g}:{p:.3f}" for rr, p in zip(RR, pace)))
for l in (34.79, 40.0, 45.0):
    x = (l / STOP0) ** 2
    row = []
    for kind in ("eval", "funded"):
        d = float(np.interp(x, RR, out[kind]))
        row.append(f"{kind} {d:.3f} d/trade, clock round {max(3, round(14 / d))} (14/d = {14 / d:.2f})")
    print(f"stop {l:5.2f}: " + "; ".join(row))
json.dump(out, open("/tmp/arena-prime-audit/candidate-1/out_s03b.json", "w"))
