import json, math
from eng import BTC, BMA, MAJ_ON, donch
from stages import stage_series
R = json.load(open('rh_lines.json'))
def trades(ds, L, nin, nout, gated):
    st = stage_series(L) if len(L) >= 26 else [("", {})] * len(L)
    out = []; i = nin
    while i < len(L) - 1:
        d = ds[i]
        sig = L[i] > max(L[i - nin:i])
        if gated and not (d in BMA[100] and BTC.get(d, 0) > BMA[100][d] and MAJ_ON[(30, 15)].get(d, True)): sig = False
        if not sig: i += 1; continue
        ent = L[i]; w = 1; acc = 0; ex = None
        for j in range(i + 1, len(L)):
            if L[j] < min(L[j - nout:j]): ex = j; break
            if w == 1 and st[j][0] == 'HOT': acc += .5 * (L[j] / ent - 1); w = .5
        last = ex if ex is not None else len(L) - 1
        out.append((d, ds[last], round((acc + w * (L[last] / ent - 1) - 0.01) * 100, 1), 'closed' if ex is not None else 'open'))
        if ex is None: break
        i = ex + 1
    return out
for k, line in R.items():
    ds = [d for d, _ in line]; L = [v for _, v in line]
    pk = max(range(len(L)), key=lambda i: L[i])
    print(f"\n{k}: {ds[0]}..{ds[-1]} n={len(L)} peak {ds[pk]} now {L[-1]/L[pk]-1:+.0%} from peak, 30d {L[-1]/L[max(0,len(L)-31)]-1:+.0%}")
    for nin, nout in ((10, 5), (30, 15)):
        if len(L) <= nin + 1: continue
        for g in (False, True):
            t = trades(ds, L, nin, nout, g)
            print(f"  {nin}/{nout} {'gated' if g else 'no gate'}: {t}")
