How to use one indicators values to create a moving average in mql4 (for an EA) without calculating it muanually

 

Evening gents, I've been trying to do a calculation where it takes the values of an indicator and then creates a moving average from that. I can do this by using a horrible for-loop and use the shift function to add up and then divide by the amount I shifted it.

ex:

for(int i = 2; i <= lookback; i++){
      Vol01 += iVolume(_Symbol,PERIOD_CURRENT,i);
      
      if(i == lookback){
      Vol1 = Vol01/(lookback-1);
      }
      }


However, when using this method with more complex indicators, it causes mt4 to being extremely slow. I know the proper way to do this is by using the MA-ON-ARRAY, function, but there is little to no documentation on how one actually does this. I know how to make arrays etc. but I'm confused to what the array is actually supposed to be holding. If i made an array with a length of 50, an then used the FILL-ARRAY & ARRAY-SET-AS-SERIES function, inputting the values from the indicator, & then  used the MA-ON-ARRAY, with the values from the array, what exactly is this doing? does this mean that the MA length would need to be less than the array length?

 
  1. Baki Hanma: I can do this by using a horrible for-loop and use the shift function to add up and then divide by the amount I shifted it.

    Drop your unnecessary if inside the loop.

        for(int i = 2; i <= lookback; i++){
            Vol01 += iVolume(_Symbol,PERIOD_CURRENT,i);
        }   
        vol1 = Vol01/(lookback - 1);
    }
  2. There is nothing "horrible" about for loops. Write them correctly and use modular coding.

    vol1 = sma_on_array(Volume, 2, lookback+1);
    
    //////////////////////////////////////////////////////////////
    template<typename T>
    double sma_on_array(const T& array[], int iBeg=0, int iEnd=WHOLE_ARRAY){
        if(iEnd ==  WHOLE_ARRAY) iEnd = ArraySize(array);
        return sum_array(array, iBeg, iEnd) / double(iEnd - iBeg);
    }
    template<typename T>
    double sum_array(const T& array, int iBeg, int iEnd){
        T sum=0;
        while(iBeg < iEnd) sum += array[iBeg++];
        return sum;
    }
  3. Baki Hanma: . I know the proper way to do this is by using the MA-ON-ARRAY, function, but there is little to no documentation on how one actually does this. I know how to make arrays etc. but I'm confused to what the array is actually supposed to be holding.

    Set the array to as-series, fill the array, call the function to get the MA where you want. Obviously, it has to be holding what you want the average of.

    double v[]; ArraySetAsSeries(v, true);
    
    int vSize=ArrayCopySeries(v, MODE_VOLUME)
    
    int iBeg=2, iEnd=lookback+1, length=lEnd - iBeg + 1;
    double smaVol=iMAOnArray(v, vSize, length, 0, MODE_SMA, iBeg);
  4. Baki Hanma: If i made an array with a length of 50, an then used the FILL-ARRAY & ARRAY-SET-AS-SERIES function, inputting the values from the indicator, & then  used the MA-ON-ARRAY, with the values from the array, what exactly is this doing?

    Obviously, it is computing the moving average of what you put into the array.

    int iBeg=2, iEnd=lookback+1, length=lEnd - iBeg + 1, smaLength=length;
    
    double v[]; ArrayResize(v, length); ArraySetAsSeries(v, true);
    for(int iV=0; iBeg < iEnd; ++iBeg, ++iV) v[iV] =iCustom(…, iBeg);
    
    double smaVol=iMAOnArray(v, length, smaLength, 0, MODE_SMA, 0);
  5. Baki Hanma: does this mean that the MA length would need to be less than the array length?

    For SMA/LWMA, less than or equal. For EMA see How MT4 calculate its EMA & MACD ?? The formula used by MT4 seems to be different than the formula generally used by other platform. - Technical Indicators - MQL5 programming forum #2.4 (2019)