How do you code this function for mt5

 

Hi,  I’m trying to convert this function from mt4 to mt5 but I'm not sure how as I'm not familiar with mql5 code yet.

Any help would be appreciated.


double GetFunction()
{
   int j,k;
   double sumw = (Period1+1);   
   double sum  =  sumw* Close[1];

   for(j=1, k=Period1; j<Period1; j++,k--) 
   {
      sumw += k;
      sum  += k * Close[1+j];
      if (j<=1) 
      {
         sum  += Close[1-j]*k;  
         sumw += k; 
      }
   }
return(sum/sumw);
}
 
In MQL5, to iterate through price data in chronological order (from oldest to newest), you need to call ArraySetAsSeries() to reverse the direction of the array processing


try that maybe

double GetFunction()
{
   int j, k;
   double sumw = (Period1 + 1);   
   double sum  = sumw * Close[1];

   ArraySetAsSeries(Close, true); //Set Close array to be processed from oldest to newest

   for(j = 1, k = Period1 - 1; j < Period1; j++, k--)
   {
      sumw += k;
      sum  += k * Close[j + 1];
      if (j <= 1) 
      {
         sum  += Close[1 - j] * k;  
         sumw += k; 
      }
   }
   return (sum / sumw);
}


or use ArraysSetAsSeries in a more convenient place to reverse the processing of the buffers

 
How to convert that MT5 indicator into expert advisor 
 
Conor Mcnamara #:
In MQL5, to iterate through price data in chronological order (from oldest to newest), you need to call ArraySetAsSeries() to reverse the direction of the array processing


try that maybe


or use ArraysSetAsSeries in a more convenient place to reverse the processing of the buffers

Thank you.  I will try this out.

 
Sefako Alfred #:
How to convert that MT5 indicator into expert advisor 

Thanks for the link.  It's very helpful!

 

1. In case your code is part of an indicator you have close array (with lower case 'c') in OnCalculate function. And as the above comment said one has to use the TimeSeries(AS_SERIES) flag set to true. You will need to pass the close array  as a const double array parameter to your function.

2. If this is part of an EA things are difficult. You have to use iClose or call CopyRates to access close price. However those require you to check possible errors when loading data(especially if you are using a symbol/time other than the current chart).

Reason: