Incorrect return result while calling function in loop, versus, correct return result when calling function outside loop - Calculating hull moving average function - EXPERT ADVISOR

 

Hi there.

I hope someone can help me figure out what is happening here.

I have a function, inside a expert advisor, called  "calculateHMA". It calculates the Hull Moving Average value for any specified candle on the current chart.

It returns the Hull Moving Average double value for the specified candle.

There is also a helper function, "pushDouble", that shifts all values in an array, and adds a new value in index zero.

"pushDouble"  is being used inside the "calculateHMA" function, for preparing a temporary array, to be used in the iMaOnArray function(which returns the final value).

----PROBLEM!----

When the "calculateHMA" is called with the mql4 Print function, it prints the correct return values. (These values are verified by comparing it with the Hull Moving Average Indicator)

But, when the same function is placed in a for loop, and also set up so it Prints each value, it DOES NOT print the correct value. It only prints the same value over and over.

The loop variable "i" is used to specify which candle, by passing it as a parameter into the "calculateHMA" function, should calculated. On each new itiration, the function should calculate the next candle.


How is this possible? Why am I getting different results in these 2 scenarios. I need this function to work inside a loop.

Anybody got some advice?


//////////////////////////////////////////////////////////////////////
#property  strict
input int hmaPeriod = 20;
//////////////////////////////////////////////////////////////////////
double calculateHMA(int forCandle)
  {
   int c = forCandle;
   double hmaCalculate[];
   ArrayResize(hmaCalculate,hmaPeriod);
   int periodSqrt = (int)MathRound(MathSqrt(hmaPeriod));
   int periodHalf = (int)MathRound(hmaPeriod/2);
   for(int i=0;i<hmaPeriod;i++)
     {
      double wma1 = iMA(Symbol(),0,periodHalf,0,3,PRICE_CLOSE,c);
      double wma2 = iMA(Symbol(),0,hmaPeriod,0,3,PRICE_CLOSE,c);
      pushDouble(hmaCalculate, 2*wma1 - wma2);
      c += 1;
     }
   return iMAOnArray(hmaCalculate,0, periodSqrt, 0, 3, 0);
  }
//////////////////////////////////////////////////////////////////////
void pushDouble(double &array[],double value)
  {
   int length = ArraySize(array);
   for(int i=0;i<length-1;i++) array[length-1-i] = array[length-2-i];
   array[0] = value;
  }
//////////////////////////////////////////////////////////////////////
int OnInit()
  {
   // These function calls return & print correctly
   Print(calculateHMA(0));
   Print(calculateHMA(1));
   Print(calculateHMA(2));
   Print(calculateHMA(3));
   
   // But these function calls, placed in a loop, prints incorrectly
   for(int i=0;i<4;i++) Print(calculateHMA(i));
   
   return(INIT_SUCCEEDED);
  }
//////////////////////////////////////////////////////////////////////
//Output of the first 4 function calls return & print correctly:

// 0 - 1.258478612554105
// 1 - 1.279231667532465
// 2 - 1.291050675390902
// 3 - 1.334656612510501

//Output of the second 4 function calls, in loop return & print incorrectly:

// 0 - 1.258478612554105
// 1 - 1.258478612554105
// 2 - 1.258478612554105
// 3 - 1.258478612554105




regards

Julian

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
julianrbach:

Hi there.

I hope someone can help me figure out what is happening here.

I have a function, inside a expert advisor, called  "calculateHMA". It calculates the Hull Moving Average value for any specified candle on the current chart.

It returns the Hull Moving Average double value for the specified candle.

There is also a helper function, "pushDouble", that shifts all values in an array, and adds a new value in index zero.

"pushDouble"  is being used inside the "calculateHMA" function, for preparing a temporary array, to be used in the iMaOnArray function(which returns the final value).

----PROBLEM!----

When the "calculateHMA" is called with the mql4 Print function, it prints the correct return values. (These values are verified by comparing it with the Hull Moving Average Indicator)

But, when the same function is placed in a for loop, and also set up so it Prints each value, it DOES NOT print the correct value. It only prints the same value over and over.

The loop variable "i" is used to specify which candle, by passing it as a parameter into the "calculateHMA" function, should calculated. On each new itiration, the function should calculate the next candle.


How is this possible? Why am I getting different results in these 2 scenarios. I need this function to work inside a loop.

Anybody got some advice?





regards

Julian


Your iMA s are missing the required shifts. You query the iMA every time on the most current candle.

Read the docs to iMA and add the "i" variable to it accordingly.

 
Dominik Egert #:

Your iMA s are missing the required shifts. You query the iMA every time on the most current candle.

Read the docs to iMA and add the "i" variable to it accordingly.

I thought so too until I saw this😄:

for(int i=0;i<hmaPeriod;i++)
     {
      double wma1 = iMA(Symbol(),0,periodHalf,0,3,PRICE_CLOSE,c);
      double wma2 = iMA(Symbol(),0,hmaPeriod,0,3,PRICE_CLOSE,c);
      pushDouble(hmaCalculate, 2*wma1 - wma2);
      c += 1;
     }
 
Vladislav Boyko #:

I thought so too until I saw this😄:


Right. So the problem seems to be between the pushArray and iMAonArray function.

I am not very familiar with iMAonArray, so I can only guess about its calculation direction. But I suspect the pushArray function is the issue.

Since the array is being filled anyways, why not just fill it directly inside the loop, instead of using the pushArray function?


hmaCalculate[hmaPeriod - 1 - i] = 2*wma1 - wma2;
 

Forum on trading, automated trading systems and testing trading strategies

Should i move from MQL4 to MQL5 ?

Alain Verleyen, 2015.12.04 11:20

Yes it can. iMAOnArray() is obsolete and ineffective, replace it with calls to functions from MovingAverages.mqh library (standard with MT4, provided in Include folder).


You can always have bugs in MT4 and MT5 as in any software.

Reason: