Translating from MQL4 to MQL5

 

Hello everyone!

I'm trying to translate (without much success) this indicator from MQL4 to MQL5. The code compiles, but there is no plot at all. What I'm missing?


MQL4 code:

//+------------------------------------------------------------------+
//|                                                   ppsev indi.mq4 |
//|                                     Pedro Pablo Severin Honorato |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pedro Pablo Severin Honorato"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3

input int       Mode =0;   // 0-RSI method; 1-Stoch method
input int       Length= 9; // Period
input int       Smooth= 1; // Period of smoothing
input int       Signal= 4; // Period of Signal Line
input int       Price=0;   // Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
input int       ModeMA= 3; // Mode of Moving Average

double SigBulls[];
double SigBears[];
double Bulls[];
double Bears[];
double AvgBulls[];
double AvgBears[];
double SmthBears[];
double SmthBulls[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(8);

   SetIndexStyle(0,DRAW_LINE,EMPTY,1);
   SetIndexBuffer(0,SigBulls);
   SetIndexStyle(1,DRAW_LINE,EMPTY,1);
   SetIndexBuffer(1,SigBears);

   SetIndexBuffer(2,Bulls);
   SetIndexBuffer(3,Bears);
   SetIndexBuffer(4,AvgBulls);
   SetIndexBuffer(5,AvgBears);
   SetIndexBuffer(6,SmthBears);
   SetIndexBuffer(7,SmthBulls);

   SetIndexDrawBegin(0,Length+Smooth+Signal);
   SetIndexDrawBegin(1,Length+Smooth+Signal);

   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(3,0.0);
   SetIndexEmptyValue(4,0.0);
   SetIndexEmptyValue(5,0.0);
   SetIndexEmptyValue(6,0.0);
   SetIndexEmptyValue(7,0.0);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      shift,limit,counted_bars=IndicatorCounted();
   double   Price1,Price2,smax,smin;
//---

   if(counted_bars < 0)return(-1);
   if(counted_bars==0)limit=Bars-Length+Smooth+Signal-1;
   if(counted_bars<1)
      for(int i=1;i<Length+Smooth+Signal;i++)
        {
         Bulls[Bars-i]=0;
         Bears[Bars-i]=0;
         AvgBulls[Bars-i]=0;
         AvgBears[Bars-i]=0;
         SmthBulls[Bars-i]=0;
         SmthBears[Bars-i]=0;
         SigBulls[Bars-i]=0;
         SigBears[Bars-i]=0;
        }
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;

   for(shift=limit; shift>=0; shift--)
     {
      Price1=iMA(NULL,0,1,0,0,Price,shift);
      Price2=iMA(NULL,0,1,0,0,Price,shift+1);
      //----
      if(Mode==0)
        {
         Bulls[shift]=0.5*(MathAbs(Price1-Price2)+(Price1-Price2));
         Bears[shift]=0.5*(MathAbs(Price1-Price2)-(Price1-Price2));
        }
      if(Mode==1)
        {
         smax=High[Highest(NULL,0,MODE_HIGH,Length,shift)];
         smin=Low[Lowest(NULL,0,MODE_LOW,Length,shift)];
         Bulls[shift]=Price1 - smin;
         Bears[shift]=smax - Price1;
        }
     }
   for(shift=limit; shift>=0; shift--)
     {
      AvgBulls[shift]=iMAOnArray(Bulls,0,Length,0,ModeMA,shift);
      AvgBears[shift]=iMAOnArray(Bears,0,Length,0,ModeMA,shift);
     }
   for(shift=limit; shift>=0; shift--)
     {
      SmthBulls[shift]=iMAOnArray(AvgBulls,0,Smooth,0,ModeMA,shift);
      SmthBears[shift]=iMAOnArray(AvgBears,0,Smooth,0,ModeMA,shift);
     }

   for(shift=limit; shift>=0; shift--)
     {
      SigBulls[shift]=  SmthBulls[shift];
      SigBears[shift]=  SmthBears[shift];
      if(SmthBulls[shift]-SmthBears[shift]>0)
         SmthBears[shift]=0;
      else
         SmthBulls[shift]=0;
     }  //end for( shift=limit; shift>=0; shift--)

//--- return value of prev_calculated for next call
   return(0);
  }
