Can you help convert this MT4 indicator to MT5, please?

 
Please see the mq4 file attached below.
 
Bob:
Please see the mq4 file attached below.

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.

What is good about your indicator?

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.07.04
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
You can convert your indicator yourself:
  1. Open the indicator file in your preferred code editor.
  2. Change the file extension from ".mq4" to ".mq5".
  3. Update the copyright information at the top of the file to reflect the conversion to MT5.
  4. Replace the following MQL4 functions with their MQL5 equivalents:
    • OnInit()    OnInitialize()
    • OnCalculate()    OnCalculate(const int total, const int passed, 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[])
    • iHigh()    iHighest()
    • iLow()    iLowest()
  5. Update the ObjectCreate and ObjectSet functions to use the MQL5 syntax. For example:
    • ObjectCreate("HGrid_txt", OBJ_LABEL, 0, 0, 0);    ObjectCreate(chart_ID, "HGrid_txt", OBJ_LABEL, 0, 0, 0);
    • ObjectSet("HGrid_txt", OBJPROP_CORNER, Font_Corner);    ObjectSetInteger(chart_ID, "HGrid_txt", OBJPROP_CORNER, Font_Corner);
  6. Replace the  CurTime()  function with  TimeCurrent() .
  7. Update the  IndicatorCounted()  function to  counted_bars = prev_calculated; .
  8. Save the file with the ".mq5" extension.
  9. Compile.
Then your indicator should work in MT5 with the same functionality as in MT4. 
 
Oleksandr Medviediev #:

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.

What is good about your indicator?

I've personally used this indicator to set the length of the price change from opening to closing so it's easier for me to look at this price value easily, for example, for XAU/USD it changes $15 so I set the grid value to align with the change value. I don't know if this's interesting for the forum members or not, I hope it is if the members expect for an indicator like I said. Thanks, Oleksandr Medviediev.    

 
Oleksandr Medviediev #:
You can convert your indicator yourself:
  1. Open the indicator file in your preferred code editor.
  2. Change the file extension from ".mq4" to ".mq5".
  3. Update the copyright information at the top of the file to reflect the conversion to MT5.
  4. Replace the following MQL4 functions with their MQL5 equivalents:
    • OnInit()  →  OnInitialize()
    • OnCalculate()    OnCalculate(const int total, const int passed, 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[])
    • iHigh()  →  iHighest()
    • iLow()  →  iLowest()
  5. Update the ObjectCreate and ObjectSet functions to use the MQL5 syntax. For example:
    • ObjectCreate("HGrid_txt", OBJ_LABEL, 0, 0, 0);    ObjectCreate(chart_ID, "HGrid_txt", OBJ_LABEL, 0, 0, 0);
    • ObjectSet("HGrid_txt", OBJPROP_CORNER, Font_Corner);    ObjectSetInteger(chart_ID, "HGrid_txt", OBJPROP_CORNER, Font_Corner);
  6. Replace the  CurTime()  function with  TimeCurrent() .
  7. Update the  IndicatorCounted()  function to  counted_bars = prev_calculated; .
  8. Save the file with the ".mq5" extension.
  9. Compile.
Then your indicator should work in MT5 with the same functionality as in MT4. 

This is very interesting. Have you tried following your advice yourself?

 
Oleksandr Medviediev #:
You can convert your indicator yourself:
  1. Open the indicator file in your preferred code editor.
  2. Change the file extension from ".mq4" to ".mq5".
  3. Update the copyright information at the top of the file to reflect the conversion to MT5.
  4. Replace the following MQL4 functions with their MQL5 equivalents:
    • OnInit()    OnInitialize()
    • OnCalculate()    OnCalculate(const int total, const int passed, 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[])
    • iHigh()    iHighest()
    • iLow()    iLowest()
  5. Update the ObjectCreate and ObjectSet functions to use the MQL5 syntax. For example:
    • ObjectCreate("HGrid_txt", OBJ_LABEL, 0, 0, 0);    ObjectCreate(chart_ID, "HGrid_txt", OBJ_LABEL, 0, 0, 0);
    • ObjectSet("HGrid_txt", OBJPROP_CORNER, Font_Corner);    ObjectSetInteger(chart_ID, "HGrid_txt", OBJPROP_CORNER, Font_Corner);
  6. Replace the  CurTime()  function with  TimeCurrent() .
  7. Update the  IndicatorCounted()  function to  counted_bars = prev_calculated; .
  8. Save the file with the ".mq5" extension.
  9. Compile.
Then your indicator should work in MT5 with the same functionality as in MT4. 

I did it following your guidance carefully but it doesn't work and shows 91 errors, 21 warnings and I don't know how to correct the errors as I have no coding knowledge at all. So what I tried to do is Find & Replace by finding the functions where you told me to replace.
Please see the edited file below and hope you help correct:

//+------------------------------------------------------------------+

//|                                        Horizontal_Grid_Lines.mq5 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//| Edits and Improvements: File45                                   |

//| https://www.mql5.com/en/users/file45/publications                |

//+------------------------------------------------------------------+

#property copyright "Copyright 2015, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "3.00"

#property strict

#property indicator_chart_window

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

enum LW

  {

   One = 1,

   Two = 2,

   Three= 3,

   Four = 4,

   Five = 5,

  };


// DEFAULT INPUTS : START


input int   HGrid_Weeks = 10;            // Weeks 

input double HGrid_Pips = 15.0;          // H-Grid Size  

input color HLine_2=MidnightBlue;    // H-Grid 1 Round Number Color

input LW Line_2_Width=1;               // H-Grid 1 Width

input ENUM_LINE_STYLE Line_2_Style=STYLE_DOT;  // H-Grid 1 Style

input color HLine_1=MidnightBlue;    // H-Grid 2 Non Round Number Color

input LW Line_1_Width=1;               // H-Grid 2 Width

input ENUM_LINE_STYLE Line_1_Style=STYLE_DOT;  // H-Grid 2 Style


input string H_Grid="hg";  // Label Text 

input bool Show_Label=true;            // Label Visible

input color Font_Color=DodgerBlue;  // Label Color

input int Font_Size = 11;                // Font Size

input bool Font_Bold = false;            // Font Bold

input int Font_Corner = 1;               // Corner

input int Left_Right = 25;               // Left - Right

input int Up_Down = 190;                 // Up - Down


// DEFAULT INPUTS : END


string HGrid_text;

string FB;


bool firstTime=true;

datetime lastTime=0;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInitialize()

  {

   firstTime=true;


   switch(Font_Bold)

     {

      case 0 : FB = "Arial"; break;

      case 1 : FB = "Arial Bold"; break;

     }

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate

         (const int total,

         const int passed, 

         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[])

  {

   ObjectCreate(chart_ID, "HGrid_txt", OBJ_LABEL, 0, 0, 0;

   ObjectSetInteger(chart_ID, "HGrid_txt", OBJPROP_CORNER, Font_Corner);

   ObjectSet("HGrid_txt",OBJPROP_XDISTANCE,Left_Right);

   ObjectSet("HGrid_txt",OBJPROP_YDISTANCE,Up_Down);

   if(Show_Label==1)

     {

      ObjectSetText("HGrid_txt",H_Grid+": "+DoubleToString(HGrid_Pips,1),Font_Size,FB,Font_Color);

     }

   else

     {

      ObjectSetText("HGrid_txt","");

     }


   int    counted_bars = prev_calculated;


   if(true /*lastTime == 0 || TimeCurrent() - lastTime > 5*/)

     {

      firstTime= false;

      lastTime = TimeCurrent();


      if(HGrid_Weeks>0 && HGrid_Pips*10>0)

        {

         double weekH = iHighest( NULL, PERIOD_W1, 0 );

         double weekL = iLowest( NULL, PERIOD_W1, 0 );


         for(int i=1; i<HGrid_Weeks; i++)

           {

            weekH = MathMax( weekH, iHighest( NULL, PERIOD_W1, i ) );

            weekL = MathMin( weekL, iLowest( NULL, PERIOD_W1, i ) );

           }


         double pipRange=HGrid_Pips *10*Point;

         if(Symbol()=="GOLD" || Symbol()=="XAUUSD")

           {

            pipRange=pipRange*10.0;

           }


         double topPips = (weekH + pipRange) - MathMod( weekH, pipRange );

         double botPips = weekL - MathMod( weekL, pipRange );


         for(double p=botPips; p<=topPips; p+=pipRange)

           {

            string gridname="grid_"+DoubleToStr(p,Digits);

            ObjectCreate(gridname,OBJ_HLINE,0,0,p);


            double pp=p/Point;

            //int pInt = MathRound(pp);

            int pInt=StrToInteger(DoubleToStr(pp,0));

            int mod=100;

            if(Symbol()=="GOLD" || Symbol()=="XAUUSD")

               mod=1000;

            if((pInt%mod)==0)

              {

               ObjectSet(gridname,OBJPROP_COLOR,HLine_2);

               ObjectSet(gridname,OBJPROP_STYLE,Line_2_Style);

               ObjectSet(gridname,OBJPROP_WIDTH,Line_2_Width);

              }

            else

              {

               ObjectSet(gridname,OBJPROP_COLOR,HLine_1);

               ObjectSet(gridname,OBJPROP_STYLE,Line_1_Style);

               ObjectSet(gridname,OBJPROP_WIDTH,Line_1_Width);

               ObjectSet(gridname,OBJPROP_PRICE1,p);

               ObjectSet(gridname,OBJPROP_BACK,true);

              }

           }

        }

     }

   return(rates_total);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   for(int i=ObjectsTotal()-1; i>=0; i--)

     {

      string name=ObjectName(i);

      if(StringFind(name,"grid_")>=0)

         ObjectDelete(name);

     }

   ObjectDelete("HGrid_txt");


   return;

  }

//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.07.11
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions