Yesterday iMA value in EA

 

Hi

I need to get yesterday iMA value in this mql5 EA, Isn't the code below over complicated? if so, is there a simpler way? Thanks


  double maValue(int days){
    int handle = iMA(NULL, PERIOD_D1, days, 0, MODE_SMA, PRICE_WEIGHTED);
    double maArray[2];
    SetIndexBuffer(0,maArray,INDICATOR_DATA);
    CopyBuffer(handle, 0, 0, 2, maArray);
    IndicatorRelease(handle);
    return maArray[1];  //since index 0 is today and we want yesterday value
  }
 

As you know MA is just all the price values of the period bars added up to each other and then divided by the period to get to the average.

So you could simply calculate it yourself without having to call the indicator.

Reason: