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?
regards
Julian
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; }
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).- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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