Moving averages slopes

Dear Tomaz,
I'm trying to get moving average slopes used that code, but I can't not read zero slope of moving average.


MOV_val = MA( Close, 20 );

MOV_clr = IIf( MOV_val > Ref(MOV_val,-1), colorGreen,  IIf( MOV_val < Ref(MOV_val,-1), colorRed,  IIf( MOV_val == Ref(MOV_val,-1),  colorBlue,  colorWhite)) )  ; 

Plot( MOV_val, "MOV", MOV_clr ,  styleLine , Null, Null, 0, 0, 2 );


MOV_val = MA( Close, 20 );
// the following 2 lines are added to verify colors... Comment them out
Mov_Val[BarCount - 3] = Mov_Val[BarCount - 4];
Mov_Val[BarCount - 2] = Mov_Val[BarCount - 3];

MOV_clr = IIf( MOV_val > Ref( MOV_val, -1 ), colorGreen,  IIf( MOV_val < Ref( MOV_val, -1 ), colorRed,  colorWhite ) )  ;
Plot( MOV_val, "MOV", MOV_clr, styleLine, Null, Null, 0, 0, 2 );

how to do that over all bars, not over last bars?

@ahm.montaser, in the above example, these 2 lines were added only to show that the color white is displayed whenever 2 (or more) consecutive values are the same: something that in a moving average is improbable.

I suggest you ask Chat-GPT:

"When calculating a 20-period simple moving average (SMA) over a stock price series, what is the probability that two consecutive SMA values will be identical? Under what conditions will this occur?"

3 Likes