# Apply the train-fixed rules (results/fixed_rule.json) to all periods; breakdowns; random same-minute trade benchmark; plain-cross benchmark. import numpy as np,pandas as pd,sys,os,json sys.path.insert(0,os.path.dirname(__file__)); from cwlib import *; from stats import variants,split_mask DD=load(); days=sorted(DD); E=pd.read_pickle(f'{C}/results/events.pkl'); T=pd.read_pickle(f'{C}/results/trades_all.pkl'); T=T[T.status=='ok'] T=T.merge(E[['date','side','wall','K','hm','dte','wd','spot','exp']],left_on='ev',right_index=True) pos={d:i for i,d in enumerate(days)} T['tdte']=[sum(1 for x in days if d0).mean()*100,rs_lot_total=h.pnl_rs_lot.sum(),rs_lot_mean=h.pnl_rs_lot.mean(), mean_ex_srcdays=h2.pnl.mean() if per=='test' else np.nan)) allrows.append(t) S=pd.DataFrame(out); S.round(2).to_csv(f'{C}/results/rule_vs_bench.csv',index=False) A=pd.concat(allrows); A.to_csv(f'{C}/results/rule_trades.csv',index=False) def br(g,key): return g.groupby(key).agg(n=('pnl','size'),mean_pts=('pnl','mean'),win=('pnl',lambda x:(x>0).mean()*100),rs_lot=('pnl_rs_lot','sum')).round(2) A['year']=A.date.dt.year; A['month']=A.date.dt.strftime('%Y-%m') by=[] for side,g in A.groupby('side'): for key in ('year','month','gamma','wd'): b=br(g,key).reset_index().rename(columns={key:'bucket'}); b['side']=side; b['by']=key; by.append(b) BY=pd.concat(by); BY.to_csv(f'{C}/results/rule_breakdowns.csv',index=False) W=A.groupby(['side','date']).pnl_rs_lot.sum().reset_index().sort_values('pnl_rs_lot').groupby('side').head(3); W.to_csv(f'{C}/results/rule_worst_days.csv',index=False) pd.set_option('display.width',250); print(S.round(2).to_string()); print(BY.to_string()); print(W) # t-test-ish for test mean from scipy.stats import ttest_1samp,mannwhitneyu for side,g in A.groupby('side'): h=g[g.period=='test'].pnl if len(h)>2: print(side,'test mean',h.mean().round(2),'t-test p',ttest_1samp(h,0).pvalue.round(3))