I need to convert this loop from mql4 to mql5!

 
string TestROC_( int nPeriod, string cPair)
{
   
       int i;
   int sizeOfnResultROC = nPeriod;      
   double nResultROC[];  
   double nResultClose;
   double nResult;  
   double nResultAbs;  
   double nResultA;
   double nResultB;

 
   int nRow = 2;
   int nCol = 3;
   
   ArrayResize(nResultROC,sizeOfnResultROC);
   ArraySetAsSeries(nResultROC,true);   
   
   for(i = 0; i < sizeOfnResultROC ; i++)
   {       
   nResultROC[i] = 100*(iClose( cPair,nTimeFrame1,i)-iOpen(cPair,nTimeFrame1,i))/iOpen(cPair,nTimeFrame1,i);  
   }
   nResult = iMAOnArray( nResultROC,0,PeriodEA,0,MODE_SMA,0);
   nResultAbs = MathAbs(nResult);
   nResultA = iOpen(cPair,nTimeFrame1,0)*(1 + nResultAbs);
   nResultB = iOpen(cPair,nTimeFrame1,0)*(1 - nResultAbs);

   nResultClose = iClose(cPair,nTimeFrame1,0);

   if( nResultClose < nResultB  &&  nResult < 0 ) return("SELL");
   if( nResultClose > nResultA  &&  nResult > 0 ) return("BUY");
   return("");

}

 
Robert Milovic:
string TestROC_( int nPeriod, string cPair)
{
   
       int i;
   int sizeOfnResultROC = nPeriod;      
   double nResultROC[];  
   double nResultClose;
   double nResult;  
   double nResultAbs;  
   double nResultA;
   double nResultB;

 
   int nRow = 2;
   int nCol = 3;
   
   ArrayResize(nResultROC,sizeOfnResultROC);
   ArraySetAsSeries(nResultROC,true);   
   
   for(i = 0; i < sizeOfnResultROC ; i++)
   {       
   nResultROC[i] = 100*(iClose( cPair,nTimeFrame1,i)-iOpen(cPair,nTimeFrame1,i))/iOpen(cPair,nTimeFrame1,i);  
   }
   nResult = iMAOnArray( nResultROC,0,PeriodEA,0,MODE_SMA,0);
   nResultAbs = MathAbs(nResult);
   nResultA = iOpen(cPair,nTimeFrame1,0)*(1 + nResultAbs);
   nResultB = iOpen(cPair,nTimeFrame1,0)*(1 - nResultAbs);

   nResultClose = iClose(cPair,nTimeFrame1,0);

   if( nResultClose < nResultB  &&  nResult < 0 ) return("SELL");
   if( nResultClose > nResultA  &&  nResult > 0 ) return("BUY");
   return("");

}

Try This converted from MQL4 to MQL5.

//+------------------------------------------------------------------+
//|                                                     TestROC_.mq5 |
//|                        Converted from MQL4 to MQL5               |
//+------------------------------------------------------------------+
string TestROC_(int nPeriod, string cPair)
{
   //--- variables
   int sizeOfnResultROC = nPeriod;
   double nResultROC[];  // array for percentage changes
   ArrayResize(nResultROC, sizeOfnResultROC);
   ArraySetAsSeries(nResultROC,true); // so that index 0 is the current bar

   // arrays to hold open and close prices
   double openPrices[];
   double closePrices[];
   
   //--- Retrieve open and close prices for the last nPeriod bars
   // CopyOpen() and CopyClose() return number of copied bars. Check for errors.
   if(CopyOpen(cPair, nTimeFrame1, 0, sizeOfnResultROC, openPrices) <= 0)
   {
      Print("Error copying open prices for ", cPair);
      return "";
   }
   if(CopyClose(cPair, nTimeFrame1, 0, sizeOfnResultROC, closePrices) <= 0)
   {
      Print("Error copying close prices for ", cPair);
      return "";
   }
   
   //--- Set arrays as series so that index 0 is the latest bar
   ArraySetAsSeries(openPrices, true);
   ArraySetAsSeries(closePrices, true);
   
   //--- Calculate Rate-of-Change percentage for each bar
   for(int i = 0; i < sizeOfnResultROC; i++)
   {
      // Be cautious with division by zero
      if(openPrices[i] != 0)
         nResultROC[i] = 100.0 * (closePrices[i] - openPrices[i]) / openPrices[i];
      else
         nResultROC[i] = 0;
   }
   
   //--- Calculate Moving Average on the nResultROC array
   // iMAOnArray parameters:
   //   array, total (set to 0 to use whole array), period, shift, method, start index.
   double nResult = iMAOnArray(nResultROC, 0, PeriodEA, 0, MODE_SMA, 0);
   double nResultAbs = MathAbs(nResult);
   
   //--- Current open and close prices (for bar 0)
   double currentOpen  = openPrices[0];
   double currentClose = closePrices[0];
   
   //--- Calculate threshold prices
   double nResultA = currentOpen * (1 + nResultAbs);
   double nResultB = currentOpen * (1 - nResultAbs);
   
   //--- Decision logic: compare current close price with thresholds
   if(currentClose < nResultB && nResult < 0)
      return("SELL");
   if(currentClose > nResultA && nResult > 0)
      return("BUY");
   
   return("");
}
 
Thanks a lot, I will check this out and let you know!
 
This is the problem that I get when I input the above code, enclosed in the screenshot. If you can suggest some solution to the problem I`d appreciate it, Thanks
Files:
 
Robert Milovic #:
This is the problem that I get when I input the above code, enclosed in the screenshot. If you can suggest some solution to the problem I`d appreciate it, Thanks

Try This....

//+------------------------------------------------------------------+
//|                                                     TestROC_.mq5 |
//|                        Converted from MQL4 to MQL5               |
//+------------------------------------------------------------------+

// Input parameters for timeframe and moving average period
input ENUM_TIMEFRAMES nTimeFrame1 = PERIOD_CURRENT;  // Timeframe input
input int            PeriodEA     = 14;              // Moving average period

// Custom function to compute a Simple Moving Average on an array.
// This version supports only SMA and ignores shift.
double MyIMAOnArray(const double &array[], int period, int start=0)
{
   int size = ArraySize(array);
   if(start < 0 || start + period > size)
   {
      Print("Not enough elements for the moving average calculation.");
      return 0.0;
   }
   
   double sum = 0.0;
   for(int i = start; i < start + period; i++)
      sum += array[i];
   
   return sum / period;
}

// TestROC_ calculates the Rate-of-Change (ROC) based signal for a given symbol.
// It returns "BUY", "SELL", or an empty string if no signal is generated.
string TestROC_(int nPeriod, string cPair)
{
   //--- Resize and set array for ROC values (index 0 is the current bar)
   int sizeOfnResultROC = nPeriod;
   double nResultROC[];
   ArrayResize(nResultROC, sizeOfnResultROC);
   ArraySetAsSeries(nResultROC, true);
   
   //--- Arrays to hold open and close prices
   double openPrices[];
   double closePrices[];
   
   //--- Retrieve open prices for the last nPeriod bars
   if(CopyOpen(cPair, nTimeFrame1, 0, sizeOfnResultROC, openPrices) <= 0)
   {
      Print("Error copying open prices for ", cPair);
      return "";
   }
   //--- Retrieve close prices for the last nPeriod bars
   if(CopyClose(cPair, nTimeFrame1, 0, sizeOfnResultROC, closePrices) <= 0)
   {
      Print("Error copying close prices for ", cPair);
      return "";
   }
   
   //--- Set the arrays as series so that index 0 is the current bar
   ArraySetAsSeries(openPrices, true);
   ArraySetAsSeries(closePrices, true);
   
   //--- Calculate Rate-of-Change percentage for each bar
   for(int i = 0; i < sizeOfnResultROC; i++)
   {
      if(openPrices[i] != 0)
         nResultROC[i] = 100.0 * (closePrices[i] - openPrices[i]) / openPrices[i];
      else
         nResultROC[i] = 0;
   }
   
   //--- Calculate the Moving Average on the nResultROC array using the custom function
   double nResult = MyIMAOnArray(nResultROC, PeriodEA, 0);
   double nResultAbs = MathAbs(nResult);
   
   //--- Get the current open and close prices (for bar 0)
   double currentOpen  = openPrices[0];
   double currentClose = closePrices[0];
   
   //--- Calculate threshold prices (convert the ROC percentage to a multiplier)
   double nResultA = currentOpen * (1 + nResultAbs / 100.0);
   double nResultB = currentOpen * (1 - nResultAbs / 100.0);
   
   //--- Decision logic: Determine the signal based on current price thresholds
   if(currentClose < nResultB && nResult < 0)
      return "SELL";
   if(currentClose > nResultA && nResult > 0)
      return "BUY";
   
   return "";
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("TestROC_ EA initialized");
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Example usage of TestROC_ on the current symbol with 20 bars.
   string signal = TestROC_(20, Symbol());
   if(signal != "")
      Print("Signal for ", Symbol(), ": ", signal);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Print("TestROC_ EA deinitialized");
}