LHL Wave Under+Over+Under3MA

Hi Expert,

I'm trying to write a formula that will give me stock prices that break up above the 3-day moving average (MA100, 200, and 300) and then fall back below that same 3-day moving average.

I wrote my formula using the ZigZag method because it's easier to access all the data points.

But I'm trying to understand why there's such a big difference between the M3MA result on line 16 and the M3MA on line 33.

I thought I would get roughly the same result.

Thanks,

Yves Laliberte

_SECTION_BEGIN("LHL Wave Under+Over+Under3MA");

P= 20 ;			//	Value of ZZ
MA100= Ma(C,100) ;	MA200= Ma(C,200) ;	MA300= Ma(C,300) ;	
M3MA= ((Ma100+Ma200+MA300)	/3) ;
function LHaut(Ind,Per,Pos)	{ bon= Peakbars(Ind,Per,Pos) ; return bon ; } ;
function PHaut(Ind,P,Pos)	{ bon= Peak(Ind,P,Pos) ; return bon ; } ;
//function PHaut(Ind,Long)	{ bon= Ref( Ind,-Long) ; return bon ; } ;
function PBp(Ind,P,Pos)	{ bon= trough(Ind,P,Pos) ; return bon ; } ;
function PBas(Ind,P,Pos)	{ bon= LLV(C,PeakBars(C,P,1)) ; return bon ; } ;
function BasOK(P)	{ bon= PBas(C,P,1)<= PBas(M3MA,P,1)  ; return bon ; } ;
function HoOK(P)	{ bon= PHaut(C,P,1)>= PHaut(M3MA,P,1)  ; return bon ; } ;
function BpOK(P)	{ bon= PbP(C,P,1)<= PbP(M3MA,P,1)  ; return bon ; } ;
Plot(C,"Close",colorBlack,styleThick );

Plot(M3MA,"3MA",colorOrange,styleThick );
//P= 20 ;

CrM3MA= Cross(M3MA,C) AND BPOK(P) AND BasOK(P) ; 	// AND HoOK(P)  ;	//BUG
//CrM3MA= Cross(M3MA,C) AND BPOK(P) AND HoOK(P) AND BasOK(P)   ;

Plot( CrM3MA,"CrB3MA",colorCustom12,styleLine |styleOwnScale|styleThick );
/*
Plot( PBp(C,P,1), "$BP", colorBlue,styleOwnScale|styleDashed );
Plot( PHaut(C,P,1), "$Haut", colorBlue,styleOwnScale|styleDashed );
Plot( PBas(C,P,1), "$Bas", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(C,P,1),"$Ho", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(M3MA,P,1),"$3MA", colorBlack,styleOwnScale|styleDashed );// BUG
*/
Plot(PHaut(C,P,1),"\n$Haut", colorDarkRed,styleOwnScale|styleDashed );
Plot(PHaut(M3MA,P,1),"$3MA", colorOrange,styleDashed|styleThick );

_SECTION_END();ype or paste code here

Change the percentage p to small value that would give you a closer peak if your ticker isn't that volatile,

Line 22 I think it would be better to replace it with plotshapes

_SECTION_BEGIN("LHL Wave Under+Over+Under3MA");

P= 1 ;			//	Value of ZZ
MA100= Ma(C,100) ;	MA200= Ma(C,200) ;	MA300= Ma(C,300) ;	
M3MA= ((Ma100+Ma200+MA300)	/3) ;
function LHaut(Ind,Per,Pos)	{ bon= Peakbars(Ind,Per,Pos) ; return bon ; } ;
function PHaut(Ind,P,Pos)	{ bon= Peak(Ind,P,Pos) ; return bon ; } ;
//function PHaut(Ind,Long)	{ bon= Ref( Ind,-Long) ; return bon ; } ;
function PBp(Ind,P,Pos)	{ bon= trough(Ind,P,Pos) ; return bon ; } ;
function PBas(Ind,P,Pos)	{ bon= LLV(C,PeakBars(C,P,1)) ; return bon ; } ;
function BasOK(P)	{ bon= PBas(C,P,1)<= PBas(M3MA,P,1)  ; return bon ; } ;
function HoOK(P)	{ bon= PHaut(C,P,1)>= PHaut(M3MA,P,1)  ; return bon ; } ;
function BpOK(P)	{ bon= PbP(C,P,1)<= PbP(M3MA,P,1)  ; return bon ; } ;
Plot(C,"Close",colorBlack,styleCandle+styleThick );

