Hour() in Minutes Error?

 

Hello, I am trying to Calculate the Index of the First Candle of the Day within the M1 Time Frame and I feel like I found an error in the MQL System; if anyone knows an explanation or a remedy for this, please let me know. 

int h = Hour(), //the Number of Hours that have passed since the Beginning of the Day 
m = Minute(), //the Number of Minutes that have passed since the Beginning of the Hour 
hourstart = m, //If Minute() is the Number of Minutes that have passed since the Beginning of the Hour, then the Index of the Start of the Hour == Minute() or m 
//Since Minute is based on 59 instead of 60, the Correct hourstart Index is m - 1: 
hourstart = m - 1; 

Now in theory, from hourstart I should be able to Calculate the amount of Minutes that have passed According to any amount of Hours by multiplying it by 60 (which is the Number of Minutes within a Day). 

//If: 
h = 23, //The Market is Closed Right Now; Hour() is based off 23 Hour Cycles instead of 24 so the Highest it can get is 23:59 which is being portrayed here; basically this is the last Hour of the Day 
m = 59, //The Last Minute of the Hour 
hourstart = 60; //(m - 1); In the Context of MQL Indices 

This Chart should show the Correct Values of (h *60); Note that these are based off off the Indices of MQL and the Conditions Above and not Pure Math: 


h y Time 
23 0 23:00 (hourstart)
22 60 22:00
21 120 21:00
20 180 20:00
19 240 19:00
18 300 18:00
17 360 17:00 
16 420 16:00 
15 480 15:00 
14 540 14:00 
13 600 13:00 
12 660 12:00
11 720 11:00
10 780 10:00
9 840 9:00
8 900 8:00
7 960 7:00
6 1020 6:00
5 1080 5:00
4 1140 4:00
3 1200 3:00
2 1260 2:00
1 1320 1:00
0  1380 0:00

 These Numbers start based off hourstart (which is 23:00). Now in theory, it should give me the Time in the Chart, but somewhere around where h == 16, it starts diverging from the Formula in Minutes. 

Here are the Numbers I got instead (note, it took a Minute to make this Chart XD): 

h y Time 
23 0 23:00 (hourstart)
22 60 22:00
21 120 21:00
20 180 20:00
19 240 19:00
18 300 18:00
17 360 17:00 
16 420 16:00 
15 480 15:00 
14 540 14:00 
13 600 13:00 
12 660 12:00
11 720 11:00
10 780 10:00
9 840 9:00
8 900 8:00
7 960 7:00
6 1020 6:00
5 ???  5:59
4 ???  4:59 
3 ???  3:59
2 ???  2:59
1 ???  1:58
0 ???  0:58
-1  ???  23:50 

 

Around the Points where h == 17, it switches to hh:59, then it devolves further from the Formula by going to 1:58 at x == 21, then it went to 00:58, and then finally at the Point I was trying to Calculate it went to 23:50 Randomly. 

Can someonet tell me how the Numbers got screwed up? Is it because of a few Candles missing? I would include an Offset for what I am trying to Calculate but I feel like it doesn't even stay at 23:50, sometimes it goes to 23:48 and such. 

Is it intentional? Is it an error? Am I missing something here? I'd really like to know because believe it or not this Conundrum caused me to halt my Forex studies for almost a Year because I felt like I was doing everything wrong since I couldn't get the Right Number, but I think it's not me but a technicality. 


 Thanks for you help and God Bless :D 

 

To test this yourself, put these three functions into a Program and substitute the variable n for h (h or Hour() can be Equal to any Number from 1 to 23). 

//Change n to act as variable h, which is a theoretical Hour(). 
//In other words, if the Hour() == n, then this would be the Starting Candle of that Hour. 
int n = 0; 

void PlotLine() 



{

   VLineCreate(0, "Theoretical Hourly Start", 0, Time[TheoHourLine(n)],255, 0, 2, 0, 0); 





} 

int TheoHourLine(int x = 1) 
{
   double h1start = Minute() - 1, h = Hour(); 
   
   
   return (h1start  + (60 * x)); 


}


//The Function below is just a pre-defined Function to allow V-Lines to be Plotted. 

bool VLineCreate(const long            chart_ID=0,        // chart's ID
                 const string          name="VLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 datetime              time=0,            // line time
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)         // priority for mouse click
  {
//--- if the line time is not set, draw it via the last bar
   if(!time)
      time=TimeCurrent();
//--- reset the error value
   ResetLastError();
//--- create a vertical line
   if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time,0))
     {
      Print(__FUNCTION__,
            ": failed to create a vertical line! Error code = ",GetLastError());
      return(false);
     }
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
 
  1. galfaria: Now in theory, from hourstart I should be able to Calculate the amount of Minutes that have passed According to

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (20 June 2006)
              No candle if open = close ? - MQL4 programming forum

    Use iBarShift.

  2. What are you trying to do?
    How To Ask Questions The Smart Way. (2004)
              The XY Problem

    datetime today=date();
    for(int hour=0; hour < 24; ++hour){
       datetime topH1 = today + hour * 3600;
       VLineCreate(0, "Actual Hourly Start", 0, topH1); 
    }
    
    date/time
 
Dealing with Time (Part 1): The Basics
Dealing with Time (Part 1): The Basics
  • www.mql5.com
Functions and code snippets that simplify and clarify the handling of time, broker offset, and the changes to summer or winter time. Accurate timing may be a crucial element in trading. At the current hour, is the stock exchange in London or New York already open or not yet open, when does the trading time for Forex trading start and end? For a trader who trades manually and live, this is not a big problem.