//+------------------------------------------------------------------+

MQL5 code:

//+------------------------------------------------------------------+
//|                                          AbsoluteHisto PPSEV.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_plots 2
#property indicator_color1 clrBlue
#property indicator_style1 DRAW_LINE
#property indicator_width1 3
#property indicator_color2 clrRed
#property indicator_style2 DRAW_LINE
#property indicator_width2 3

input int       Mode =0;   // 0-RSI method; 1-Stoch method
input int       Length= 9; // Period
input int       Smooth= 1; // Period of smoothing
input int       Signal= 4; // Period of Signal Line
input int       Price=0;   // Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
input int       ModeMA= 3; // Mode of Moving Average

double SigBulls[];
double SigBears[];
double Bulls[];
double Bears[];
double AvgBulls[];
double AvgBears[];
double SmthBears[];
double SmthBulls[];
int imaPriceHandle,imaAvgBullsHandle,imaAvgBearsHandle,imaSmthBullsHandle,imaSmthBearsHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   int drawBegin=Length+Smooth+Signal;
   imaPriceHandle=iMA(NULL,PERIOD_CURRENT,1,0,MODE_EMA,PRICE_CLOSE);

   SetIndexBuffer(0,SigBulls,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,drawBegin);

   SetIndexBuffer(1,SigBears,INDICATOR_DATA);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,drawBegin);

   SetIndexBuffer(2,Bulls,INDICATOR_DATA);
   SetIndexBuffer(3,Bears,INDICATOR_DATA);
   SetIndexBuffer(4,AvgBulls,INDICATOR_DATA);
   SetIndexBuffer(5,AvgBears,INDICATOR_DATA);
   SetIndexBuffer(6,SmthBears,INDICATOR_DATA);
   SetIndexBuffer(7,SmthBulls,INDICATOR_DATA);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int shift,limit,toCopy,counted_bars=prev_calculated;
   double Price1,Price2,smax,smin,imaPriceBuffer[];
   ArraySetAsSeries(imaPriceBuffer,true);

   if(counted_bars<0) return -1;
   if(counted_bars==0) limit=rates_total -Length+Smooth+Signal-1;
   if(counted_bars<1)
     {
      for(int i=1;i<Length+Smooth+Signal;i++)
        {
         Bulls[rates_total-i]=0;
         SigBulls[rates_total-i]=0;
         SigBears[rates_total-i]=0;

         Bears[rates_total-i]=0;
         AvgBulls[rates_total-i]=0;
         AvgBears[rates_total-i]=0;
         SmthBulls[rates_total-i]=0;
         SmthBears[rates_total-i]=0;

        }
     }
   if(counted_bars>0) limit=rates_total-counted_bars;
   limit--;

   CopyBuffer(imaPriceHandle,0,0,rates_total+1000,imaPriceBuffer);

   for(shift=limit;shift>=0;shift--)
     {
      Price1 = imaPriceBuffer[shift];
      Price2 = imaPriceBuffer[shift+1];
      if(Mode==0)
        {
         Bulls[shift]=0.5*(MathAbs(Price1-Price2)+(Price1-Price2));
         Bears[shift]=0.5*(MathAbs(Price1-Price2)-(Price1-Price2));
        }
      if(Mode==1)
        {
         smax=high[Highest(NULL,0,MODE_HIGH,Length,shift)];
         smin=low[Lowest(NULL,0,MODE_LOW,Length,shift)];
         Bulls[shift]=Price1 - smin;
         Bears[shift]=smax - Price1;
        }
     }
   for(shift=limit; shift>=0; shift--)
     {
      AvgBulls[shift]=iMAOnArrayMQL4(Bulls,0,Length,0,MODE_SMMA,shift);
      AvgBears[shift]=iMAOnArrayMQL4(Bears,0,Length,0,MODE_SMMA,shift);
     }
   for(shift=limit; shift>=0; shift--)
     {
      SmthBulls[shift]=iMAOnArrayMQL4(AvgBulls,0,Smooth,0,MODE_SMMA,shift);
      SmthBears[shift]=iMAOnArrayMQL4(AvgBears,0,Smooth,0,MODE_SMMA,shift);
     }
   for(shift=limit; shift>=0; shift--)
     {
      SigBulls[shift]=  SmthBulls[shift];
      SigBears[shift]=  SmthBears[shift];
      if(SmthBulls[shift]-SmthBears[shift]>0)
         SmthBears[shift]=0;
      else
         SmthBulls[shift]=0;
     }  //end for( shift=limit; shift>=0; shift--)

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iMAOnArrayMQL4(double &array[],
                      int total,
                      int period,
                      int ma_shift,
                      int ma_method,
                      int shift)
  {
   double buf[],arr[];
   if(total==0) total=ArraySize(array);
   if(total>0 && total<=period) return(0);
   if(shift>total-period-ma_shift) return(0);
   switch(ma_method)
     {
      case MODE_SMA :
        {
         total=ArrayCopy(arr,array,0,shift+ma_shift,period);
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0;
         int    i,pos=total-1;
         for(i=1;i<period;i++,pos--)
            sum+=arr[pos];
         while(pos>=0)
           {
            sum+=arr[pos];
            buf[pos]=sum/period;
            sum-=arr[pos+period-1];
            pos--;
           }
         return(buf[0]);
        }
      case MODE_EMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double pr=2.0/(period+1);
         int    pos=total-2;
         while(pos>=0)
           {
            if(pos==total-2) buf[pos+1]=array[pos+1];
            buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
            pos--;
           }
         return(buf[shift+ma_shift]);
        }
      case MODE_SMMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0;
         int    i,k,pos;
         pos=total-period;
         while(pos>=0)
           {
            if(pos==total-period)
              {
               for(i=0,k=pos;i<period;i++,k++)
                 {
                  sum+=array[k];
                  buf[k]=0;
                 }
              }
            else sum=buf[pos+1]*(period-1)+array[pos];
            buf[pos]=sum/period;
            pos--;
           }
         return(buf[shift+ma_shift]);
        }
      case MODE_LWMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0.0,lsum=0.0;
         double price;
         int    i,weight=0,pos=total-1;
         for(i=1;i<=period;i++,pos--)
           {
            price=array[pos];
            sum+=price*i;
            lsum+=price;
            weight+=i;
           }
         pos++;
         i=pos+period;
         while(pos>=0)
           {
            buf[pos]=sum/weight;
            if(pos==0) break;
            pos--;
            i--;
            price=array[pos];
            sum=sum-lsum+price*period;
            lsum-=array[i];
            lsum+=price;
           }
         return(buf[shift+ma_shift]);
        }
      default: return(0);
     }
   return(0);
  }
//+------------------------------------------------------------------+

Thank you in advance!

PS

 
Pedro Severin:

Hello everyone!

I'm trying to translate (without much success) this indicator from MQL4 to MQL5. The code compiles, but there is no plot at all. What I'm missing?


MQL4 code:

MQL5 code:

Thank you in advance!

PS

If I was you, I will place an order in FreeLance, it won't cost more than 30 bucks and you won't have to share your code with everyone. 
 
Mafuta Landou:
If I was you, I will place an order in FreeLance, it won't cost more than 30 bucks and you won't have to share your code with everyone. 

Thank you. The thing is that its not a code I made originally, it can be found on the internet so I have no problem sharing it :).