Calling iMA efficiently

 

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?

#define UPTREND 1
#define NOTREND 0
#define DOWNTREND -1

int movingAverageTrend(){
    double fastMaM15, slowMaM15, fastMaH1, slowMaH1;
    fastMaM15 = iMA(_Symbol, PERIOD_M15, 18, 0, MODE_EMA, PRICE_CLOSE, 0);
    slowMaM15 = iMA(_Symbol, PERIOD_M15, 50, 0, MODE_EMA, PRICE_CLOSE, 0);
    fastMaH1  = iMA(_Symbol, PERIOD_H1,  18, 0, MODE_EMA, PRICE_CLOSE, 0);
    slowMaH1  = iMA(_Symbol, PERIOD_H1,  50, 0, MODE_EMA, PRICE_CLOSE, 0);

    if(fastMaM15 < slowMaM15 && fastMaH1 < slowMaH1)
        return DOWNTREND;
    else if(fastMaM15 > slowMaM15 && fastMaH1 > slowMaH1)
        return UPTREND;
    else
        return NOTREND;
}

Thanks.

 

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.

 
Thanks for your response, Luke. I'll need to take a closer look at what makes MQ4 vs MQ5.
 
LukeB:

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 ?

 
LukeB:

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 ?

 
budiali:

Is it possible moving the handles part in OnInit() section into OnTick() section ?

I did just that in the code here: https://www.mql5.com/en/code/23491 (OnCalculate section, which could be done similarly with OnTick).  The idea is that it implements retries for getting the handles (although I haven't ever seen a failure for that).
NonLagMA with ATR Bands and Control Panel
NonLagMA with ATR Bands and Control Panel
  • www.mql5.com
The control dashboard has features as shown: Click "Trend Bar" to turn the indicator on or off.Drag and Drop "Non-Lag-MA" to reposition the information display.Clicking "Top Band" will display the 'alfa' array and weight variable of the nonLagMA.  This nothing but a curiosity display.Clicking anyplace on the chart selects the bar at that chart...
 
Simone Gill:

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);

 
LukeB:

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?
 
rhodium1trading :
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

How to start with MQL5

Vladimir Karputov, 2020.11.13 06:01

Receiving data from an indicator in an MQL5.

Scheme:

Getting data from indicators in an MQL5 Expert Advisor

An example for the iMA indicator:


 
  1. 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.

  2. 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

Reason: