Question about Bollinger Bands

The following formula gives us the Brake Out and Brake Down of the Bollinger Bands
I would like to be able to count (the number of times) the Brake Downs between two Brake Out
THANKS
Yves Laliberté

_SECTION_BEGIN("CrBBT et BBB");		
/*   Test pour BB	*/

function BBT(Ind,P)	{ bon= BBandTop(Ind,P,2) ; return bon ; } ;
function BBB(Ind,P)	{ bon= BBandBot(Ind,P,2) ; return bon ; } ;
function CrBBT(Ind,P)	{ bon= Cross(C,BBT(Ind,P)) ; return bon ; } ;
function CrBBB(Ind,P)	{ bon= Cross(BBB(Ind,P),C) ; return bon ; } ;
function EcBB(Ind,P)	{ bon= BBandTop(Ind,P,2)-BBandBot(Ind,P,2) ; return bon ; } ;


Plot( CrBBT(C,15),"CrBBT",colorBlue,styleOwnScale ); //Brake Out

Plot( CrBBB(C,15),"CrBBB",colorred,styleOwnScale );	//Brake Down

_SECTION_END();type or paste code here

Hello @yveslaliberte.
I think the function you are going to get most out of for this task is the built in SumSince() function
My approach would be to define both the condition for bollanger band breakout and the bollanger band breakdown as a boolean (i.e returns a 1 if true) and then count the number of times the condition was met for a certain lookback period. My assumption is that you'd want to use volatility expansion as a marker of when to start to count the breakouts or breakdowns (but thats for you to play with of course) and I reckon you could probably use bollanger band width to this effect. I haven't really backtested it but I think you could probably get close by having a play with;

_SECTION_BEGIN("Bollinger Bandwidth");
a1=Param("period",20,10,50,1,0);
a2=Param("Standard Deviation",2,1.5,2.5,0.1,0);
a3=BBandTop(Close, a1, a2);
a4=BBandBot(Close, a1, a2);
a5=a3-a4;
a6=MA(Close,a1);
a7=a5/a6*100;
Plot( a7,"Bollinger Bandwidth",ParamColor( "color", colorCycle ), ParamStyle("Style")  );
_SECTION_END();

_SECTION_BEGIN("BBand breakout and Breakdown count");
p=Param("Lookbackl Period",20,1,50);
bbBreakout = (H>BBandTop(C,P,2)) AND (Ref(H,-1)<BBandTop(C,P,2));
bbBreakdown = (L<BBandBot(C,P,2)) AND (Ref(L,-1)>BBandBot(C,P,2));
BbandExpansion =  a7 > Ref(a7,-1) AND (Ref(a7,-1) < Ref(a7,-2);

myBreakoutCount = SumSince(BbandExpansion, bbBreakout);
myBreakdownCount = SumSince(BbandExpansion, bbBreakdown);
_SECTION_END();


I think I prefer your use of the 'cross' function to my messing around with ref(H,-1), but you get the picture. I note also that you are using bon as your boolean whether the close is crossing the top or the bottom band. Personally I'd probably separate the 2, but whatever works for you.
Also, be clear whether you want to define a break of the band to mean that the wick penetrated, or the body, as that will change whether you use the close value, or the high (for top, and low for the bottom)
Good luck!