Help with Hull Moving Averages

 

So, by accident I have found about Hull Moving Averages and I love using them.  Unfortunately, most platforms don't have native support for them, so I find myself writing my own indicators using them.  The problem I have is that I am finding an inconsistency with some of my implementations and I'm hoping someone can spot the problem.

First is the indicator code that I feel works correctly:

   for(int i = limit - 1; i >= 0; i--) {
      ExtBuffer[i] = 2.0 * iMA(_Symbol, _Period, HalfPeriod, 0, MODE_LWMA, Apply_To, i) -
         iMA(_Symbol, _Period, HullMA_Period, 0, MODE_LWMA, Apply_To, i);
   }
   
   for (int i = limit -1; i >= 0; i--) {
      HullMA_Buffer[i] = NormalizeDouble(iMAOnArray(ExtBuffer, 0, HullPeriod, 0, MODE_LWMA, i), _Digits);
   }


The problem comes in with another function I have written to calculate the same information but on a much smaller scale to be used in an EA:

double HullMA(const int period, const int price, const int index) {
   int HalfPeriod = (int) MathRound(period / 2);
   int HullPeriod = (int) MathRound(MathSqrt(period));
   double temp_buffer[];

   ArrayResize(temp_buffer, HullPeriod);
   ArraySetAsSeries(temp_buffer, true);

   for(int i = (HullPeriod - 1); i >= 0; i--) {
      temp_buffer[i] = 2.0 * iMA(NULL, 0, HalfPeriod, 0, MODE_LWMA, price, i + index) -
         iMA(NULL, 0, period, 0, MODE_LWMA, price, i + index);
   }
   
   return iMAOnArray(temp_buffer, 0, HullPeriod, 0, MODE_LWMA, 0);
}

When plotted on a chart, the two are slightly different.

The Problem

Any suggestions as to where I'm going wrong?


-Ditto2X

 
There are already plenty of versions of Hull on this site.
 
Alain Verleyen:
There are already plenty of versions of Hull on this site.

Exactly. No need to reinvent the wheel.

This is the version I use written by mladen.

https://www.mql5.com/en/code/16800

Hull Moving Average
Hull Moving Average
  • www.mql5.com
Hull moving average with arbitrary weights of calculation. When Hull power is set to 1 it is the same as regular Hull moving average. Any other value makes it different than the...
 
Alain Verleyen:
There are already plenty of versions of Hull on this site.

Correct, but I also would like to be able to apply this MA on other sets of data.  In the second code piece I gave, I could easily have used iMAOnArray on some other indicators data, and I'm trying to figure out why my implementation doesn't match up.

Reason: