mql4 to mql5

 

i tried to convert this indicator code from mql4 to mql5. mql5 code shows nothing. what is mql5 code problem?

mql4 code:

//+------------------------------------------------------------------+
//|                                                   WelchHMABB.mq4 |
//|                                  Copyright © 2011, Timothy Welch |
//|                                     http://www.timothy-welch.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Timothy Welch"
#property link      "http://www.timothy-welch.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_width1 4
#property indicator_color2 Yellow
#property indicator_width2 4
#property indicator_color3 Red
#property indicator_width3 4
#property indicator_color4 DodgerBlue
#property indicator_width4 2



#property  indicator_minimum 0
#property  indicator_maximum 300

//---- indicator parameters
extern int     BandsPeriod=20;
extern int     BandsShift=0;
extern double  BandsDeviations=2.0;
extern int     MinRangePercent=20;
extern string  Note1="How far back to look for Max/Min width";
extern int     WidthCalcPeriod=100;
extern string  Note2="If TRUE, shows the the Width on a line chart";
extern bool    ShowWidthLine=false;

//---- buffers
double devBuffer[];
double squishyBuffer1[];
double squishyBuffer2[];
double squishyBuffer3[];

double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];

double dVal=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(7);
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,squishyBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,squishyBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,squishyBuffer3);

   if(ShowWidthLine)
     {
      SetIndexStyle(3,DRAW_LINE);
        } else {
      SetIndexStyle(3,DRAW_NONE);
     }
   SetIndexBuffer(3,devBuffer);
   SetIndexBuffer(4,MovingBuffer);
   SetIndexBuffer(5,UpperBuffer);
   SetIndexBuffer(6,LowerBuffer);
//----
   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----
// If we have 5 digits, or 4 digits and the symbol is a JPY currency, then dVal is 10.
   if(Digits==5 || (Digits==4 && StringFind(Symbol(),"JPY",0)>0)) { dVal=10; }

   if(BandsPeriod==0) { BandsPeriod=20; }
   if(MinRangePercent==0) { MinRangePercent=10; }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=BandsPeriod) return(0);
//----
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+BandsPeriod;
//----
   int    i,k;
   double deviation;
   double sum,oldval,newres;
//----
   for(i=0; i<limit; i++)
      MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
//----
   i=limit;
   while(i>=0)
     {
      sum=0.0;
      k=i+BandsPeriod-1;
      oldval=MovingBuffer[i];
      while(k>=i)
        {
         newres=Close[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;
      devBuffer[i]=MathAbs(UpperBuffer[i]-LowerBuffer[i])/Point/dVal;

      int tMaxIdx = ArrayMaximum(devBuffer, WidthCalcPeriod, i);
      int tMinIdx = ArrayMinimum(devBuffer, WidthCalcPeriod, i);

      // This will give us the percentage of closeness compared to each of tMin and tMax.
      // If we are < MinRangePercent away from the tMin value, we are considered in a rangebound market.
      // i.e. tMin=30, tMax=150, devBuffer[0]=42.  In that case, tLow=10, tHigh=90. tLow <15% from tMin
      double tLow=(MathAbs(devBuffer[tMinIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx]))*100;
      double tHigh=(MathAbs(devBuffer[tMaxIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx]))*100;

      if(tLow<tHigh && tLow<=MinRangePercent)
        {
         squishyBuffer1[i]=300; // Ranging
           } else if(tLow<tHigh && tLow<=(MinRangePercent*2)) {
         squishyBuffer2[i]=300; // Start/End of Range
           } else {
         squishyBuffer3[i]=300; // Not ranging
        }
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

converted mql5 code:

//+------------------------------------------------------------------+
//|                                                   WelchHMABB.mq4 |
//|                                  Copyright © 2011, Timothy Welch |
//|                                     http://www.timothy-welch.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Timothy Welch"
#property link      "http://www.timothy-welch.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_width1 4
#property indicator_color2 Yellow
#property indicator_width2 4
#property indicator_color3 Red
#property indicator_width3 4
#property indicator_color4 DodgerBlue
#property indicator_width4 2



#property  indicator_minimum 0
#property  indicator_maximum 300

//---- indicator parameters
input int     BandsPeriod=20;
input int     BandsShift=0;
input double  BandsDeviations=2.0;
input int     MinRangePercent=20;
input string  Note1="How far back to look for Max/Min width";
input int     WidthCalcPeriod=100;
input string  Note2="If TRUE, shows the the Width on a line chart";
input bool    ShowWidthLine=false;

//---- buffers
double devBuffer[];
double squishyBuffer1[];
double squishyBuffer2[];
double squishyBuffer3[];

double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];

double dVal=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //IndicatorBuffers(7);
//---- indicators
   SetIndexBuffer(0,squishyBuffer1);
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);
   SetIndexBuffer(1,squishyBuffer2);
   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);
   SetIndexBuffer(2,squishyBuffer3);
   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);
   

   if(ShowWidthLine)
     {
      PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_LINE);
        } else {
      PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_NONE);
     }
   SetIndexBuffer(3,devBuffer);
   SetIndexBuffer(4,MovingBuffer);
   SetIndexBuffer(5,UpperBuffer);
   SetIndexBuffer(6,LowerBuffer);
//----
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,BandsPeriod+BandsShift);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,BandsPeriod+BandsShift);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,BandsPeriod+BandsShift);
  
//----
// If we have 5 digits, or 4 digits and the symbol is a JPY currency, then dVal is 10.
   if(_Digits==5 || (_Digits==4 && StringFind(Symbol(),"JPY",0)>0)) { dVal=10; }

 //  if(BandsPeriod==0) { BandsPeriod=20; }
  // if(MinRangePercent==0) { MinRangePercent=10; }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
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[])
   {
   if(Bars(_Symbol,PERIOD_CURRENT)<=BandsPeriod) return(0);
//----
   int counted_bars=BarsCalculated(PRICE_CLOSE);
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars(_Symbol,PERIOD_CURRENT)-counted_bars;
   if(counted_bars==0) limit-=1+BandsPeriod;
//----
   int    i,k;
   double deviation;
   double sum,oldval,newres;
//----
   for(i=0; i<limit; i++)
      MovingBuffer[i]= iMA(_Symbol,PERIOD_CURRENT,i,BandsShift,MODE_SMA,PRICE_CLOSE);
//----
   i=limit;
   while(i>=0)
     {
      sum=0.0;
      k=i+BandsPeriod-1;
      oldval=MovingBuffer[i];
      while(k>=i)
        {
         newres=iClose(_Symbol,PERIOD_CURRENT,k)-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;
      devBuffer[i]=MathAbs(UpperBuffer[i]-LowerBuffer[i])/_Point/dVal;

      int tMaxIdx = ArrayMaximum(devBuffer, WidthCalcPeriod, i);
      int tMinIdx = ArrayMinimum(devBuffer, WidthCalcPeriod, i);

      // This will give us the percentage of closeness compared to each of tMin and tMax.
      // If we are < MinRangePercent away from the tMin value, we are considered in a rangebound market.
      // i.e. tMin=30, tMax=150, devBuffer[0]=42.  In that case, tLow=10, tHigh=90. tLow <15% from tMin
      double tLow=(MathAbs(devBuffer[tMinIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx]))*100;
      double tHigh=(MathAbs(devBuffer[tMaxIdx]-devBuffer[i])/MathAbs(devBuffer[tMinIdx]-devBuffer[tMaxIdx]))*100;

      if(tLow<tHigh && tLow<=MinRangePercent)
        {
         squishyBuffer1[i]=300; // Ranging
           } else if(tLow<tHigh && tLow<=(MinRangePercent*2)) {
         squishyBuffer2[i]=300; // Start/End of Range
           } else {
         squishyBuffer3[i]=300; // Not ranging
        }
      i--;
     }
//----
   return(0);
   
  }
//+------------------------------------------------------------------+
 
tom standly:

i tried to convert this indicator code from mql4 to mql5. mql5 code shows nothing. what is mql5 code problem?

mql4 code:

converted mql5 code:

I dont know, you think someone is looking thru the Code? Tell uns the Problem is you didnt use Chat gpt

 
in mql5 you need the indicator_plots property, it is missing in your conversion 
 
Conor Mcnamara #:
in mql5 you need the indicator_plots property, it is missing in your conversion 
That and you should also know that there are different drawing types in MQL5. For example some of the things that were done with multiple DRAW_HISTOGRAM plots in MQL4 are now done with DRAW_HISTOGRAM_2. 

It is highly recommended that you look this up, in documentation but also there is a very basic article about the new MQL5 drawing styles.

Last thing did you know you can create indicators by means of mql wizard ready with the plot and buffers you need, then you just have to implement the calculation.
 
tom standly:

i tried to convert this indicator code from mql4 to mql5. mql5 code shows nothing. what is mql5 code problem?

mql4 code:

converted mql5 code:

Convert MT4 Indicators to MT5 (mt4converter.com)

Don't use converters or Chat GPT, we can see if a code is hand written or if it was AI..If you post a piece of code written by AI we wont be able to help you. Here is a link on how to convert mt4 to 5

Convert MT4 Indicators to MT5
  • mt4converter.com
We will show you how you can upgrade & convert your existing MT4 (MQL4) indicators to be used on the latest MT5 (MQL5) trading platform.