Error in iMAOnArray() LWMA Algorithm - page 2

 

Following idea to stop 'going backwards too far' by adjusting 'periods to use' ...

      int RangePeriodAct = MathMin(RangePeriod, Bars-ix);
      int ixHH = iHighest(NULL, TF, MODE_HIGH, RangePeriodAct, ix);
      double HiHi = iHigh(NULL, TF, ixHH);

(just copied from code)

 

so do you mean to use RangePeriodAct for the total parameter in iMAOnArray() ?

 

Success at last !!

I was right, the problem was a fact of the Drawing Buffer getting asigned the size of the full chart, so it leaves many unfilled buffer indexes, these were getting used by the EMA calculation and as far as i could tell there is no way to prevent iMAOnArray from calculating the EMA starting from the very last index, therefore EMA has huge erroneous values created from EMPTY_VALUE before it starts using the correct values. The large error values are then incorporated in the EMA calculation for subsequent bars.

I changed my code to use regular arrays resized to limit. Therefore there are no empty array indexes.

Lesson learned: Do not use drawing buffers in iMAOnArray while using limited chart hostory especially not if iMAOnArray is to use the EMA averaging algorithm.

As you can see from these two images there are no erroneous beginnings to the iMAOnArray lines (the dotted ones) when regular arrays are used for counting instead of drawing buffers so I was wrong in my original theory, the error is not within the averageing algorithm itself, it is caused by the implementation of iMAOnArray and its behaviour with drawing buffers that have empty indexes, there may be a way to fix this another way so to be able to use drawing buffers without errors, but I was unable to find it.

 
Ian Venner:

Success at last !!

I was right, the problem was a fact of the Drawing Buffer getting asigned the size of the full chart, so it leaves many unfilled buffer indexes, these were getting used by the EMA calculation and as far as i could tell there is no way to prevent iMAOnArray from calculating the EMA starting from the very last index, therefore EMA has huge erroneous values created from EMPTY_VALUE before it starts using the correct values. The large error values are then incorporated in the EMA calculation for subsequent bars.

I changed my code to use regular arrays resized to limit. Therefore there are no empty array indexes.

Lesson learned: Do not use drawing buffers in iMAOnArray while using limited chart hostory especially not if iMAOnArray is to use the EMA averaging algorithm.

As you can see from these two images there are no erroneous beginnings to the iMAOnArray lines (the dotted ones) when regular arrays are used for counting instead of drawing buffers so I was wrong in my original theory, the error is not within the averageing algorithm itself, it is caused by the implementation of iMAOnArray and its behaviour with drawing buffers that have empty indexes, there may be a way to fix this another way so to be able to use drawing buffers without errors, but I was unable to find it.

This is very helpful. Have now fixed the problem I was having with my Controller indicator. So thank you for sharing.

Question. Have you ever tried using Hull MA with iMAOnArray?‌ Could it be done? The Hull MA is both smooth and has only small lag so would be really great for my purposes.

Reason: