# TRAIN-only selection of one exit per candidate (REAL cost, >=20 trades). Writes results/frozen_choices.json. Does not read test rows. import pandas as pd,json,datetime T=pd.read_csv('results/trades_candidates.csv',parse_dates=['date']); T=T[(T.status=='ok')&(T.split=='train')&(T.scen=='REAL')] g=T.groupby(['cand','ex']).pnl.agg(['count','mean','median']).round(2); g['win']=T.groupby(['cand','ex']).pnl.apply(lambda x:(x>0).mean()*100).round(1) print(g.to_string()); g.to_csv('results/train_grid.csv') ch={} for c,gg in g.reset_index().groupby('cand'): ok=gg[gg['count']>=20] if len(ok)==0: ok=gg; note='<20 train trades for every exit; picked best anyway (flag small sample)' else: note='' b=ok.sort_values('mean',ascending=False).iloc[0]; ch[c]=dict(ex=b.ex,train_n=int(b['count']),train_mean=float(b['mean']),note=note) ch['_fixed_at_ist']=datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=5,minutes=30))).strftime('%Y-%m-%d %H:%M IST') json.dump(ch,open('results/frozen_choices.json','w'),indent=1); print(json.dumps(ch,indent=1))