I am trying to dynamically adjust the lookback period of a pandas dataframe to run logic on different lengths of prices. As an easy example take an MA cross.
date Prices Diff signal
20150101 8.5 -1.5 FALSE
20150101 11.5 0.3 TRUE
20150102 14.5 4.5 FALSE
20150103 16.67 3.66 FALSE
20150104 18 2 FALSE
20150105 18.5 0.5 FALSE
20150106 18.17 -2.17 TRUE
20150107 17 -3 FALSE
Diff is the difference between 2 moving averages and signal identifies a cross with true/false. Numbers are arbitrary, just examples. There could be 10, 50, 100, etc rows between each signal.
Now I would like to run a regression on the prices for whatever length exists between signals, so at row 6 I would want prices[-4:], and row 8 I would want prices[-1:].
Edit: I have actually solved this problem, but my solution is not very elegant, so if anyone has suggestions let me know. Thank you