import sys;sys.path.insert(0,'../scripts');from load import m1,SESS import pandas as pd,numpy as np N=m1('NIFTY'); rows=[] for d in SESS: g=N[(N.date==d)&(N.hm<='15:14')].reset_index(drop=True) lo=g.low.values; hi=g.high.values # day low i=int(np.nanargmin(lo)); pk=int(np.nanargmax(hi[:i+1])) rows.append(dict(date=d,kind='day low',hm=g.hm[i],low=lo[i],peak_hm=g.hm[pk],peak=hi[pk],fall=hi[pk]-lo[i])) # zigzag swing lows: fall >=150 from running peak, then reversal >=60 peak=hi[0];pki=0;cand=None for j in range(len(g)): if cand is None: if hi[j]>peak: peak,pki=hi[j],j if peak-lo[j]>=150: cand=j else: if lo[j]=60: rows.append(dict(date=d,kind='swing low',hm=g.hm[cand],low=lo[cand],peak_hm=g.hm[pki],peak=peak,fall=peak-lo[cand])) peak,pki=hi[j],j; cand=None # restart peak tracking from here # (lows still 'cand' at end of day are the day low or unconfirmed) L=pd.DataFrame(rows).drop_duplicates(['date','hm']).sort_values(['date','hm']) L['kind']=L.groupby(['date','hm']).kind.transform('first') print(L.to_string()); L.to_csv('results/lows_list.csv',index=False)