Plot(M3MA,"3MA",colorOrange,styleThick );
//P= 20 ;

CrM3MA= Cross(M3MA,C) AND BPOK(P) AND BasOK(P) ; 	// AND HoOK(P)  ;	//BUG
//CrM3MA= Cross(M3MA,C) AND BPOK(P) AND HoOK(P) AND BasOK(P)   ;

Plotshapes( CrM3MA*shapeupArrow,colorCustom12,0,l );
/*
Plot( PBp(C,P,1), "$BP", colorBlue,styleOwnScale|styleDashed );
Plot( PHaut(C,P,1), "$Haut", colorBlue,styleOwnScale|styleDashed );
Plot( PBas(C,P,1), "$Bas", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(C,P,1),"$Ho", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(M3MA,P,1),"$3MA", colorBlack,styleOwnScale|styleDashed );// BUG
*/
Plot(PHaut(C,P,1),"\n$Haut", colorDarkRed,styleOwnScale|styleDashed );
Plot(PHaut(M3MA,P,1),"$3MA", colorOrange,styleDashed|styleThick );

_SECTION_END();

Below is what it looks after update

Thanks Mmha

Plotshapes might be a good idea.

Did you read the first few lines of my post?

I'm trying to write a formula that will give me stock prices that break up above the 3-day moving average (MA100, 200, and 300) and then fall back below that same 3-day moving average.

And changing the value of P from 20 to 1 won't give me an upward wave that will then fall below the M3MA.

Look at your chart; the two values ​​for 3MA and $3MA are very different.

Normally, these two values ​​should be almost identical, but on your chart, they double.

Why?

That's the bug.

$3MA from the code you posted only return the peak of 3MA with 20 percent used this would mostly give you a major peak of 3MA

It would be very clear if you also plot zigzag for same and comment other plots

P= 20 ;			//	Value of ZZ
MA100= Ma(C,100) ;	MA200= Ma(C,200) ;	MA300= Ma(C,300) ;	
M3MA= ((Ma100+Ma200+MA300)	/3) ;
function LHaut(Ind,Per,Pos)	{ bon= Peakbars(Ind,Per,Pos) ; return bon ; } ;
function PHaut(Ind,P,Pos)	{ bon= Peak(Ind,P,Pos) ; return bon ; } ;
//function PHaut(Ind,Long)	{ bon= Ref( Ind,-Long) ; return bon ; } ;
function PBp(Ind,P,Pos)	{ bon= trough(Ind,P,Pos) ; return bon ; } ;
function PBas(Ind,P,Pos)	{ bon= LLV(C,PeakBars(C,P,1)) ; return bon ; } ;
function BasOK(P)	{ bon= PBas(C,P,1)<= PBas(M3MA,P,1)  ; return bon ; } ;
function HoOK(P)	{ bon= PHaut(C,P,1)>= PHaut(M3MA,P,1)  ; return bon ; } ;
function BpOK(P)	{ bon= PbP(C,P,1)<= PbP(M3MA,P,1)  ; return bon ; } ;
//Plot(C,"Close",colorBlack,styleCandle+styleThick );

Plot(M3MA,"3MA",colorOrange,styleThick );
//P= 20 ;

CrM3MA= Cross(M3MA,C) AND BPOK(P) AND BasOK(P) ; 	// AND HoOK(P)  ;	//BUG
//CrM3MA= Cross(M3MA,C) AND BPOK(P) AND HoOK(P) AND BasOK(P)   ;

Plotshapes( CrM3MA*shapeupArrow,colorCustom12,0,l );
/*
Plot( PBp(C,P,1), "$BP", colorBlue,styleOwnScale|styleDashed );
Plot( PHaut(C,P,1), "$Haut", colorBlue,styleOwnScale|styleDashed );
Plot( PBas(C,P,1), "$Bas", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(C,P,1),"$Ho", colorBlack,styleOwnScale|styleDashed );

Plot(PHaut(M3MA,P,1),"$3MA", colorBlack,styleOwnScale|styleDashed );// BUG
*/
//Plot(PHaut(C,P,1),"\n$Haut", colorDarkRed,styleOwnScale|styleDashed );
Plot((PHaut(M3MA,P,1)),"$3MA", colorOrange,styleDashed|styleThick );
Plot (Zig(m3ma,20),"zig",colorWhite);
PlotShapes(shapeDownArrow*(m3ma==peak(M3MA,P,1)),colorAqua,0,h);

_SECTION_END();