# Secondary/exploratory: time-of-day random entries, strike & 0DTE sensitivity, spot proxy by year, 60-min cap effect. import sys; sys.path.insert(0,'/workspace/research/buyer-method-audit/scripts'); from lib import *; import json S,PC=load_spot(); opt(); CH=json.load(open('results/frozen_choices.json')) X=pd.read_csv('results/signals_all.csv',parse_dates=['date']) # 1) random entries by time of day, both directions, fixed R=40, exits E0/E1/E4, REAL cost rng=np.random.default_rng(3); od=[d for d in sorted(S) if d>=pd.Timestamp('2024-10-21') and nexp(d) is not None]; rows=[] for hm in ['09:20','09:30','09:45','10:15','11:00','12:00','13:00','14:00']: m=hm2i(hm) for d in od: a=S[d]; en=a[3,m] if np.isnan(en): continue for dd in (1,-1): for ex in ('E0','E1','E4'): z=sim(a,d,m,dd,en,en-dd*40,40,ex,'REAL') if z and z['status']=='ok': rows.append(dict(hm=hm,date=d,dir=dd,ex=ex,split=split(d),gross=z['gross'],pnl=z['pnl'])) TD=pd.DataFrame(rows); TD.to_csv('results/random_by_tod_trades.csv',index=False) t=TD.groupby(['hm','ex']).agg(n=('pnl','size'),gross_incl_1pt_slip=('gross','mean'),pnl=('pnl','mean'),win=('pnl',lambda v:(v>0).mean()*100)).round(2); print(t.to_string()); t.to_csv('results/random_by_tod.csv') # with-trend vs counter-trend at 10:15 (trend = sign(close 10:14 - open 09:15)) TD['trend']=[np.sign(S[d][3,hm2i(h)-1]-S[d][0,0]) for d,h in zip(TD.date,TD.hm)] TD['with']=np.where(TD.trend==TD.dir,'with_trend','counter') w=TD[TD.hm!='09:20'].groupby(['hm','ex','with']).pnl.mean().unstack().round(2); print(w.to_string()); w.to_csv('results/random_with_vs_counter.csv') # 2) strike / 0DTE sensitivity for chosen exits rows=[] X2=X[X.date>=pd.Timestamp('2024-10-21')] for r in X2.itertuples(): ex=CH[r.cand]['ex']; a=S[r.date] for lab,so,sk in (('ATM_skip0',0,True),('ATM_0DTEok',0,False),('ITM1_skip0',1,True),('OTM1_skip0',-1,True),('ITM2_skip0',2,True)): z=sim(a,r.date,r.m,r.dir,r.entry,r.stop,r.R,ex,'REAL',strike_off=so,skip0=sk) if z and z['status']=='ok': rows.append(dict(cand=r.cand,ex=ex,inst=lab,split=split(r.date),pnl=z['pnl'],fill=z['fill'])) SI=pd.DataFrame(rows); s=SI.groupby(['cand','ex','inst','split']).pnl.agg(['count','mean']).round(2).unstack(); print(s.to_string()); s.to_csv('results/strike_sensitivity.csv') # 3) spot proxy by year (spot pts, no costs) for chosen exits and E1 SPX=pd.read_csv('results/spot_proxy.csv',parse_dates=['date']); SPX['year']=SPX.date.dt.year sp=SPX.groupby(['cand','ex','year']).spot_pts.mean().unstack().round(1); print(sp.to_string()); sp.to_csv('results/spot_proxy_by_year.csv')