The above appears to be MQ4 code, and looks like it should be fine to me. The MQ5 iMA function does not return a moving average value - it returns a 'handle' to the moving average - sort of like something that points to the moving average values.
In my terminal I have a OsMA.mq5 Indicator example that shows the use of iMA:
The handles:
//--- MA handles
int ExtFastMaHandle;
int ExtSlowMaHandle;
Then in OnInit, the handles get values:
//--- get MAs handles
ExtFastMaHandle=iMA(NULL,0,InpFastEMAPeriod,0,MODE_EMA,InpAppliedPrice);
ExtSlowMaHandle=iMA(NULL,0,InpSlowEMAPeriod,0,MODE_EMA,InpAppliedPrice);
finally, in OnCalculate the actual MA values are obtained:
if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) and
if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
Only after the MA values are obtained using CopyBuffer can they be compared and the Trend be determined.
The above appears to be MQ4 code, and looks like it should be fine to me. The MQ5 iMA function does not return a moving average value - it returns a 'handle' to the moving average - sort of like something that points to the moving average values.
In my terminal I have a OsMA.mq5 Indicator example that shows the use of iMA:
The handles:
//--- MA handles
int ExtFastMaHandle;
int ExtSlowMaHandle;
Then in OnInit, the handles get values:
//--- get MAs handles
ExtFastMaHandle=iMA(NULL,0,InpFastEMAPeriod,0,MODE_EMA,InpAppliedPrice);
ExtSlowMaHandle=iMA(NULL,0,InpSlowEMAPeriod,0,MODE_EMA,InpAppliedPrice);
finally, in OnCalculate the actual MA values are obtained:
if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) and
if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
Only after the MA values are obtained using CopyBuffer can they be compared and the Trend be determined.
Hi Luke,
Just a quick question if I wanted to apply the MA to a range of currency pairs I would just change symbol to the desired pair ?
The above appears to be MQ4 code, and looks like it should be fine to me. The MQ5 iMA function does not return a moving average value - it returns a 'handle' to the moving average - sort of like something that points to the moving average values.
In my terminal I have a OsMA.mq5 Indicator example that shows the use of iMA:
The handles:
//--- MA handles
int ExtFastMaHandle;
int ExtSlowMaHandle;
Then in OnInit, the handles get values:
//--- get MAs handles
ExtFastMaHandle=iMA(NULL,0,InpFastEMAPeriod,0,MODE_EMA,InpAppliedPrice);
ExtSlowMaHandle=iMA(NULL,0,InpSlowEMAPeriod,0,MODE_EMA,InpAppliedPrice);
finally, in OnCalculate the actual MA values are obtained:
if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) and
if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
Only after the MA values are obtained using CopyBuffer can they be compared and the Trend be determined.
Is it possible moving the handles part in OnInit() section into OnTick() section ?
Is it possible moving the handles part in OnInit() section into OnTick() section ?
![NonLagMA with ATR Bands and Control Panel NonLagMA with ATR Bands and Control Panel](https://c.mql5.com/36/23/nonlagma-with-atr-bands-and-control.jpg)
- www.mql5.com
Hi Luke,
Just a quick question if I wanted to apply the MA to a range of currency pairs I would just change symbol to the desired pair ?
Yes. For MQ5, The handle would have to be obtained with the desired parameters, including the pair: For example, using a string variable with the pair string would work:
string the_symbol = _Symbol;
ExtFastMaHandle=iMA(the_symbol,0,InpFastEMAPeriod,0,MODE_EMA,InpAppliedPrice);
The above appears to be MQ4 code, and looks like it should be fine to me. The MQ5 iMA function does not return a moving average value - it returns a 'handle' to the moving average - sort of like something that points to the moving average values.
In my terminal I have a OsMA.mq5 Indicator example that shows the use of iMA:
The handles:
//--- MA handles
int ExtFastMaHandle;
int ExtSlowMaHandle;
Then in OnInit, the handles get values:
//--- get MAs handles
ExtFastMaHandle=iMA(NULL,0,InpFastEMAPeriod,0,MODE_EMA,InpAppliedPrice);
ExtSlowMaHandle=iMA(NULL,0,InpSlowEMAPeriod,0,MODE_EMA,InpAppliedPrice);
finally, in OnCalculate the actual MA values are obtained:
if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) and
if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
Only after the MA values are obtained using CopyBuffer can they be compared and the Trend be determined.
Hi.. The "CopyBuffer" part is bit confusing. I want to get the value of either Fast MA or Slow MA at the last candle. Could you please help me to do this?
An example of how to get data from indicators:
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2020.11.13 06:01
Receiving data from an indicator in an MQL5.
Scheme:
An example for the iMA indicator:
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum?
Next time post in the correct place. The moderators will likely move this thread there soon. -
On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- 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,
I have the function below and it works. But then I read this:
https://www.mql5.com/en/articles/43
where it says that iCustom is best called in OnInit(). Is this also true for my function? I can imagine that every call to iMA recreates a moving average calculated over many values and then only returns the last value. Or is this fine as it is?
Thanks.