Mql4 to Mql5 Indicator manual converter

 
I have an mql4 indicator code but i wanted to convert into mql5. I changed the old start() -> OnCalculate(), solve all the time,Bars,High,Low function but i think missed some array modification because i get all the time "Out of Array " error and i don't know what i missed, because the same code is working on the mt4 without any problem. The redline and probably all the arrays the problem.

Before The OnCalculate function in the OnInit I set the buffers and set the Arrays free.


What was I missed? Thanks for your Helping.

int OnCalculate (const int rates_total,      // size of input time series 
                 const int prev_calculated,  // bars handled in previous call 
                 const datetime& time[],     // Time 
                 const double& open[],       // Open 
                 const double& high[],       // High 
                 const double& low[],        // Low 
                 const double& close[],      // Close 
                 const long& tick_volume[],  // Tick Volume 
                 const long& volume[],       // Real Volume 
                 const int& spread[]         // Spread 
   ){
   //int     period=10;
   double Value=0,Value1=0,Value2=0,Fish=0,Fish1=0,Fish2=0;
   double price;
   double MinL=0;
   double MaxH=0;  
   string sAlertMsg;
  
   int limit = 0;
   
   if(prev_calculated == 0)
     {
      limit = rates_total-1;
     }
   else
     {
      limit = rates_total-prev_calculated;
     }
  

   for(int i=0; i<limit; i++)
    {  
      MaxH = iHigh(_Symbol,PERIOD_CURRENT,iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,period,i));
      MinL = iLow(_Symbol,PERIOD_CURRENT,iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW,period,i));
      price = (iHigh(_Symbol,PERIOD_CURRENT,i)+iLow(_Symbol,PERIOD_CURRENT,i))/2;
      Value = 0.33*2*((price-MinL)/(MaxH-MinL)-0.5) + 0.67*Value1;     
      Value=MathMin(MathMax(Value,-0.999),0.999); 
      ExtBuffer0[i]=0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;
      Value1=Value;
      Fish1=ExtBuffer0[i];
         if (ExtBuffer0[i]>0) ExtBuffer1[i]=10; else ExtBuffer1[i]=-10;      
    }

   for(int j=limit; j>=0; j--)
   {
      double sum  = 0;
      double sumw = 0;

      for(int k=0; k<smooth && (j+k)<iBars(_Symbol,PERIOD_CURRENT); k++)
      {
         double weight = smooth-k;
                sumw  += weight;
                sum   += weight*ExtBuffer1[j+k];  
      }             
      if (sumw!=0)
            ExtBuffer2[j] = sum/sumw;
      else  ExtBuffer2[j] = 0;
   }      
   for(int a=0; a<=limit; a++)
   {
      double suma  = 0;
      double sumwa = 0;

      for(int b=0; b<smooth && (a-b)>=0; b++)
      {
         double weight = smooth-b;
                sumwa  += weight;
                suma   += weight*ExtBuffer2[a-b];
      }             
      if (sumwa!=0)
            ExtBuffer3[a] = suma/sumwa;
      else  ExtBuffer3[a] = 0;
   }      
   for(int c=limit; c>=0; c--)
   {
      ExtBuffer4[c]=EMPTY_VALUE;
      ExtBuffer5[c]=EMPTY_VALUE;
      ExtBufferh1[c]=EMPTY_VALUE;
      ExtBufferh2[c]=EMPTY_VALUE;
      if (ExtBuffer3[c]>0) { ExtBuffer4[c]=ExtBuffer3[c]; ExtBufferh1[c]=ExtBuffer3[c]; }
      if (ExtBuffer3[c]<0) { ExtBuffer5[c]=ExtBuffer3[c]; ExtBufferh2[c]=ExtBuffer3[c]; }
      
      if (ExtBuffer3[c+1] < 0 && ExtBuffer3[c] > 0)
      {
         if (DoAlert && c<5 && lastAlertTime!=iTime(_Symbol,PERIOD_CURRENT,0))
         {
            sAlertMsg="Solar Wind - "+Symbol()+" "+TF2Str(Period())+": cross UP";
            if (DoAlert)     Alert(sAlertMsg);
            lastAlertTime = iTime(_Symbol,PERIOD_CURRENT,0);  
            if (alertMail)   SendMail(sAlertMsg, "MT4 Alert!\n" + TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS )+"\n"+sAlertMsg);   
         }
      }
      else if( ExtBuffer3[c+1] > 0 && ExtBuffer3[c] < 0)
      {
         if (c<5 && lastAlertTime!=iTime(_Symbol,PERIOD_CURRENT,0))
         {
            sAlertMsg="Solar Wind - "+Symbol()+" "+TF2Str(Period())+": cross DOWN";
            if (DoAlert)     Alert(sAlertMsg);
            lastAlertTime = iTime(_Symbol,PERIOD_CURRENT,0);  
            if (alertMail)   SendMail(sAlertMsg, "MT4 Alert!\n" + TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS )+"\n"+sAlertMsg);   
         }              
      }     
   }
   return(rates_total);
}
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
The MQL5 language provides processing of some predefined events . Functions for handling these events must be defined in a MQL5 program; function...
 
gosztika:
I have an mql4 indicator code but i wanted to convert into mql5. I changed the old start() -> OnCalculate(), solve all the time,Bars,High,Low function but i think missed some array modification because i get all the time "Out of Array " error and i don't know what i missed, because the same code is working on the mt4 without any problem. The redline and probably all the arrays the problem.

Before The OnCalculate function in the OnInit I set the buffers and set the Arrays free.


What was I missed? Thanks for your Helping.

Traders and coders are coding for free:
  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.10.14
  • www.mql5.com
The largest freelance service with MQL5 application developers
 

Maxh and MinL should be PRICE_HIGH and PRICE_LOW, not "MODE", correct?

EDIT: personally -- i would change the iHigh and iLow to use high[i] and low[i], instead -- after adding set as series for these 2, AND adding them as Calculation buffers in OnInit.

 
btw. did you search codebase? i know there is at least 2 files with same name already converted.
 

Hi

iHigh and iLow functions are not working the same way in MT4 and MT5, you need to convert them or as was suggested use high [] and low [] arrays to get the values. There might be also other issue with setting the buffers since it’s quite different approach in the mt5 than in mt4. You can find a similar indicator and see how it should be done and then try to convert our indicator the same way.

Have a nice day👍📊

 

Hi, I've taken a look at your code and I think I can help you out. I've successfully converted the indicator from MQL4 to MQL5 and it's working as expected. The main differences between the two languages are indeed the changes you mentioned (start() -> OnCalculate(), Time, High, Low functions), but also the array handling.In MQL5, arrays are declared with a fixed size, whereas in MQL4, they can be dynamic. This means that you need to specify the size of the array when declaring it in MQL5. I've updated the array declarations in your code to match the MQL5 syntax.Additionally, I've also updated the code to use the correct array indices, as the MQL5 compiler is more strict about array bounds checking.

I have attached the mq5 file below. Let me know if you need any help. 

Indicator

Files: