import sys,os,json,gzip,pandas as pd;sys.path.insert(0,'scripts');from up import get,q L=pd.read_csv('results/lows_list.csv') EXP={'2026-10-12':'2026-10-13','2026-10-14':'2026-10-19','2026-10-16':'2026-10-19','2026-10-19':'2026-10-27','2026-10-21':'2026-10-27','2026-10-22':'2026-10-27','2026-10-23':'2026-10-27'} con={} for e in ['2026-10-13','2026-10-19']: c,j=get('/v2/expired-instruments/option/contract',{'instrument_key':'NSE_INDEX|Nifty 50','expiry_date':e}); print(e,c,len(j.get('data',[]))) for x in j['data']: con[(e,x['instrument_type'],float(x['strike_price']))]=x['instrument_key'] ins=json.load(gzip.open('../data/NSE.json.gz')) for x in ins: if x.get('segment')=='NSE_FO' and x.get('name')=='NIFTY' and x.get('instrument_type') in('CE','PE'): e=pd.to_datetime(x['expiry'],unit='ms').strftime('%Y-%m-%d') if isinstance(x['expiry'],(int,float)) else str(x['expiry'])[:10] if e=='2026-10-27': con[(e,x['instrument_type'],float(x['strike_price']))]=x['instrument_key'] print('contracts',len(con)) os.makedirs('data/opt',exist_ok=True); rows=[] need=set() for _,r in L.iterrows(): if r.date not in EXP: continue a=round(r.low/50)*50 for k in range(a-400,a+451,50): need.add((r.date,EXP[r.date],float(k))) for d,e,k in sorted(need): for t in ['CE','PE']: f=f'data/opt/{d}_{e}_{int(k)}{t}.parquet' if os.path.exists(f): continue key=con.get((e,t,k)) if key is None: print('nokey',d,e,k,t); continue path=(f"/v2/expired-instruments/historical-candle/{q(key,safe='')}/1minute/{d}/{d}" if e<'2026-10-25' else f"/v2/historical-candle/{q(key,safe='')}/1minute/{d}/{d}") c,j=get(path) if c!=200: print('ERR',d,e,k,t,c,str(j)[:150]); continue df=pd.DataFrame(j['data']['candles'],columns=['ts','open','high','low','close','volume','oi']); df['ts']=pd.to_datetime(df.ts.str[:19]) df['date']=d;df['expiry']=e;df['strike']=k;df['type']=t; df.sort_values('ts').to_parquet(f) print('done')