"""G1 (the front's cash-rate objective, $/yr per account at 80% honoured, exact chains) for the deployed 34.8-pip cell and the 40-pip cell,
under the paper's inputs and under inputs measured here (walker.py: Jensen-corrected cost share, swap scaling, pace).  Finds the 40-pip cost share at which
G1(40) = G1(34.8).  Chains only."""
import numpy as np, time
from pa_imp import *
def cell(g,pace,wait=None,honour=0.8):
    wt=max(3,int(round(14/pace))) if wait is None else wait
    e=PA.eval_chain(0.03,g*0.03,rr=1.0,clip_up=True,clip_dn=True)
    rows=PA.funded_line_search(g,lo=0.006,hi=0.020,step=0.00025,wait=wt)
    pk=min(PA.plateau(rows),key=lambda r:(r['med_first_pay'],-r['e_W']))
    f=PA.funded_chain(pk['risk'],g*pk['risk'],PA.payout_threshold(pk['risk'],1.0,3000,0.7),rr=1.0,clip_dn=True,wait=wt)
    cash=0.7*3000*e['p_pass']*f['e_W']
    days=pace*(e['e_trades']+e['p_pass']*f['e_life'])
    return dict(g=g,pace=pace,wait=wt,rf=pk['risk'],p_pass=e['p_pass'],p_any=f['p_any'],cash_fee=cash/38,G1=(honour*cash-38)*365/days)
t0=time.time()
rows=[]
def show(tag,c): print(f"{tag:58s} gamma {c['g']:.4%} pace {c['pace']:.3f} wait {c['wait']} rf {c['rf']:.4%} pass {c['p_pass']:.4f} p_any {c['p_any']:.4f} cash/fee {c['cash_fee']:.4f}  G1 ${c['G1']:.2f}/yr  [{time.time()-t0:.0f}s]",flush=True)
SW=0.004783927673690548
show("34.8 paper inputs",cell(0.0287424637+SW,2.0250803))
show("34.8 Jensen gamma (per-trade mean share), my pace 2.108",cell(0.029642+0.004756,2.108))
show("40 paper inputs (mean-stop share, swap x l/l0, pace 2.282)",cell(0.025+SW*40/34.7917,2.2818))
show("40 beta-rule, walker inputs (gamma .02693+.005636, pace 2.348)",cell(0.026932+0.005636,2.348))
show("40 floor-rule, walker inputs (gamma .025129+.006109, pace 2.440)",cell(0.025129+0.006109,2.440))
show("40 paper gamma, walker beta pace 2.348",cell(0.025+SW*40/34.7917,2.348))
