I have OHLC data. The candle can be either 'green' (if the close is above open) or 'red' (if the open is above the close). The format is:
open close candletype
0 542 543 GREEN
1 543 544 GREEN
2 544 545 GREEN
3 545 546 GREEN
4 546 547 GREEN
5 547 542 RED
6 542 543 GREEN
What I would like is to count the number of consecutive green or red candles for n-previous rows. Lets say I want to identify rows preceded by 3 green candles.
That the desired output would be:
open close candletype pattern
0 542 543 GREEN Toofewrows
1 543 544 GREEN Toofewrows
2 544 545 GREEN Toofewrows
3 545 546 GREEN 3-GREEN-CANDLES-IN-A-ROW
4 546 547 GREEN 3-GREEN-CANDLES-IN-A-ROW
5 547 542 RED 3-GREEN-CANDLES-IN-A-ROW
6 542 543 GREEN No pattern
I know how to get the solution by extracting the row number, applying a custom function to candletype series with that row number and looking at n previous rows within that custom function, creating a n-item list and checking for isAll('GREEN') but I WAS WONDERING IF THERE IS AN ELEGANT ONE LINER APPLY SOLUTION?