# Ex-ante daily regime labels L1..L8 (PREREG). Everything uses data known by 09:15 of the trade day. import sys; sys.path.insert(0,'/workspace/research/popular-strategies/scripts'); from plib import * import os FR='/workspace/nifty-market-games-knowledge/research/factor-regression/data' S,PC=load_spot(); days=sorted(S); D=daily_table(S,PC) # daily OHLC: official (Upstox daily) 2022+; yahoo 2021 only for warm-up off=pd.read_parquet('/workspace/research/common/data/nifty_daily_hist.parquet') u=pd.read_csv('/workspace/research/flow-forensics-2026-10/data/1m/NIFTY_day.csv'); u['date']=pd.to_datetime(u.ts.str[:10]) off=pd.concat([off,u[['date','open','high','low','close']]]).drop_duplicates('date',keep='last') y=pd.read_parquet(f'{FR}/raw_NIFTY.parquet'); y.columns=[c.lower() for c in y.columns]; y=y.reset_index().rename(columns={'Date':'date'}); y['date']=pd.to_datetime(y.date) y=y[(y.date>='2020-06-01')&(y.datelab.dma20)&(pcl>lab.dma50),'up',np.where((pcl0.4']).astype(str).replace('nan',np.nan) pr=rng_.shift(1); avg=rng_.shift(2).rolling(20).mean(); lab['pdr_ratio']=pr/avg lab['L4_PDR']=pd.cut(lab.pdr_ratio,[0,0.8,1.2,99],labels=['compressed<0.8','normal','expanded>1.2']).astype(str).replace('nan',np.nan) lab['L5_NR7']=np.where(pr<=rng_.shift(1).rolling(7).min(),'NR7','no'); lab.loc[pr.isna(),'L5_NR7']=np.nan g=(DD.open-pcl)/pcl; lab['gap_pct']=g*100 lab['L6_Gap']=pd.cut(g.abs()*100,[-1,0.25,0.75,99],labels=['flat<0.25%','medium','large>0.75%']).astype(str).replace('nan',np.nan) # VIX (daily open == prior close for India VIX) v=pd.read_parquet(f'{FR}/raw_INDIAVIX.parquet'); v=v.reset_index(); v.columns=['date']+[x.lower() for x in v.columns[1:]]; v['date']=pd.to_datetime(v.date) vu=pd.read_csv('/workspace/research/flow-forensics-2026-10/data/1m/INDIAVIX_day.csv'); vu['date']=pd.to_datetime(vu.ts.str[:10]) V=pd.concat([v[['date','close']],vu[['date','close']]]).drop_duplicates('date',keep='last').set_index('date').close.sort_index() lab['vix_open']=V.reindex(lab.index).shift(1).values # prior VIX close = VIX at 09:15 open lab['L1_VIX']=pd.cut(lab.vix_open,[0,13,16,999],labels=['<13','13-16','>16']).astype(str).replace('nan',np.nan) # expiry opt(); import lib; EX=set(lib._EXPS); td=[d for d in days] def thursday_exp(d): # weekly expiry: Thursday (Tuesday from 2025-09-01), shifted to previous trading day if holiday tgt=3 if d=wk: t-=pd.Timedelta(days=1) return t exps=set() for d in td: if d>=pd.Timestamp('2024-10-21'): continue exps.add(thursday_exp(d)) exps|={e for e in EX if e<=LAST} tds=sorted(td); pos={d:i for i,d in enumerate(tds)} def ex_lab(d): if d in exps: return 'expiry_day' i=pos[d] if i+1 known before next open prev=[]; for d in tds: s=F[F.index45%']).astype(str).replace('nan',np.nan) lab.index.name='date'; lab.to_csv('/workspace/research/popular-strategies/results/regime_labels.csv') LB=[c for c in lab.columns if c[:2] in ('L1','L2','L3','L4','L5','L6','L7','L8')] for c_ in LB: print(c_, lab[c_].value_counts(dropna=False).to_dict())