Filter to scan stock if compression is for 15 bars

Length = 14;
Price = EMA(Close, Length);
 
// Keltner 
kLength = Length;
kN = 1.5;
kATR = ATR(kLength);
kUpper = Price + kN * kATR;
kLower = Price - kN * kATR;
 
// Bollinger
bbLength = Length;
bbN = 2;
bbStDevValues = StDev(Close, bbLength);
bbUpper = Price + bbN * bbStDevValues;
bbLower = Price - bbN * bbStDevValues;
 
IsBBSqueeze = bbUpper <= kUpper AND bbLower >= kLower;

Proportion = (kUpper - kLower) / (bbUpper - bbLower);
BBBreakout = Cross(1,Proportion); 
Periods = Param("BBPeriods", 14, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
bbtop=BBandTop( C, Periods, Width );
bbbot=BBandBot( C, Periods, Width );
Plot(bbtop, "", Color, Style ); 
Plot(bbbot , "", Color, Style );

sqeezcolor=ColorRGB(194,220,218);

PlotOHLC( bbtop,bbtop, bbbot,bbbot, "",IIf(IsBBSqueeze,colorYellow,colorWhite), styleCloud|styleNoRescale,  Null, Null, Null, -1 );
Plot(Close,"Close",colorGreen,styleCandle); 

Filter = BBBreakout;

AddColumn(BBBreakout, "BB Breakout", 1, colorWhite, IIf(BBBreakout==1, colorRed, colorWhite));
//set default sorting to Date/time in descending order in results window
SetSortColumns(-2);

Above is the AFL code to SCAN for compression. I want the stock to come in the scan if the compression is there for say last 15 bar. Please guide how do I filter stocks?

Thanks in advance.
Regards,
Sumit

There are multiple ways to do this. Here's one:

squeezeLength = 15;
isMultibarSqueeze = Sum(IsBBSqueeze, squeezeLength ) == squeezeLength ;

One advantage to this approach is it's easy to look for the base condition (isBBSqueeze) occurring less often than every bar in the range, for example you could look for a squeeze on 13 of the past 15 bars by changing the code to:

squeezeLength = 15;
isMultibarSqueeze = Sum(IsBBSqueeze, squeezeLength ) >= squeezeLength-2;
2 Likes

Exactly what I was looking for .. very kind of you .. thanks a ton.