# read-only Upstox historical fetch for whoflow follow-up. Token never printed. import sys,os,json,gzip,pandas as pd;sys.path.insert(0,os.path.dirname(__file__));from up import get,q OUT='data' def cand(path): c,j=get(path) if c!=200: print('ERR',path[:80],c,str(j)[:120]); return None df=pd.DataFrame(j['data']['candles'],columns=['ts','open','high','low','close','volume','oi']); df['ts']=pd.to_datetime(df.ts.str[:19]); return df.sort_values('ts') ins=json.load(gzip.open('../data/NSE.json.gz')) bn=[x for x in ins if x.get('segment')=='NSE_FO' and x.get('name') in('BANKNIFTY',) and x.get('instrument_type')=='FUT'] for x in bn: print('BN fut',x['trading_symbol'],x['instrument_key'],x.get('lot_size')) nf=[x for x in ins if x.get('segment')=='NSE_FO' and x.get('name')=='NIFTY' and x.get('instrument_type')=='FUT'] for x in nf: print('NF fut',x['trading_symbol'],x['instrument_key'],x.get('lot_size')) for x in bn+nf: for d in ['2026-10-13','2026-10-23']: f=f"{OUT}/{x['trading_symbol'].replace(' ','_')}_{d}.parquet" if os.path.exists(f): continue df=cand(f"/v2/historical-candle/{q(x['instrument_key'],safe='')}/1minute/{d}/{d}") if df is not None and len(df): df.to_parquet(f); print(f,len(df)) # 13-Oct Nifty options: expiry 10-13 (same-day expiry) and 10-19 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',[])) if isinstance(j,dict) else j) for x in j['data']: con[(e,x['instrument_type'],float(x['strike_price']))]=x['instrument_key'] d='2026-10-13' for e in ['2026-10-13','2026-10-19']: for k in range(22200,23001,50): for t in ['CE','PE']: f=f'{OUT}/opt_{d}_{e}_{k}{t}.parquet' if os.path.exists(f): continue key=con.get((e,t,float(k))) if key is None: print('nokey',e,k,t); continue df=cand(f"/v2/expired-instruments/historical-candle/{q(key,safe='')}/1minute/{d}/{d}") if df is not None and len(df): df.to_parquet(f) print('done')