BrOutBB and come back under MA200

I expert,
I’m wrote a formula which gives me a brake out of the Bollinger Bands and which returns below the Moving Average 200 (MA200).

My buying signal is a Stokastics Cross below the MA200.

I check that the Top is larger than the MA200 ( HautOK, in the formula)

I check that the Bas is smaller than the MA200 (BasOK, in the formula)

This whole part of the formula seems correct

My problem is that I cannot find the Previous Low (BasBP in the formula)

BarBP= Ref(LLVBars(C,LastValue(BarBpT)), - BarH);

I estimated that the wave leading us to the Top should be, perhaps, 1.5*the length of the bearish wave leading us to the BUY

(BarBpT= int(BarH*1.5) ;)

To find the Previous Bottom, we place ourselves on the Top and we look for the LLVBars of the Close with a maximum length of BarBpT, which gives us:

BarBp= Ref(LLVBars(C,LastValue(BarBpT)), - BarH);

I must have made a mistake in the BarBp formula because its results are not good

Where is the bug in BarBp?

Thank's expert
Yves L.


_SECTION_END();
type or paste code here
_SECTION_BEGIN("CrHL-2MA");		
/*      BrOut BB Daily et PB sous Moy-3MA(100-200)	*/
   

V20= MA(V,20) ;	
V50= IIf(IsNull(MA(V,50)),V20,MA(V,50)) ;
V100= IIf(IsNull(MA(V,100)),V50,MA(V,100)) ;
V200= IIf(IsNull(MA(V,200)),V100,MA(V,200)) ;
VolMin= Min(Min(V20,V50),Min(V100,V200)) ;
Plot(VolMin,"V-Min",colorTeal,styleOwnScale );
function BBT(Ind,P)	{ bon= BBandTop(Ind,P,2) ; return bon ; } ;
function BBB(Ind,P)	{ bon= BBandBot(Ind,Dev,2) ; return bon ; } ;
function CrBB(Ind,P)	{ bon= Cross(C,BBT(Ind,P)) ; return bon ; } ;
function EcBB(Ind,P)	{ bon= BBandTop(Ind,P,2)-BBandBot(Ind,P,2) ; return bon ; } ;
function CrBbVol(Ind,P)	{ bon= CrBB(Ind,P) AND V>=VolMin ;  return bon ; } ;
CrBbD= (CrBBVol(C,15) OR CrBBVol(C,20)) AND V>= VolMin ;	
Plot(CrBbD,"CrBB-D+Vol",colorblue,styleOwnScale );
MA200= MA(C,200) ;
CrSto1= Cross(StochK(9,3),StochD(9,3,3)) AND LLV(StochK(9,3),5)<=55 ;
LongCrBbD= BarsSince(CrBbD) ;
//Plot(LongCrBbD,"Long-CrBB",colorblue,styleOwnScale );
// Limiter la longueur du haut ???????????????
BarH= HHVBars(C,LongCrBbD+2) ;
PHaut= Ref(C,-BarH) ;
Plot(BarH,"Bar-H",colorOrange,styleOwnScale );

BarBpT= int(BarH*1.5) ;
Plot(BarBpT,"Bar-BpT",colorOrange,styleOwnScale );
BarBP= Ref(LLVBars(C,LastValue(BarBpT)), - BarH) ;
BarBp= BarBp + BarH ;
Plot(BarBP,"Bar-BP-H",colorOrange,styleOwnScale );

BarBp= BarBp + BarH ;
Plot(BarBp,"BarBp",colorOrange,styleOwnScale );
PBp= Ref( C , -BarBp) ;

BarBas= LLVBars(C, BarH) ;
PBas= LLV(C, BarH) ;

Plot(PBP,"$BP",colorOrange,styleOwnScale );
Plot(PHaut,"\n$Haut",colorOrange,styleOwnScale );
Plot(PBas,"$Bas",colorOrange,styleOwnScale );

BasOK= Ref(Ma200,-BarBas)>=Pbas ;
HautOK= Ref(Ma200,-BarH)<=PHaut ;
BpOK= Ref(Ma200,-BarBP)>=PBP ;

CrSto2= BarsSince(CrSto1) <=5 AND BarsSince(BasOK)<=5 ;
CrSto2= CrSto2 AND Ref(CrSto2,-1)==0 ;
CrSto= CrSto1 OR CrSto2 ;
CrStoOK= CrSto AND HautOK AND BasOK AND BpOK AND BarH<=88 ;   

Buy=CrStoOK ;
Plot(Buy,"Buy",colorBlack,styleOwnScale|styleThick );

_SECTION_END();