# contribution during the main intraday fall (day-high minute -> subsequent day-low minute) and the recovery after the low, real minutes only (<=15:14) import pandas as pd,numpy as np,sys; sys.path.insert(0,'scripts'); from load import * W=pd.read_csv('data/implied_weights.csv',index_col=0).pts_per_rupee N=m1('NIFTY'); M={s:m1(s) for s in STK}; out=[] for d in SESS: n=N[(N.date==d)&(N.hm<='15:14')].reset_index(drop=True) hi=n.high.idxmax(); lo=n.loc[hi:].low.idxmin() # biggest fall after the high # also largest peak-to-trough anywhere best=(0,0,0) for i in range(len(n)): j=n.loc[i:].low.idxmin(); dd=n.high[i]-n.low[j] if dd>best[0]: best=(dd,i,j) _,i,j=best; e=len(n)-1 for leg,a,b in [('fall',i,j),('rebound_after_low',j,e)]: cs={} for s in STK: m=M[s]; m=m[(m.date==d)&(m.hm<='15:14')].reset_index(drop=True) cs[s]=W[s]*(m.close[b]-m.close[a]) cs=pd.Series(cs) out.append(dict(date=d,leg=leg,start=n.hm[a],end=n.hm[b],nifty_close_pts=n.close[b]-n.close[a],peak_to_trough=n.low[b]-n.high[a] if leg=='fall' else n.close[b]-n.low[a], sum_components=cs.sum(),top5=', '.join(f'{k} {v:+.1f}' for k,v in cs.sort_values(ascending=(leg=='fall')).head(5).items()), banks_fin=cs[['HDFCBANK','ICICIBANK','AXISBANK','KOTAKBANK','SBIN','BAJFINANCE','BAJAJFINSV','SHRIRAMFIN','JIOFIN','HDFCLIFE','SBILIFE','BSE']].sum(), reliance=cs['RELIANCE'],it=cs[['INFY','TCS','HCLTECH','TECHM']].sum())) o=pd.DataFrame(out); o.to_csv('results/swing_legs.csv',index=False); pd.set_option('display.width',250); print(o.round(1).to_string())