Coding a pinescript formula on mql5

 
Hello, I am working on a project. Within this project, I need to write a method that uses more than one array. But I couldn't get the result I wanted. Can anyone help me?

The pinescript formula: 
smoothrng(int float, int t, float m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), t)
    smoothrng = ta.ema(avrng, wper) * m
    smoothrng


My MQL5 Code:

double smoothrng(const double &price[], int period, double multiplier) {
    int wper = period * 2 - 1;
    double avrng[], smoothrng[];

    for (int i = 0; i < wrep*2-1; i++) {
        arrayPush(avrng, MathAbs(price[i] - price[i + 1]));
    }

    ArraySetAsSeries(avrng, true);

    double ema_avrng[];
    for (int i = 0; i < ArraySize(avrng); i++) {
        arrayPush(ema_avrng, ExponentialMA(i, period, (i == 0 ? 0 : ema_avrng[i-1]), avrng));
    }
    ArraySetAsSeries(ema_avrng, true);
 
   double ema_smoothrng[];
    for (int i = 0; i < ArraySize(ema_avrng); i++) {
        arrayPush(ema_smoothrng, ExponentialMA(i, wper, (i == 0 ? 0 : ema_smoothrng[i-1]), ema_avrng) * multiplier);
    }
    ArraySetAsSeries(ema_smoothrng, true);
 

    return ema_smoothrng[0];
}






double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
  {
   double result=0.0;
//--- check period
   if(period>0)
     {
      double pr=2.0/(period+1.0);
      result= MathAbs(price[position]) *pr+prev_value*(1-pr);
     }
   return(result);
  }

void arrayPush(double &array[],double value){
  int arrSize = ArraySize(array)+1;
  ArrayResize(array,arrSize);
  array[arrSize-1] = value;
}
 

when arrays are set as series, the previous value would be:

ema_avrng[i+1]

and not:

ema_avrng[i-1]

so I think should be

ExponentialMA(i, period, (i == 0 ? 0 : ema_avrng[i+1]), avrng))


You could use ExponentialMA function from MovingAverages.mqh instead as well