MQL4 Time function miscalculation

 

Hi all,

I have the following function where tries to calculate the time between the last closed trade and the current time. if the time difference is <> that a certain number of bars it gives a different result.

that being said, during the backtest at some point the bar calculation begins to fail and i do not find the error. Any thoughts?

int LastOrderTicket()
  {
   datetime TicketTime = 0;
   int LastOT;

   datetime
   Timeorder = LastOrderTime(),
   Timeahora = TimeCurrent(),
   Times = Timeahora - Timeorder;

   int
   m         = TimeMinute(Times),
   h         = TimeHour(Times),
   d         = TimeDay(Times),

   bars      = (((d - 1) * 24 * 60) + (h * 60) + m) / 240;

   

   for(int ix = 0; ix < OrdersHistoryTotal(); ix++)
     {
      if(OrderSelect(ix, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())
        {
           if(bars > barsback)
           {
            LastOT = -1;
           } 
       else
          { 
            if(bars  < barsback)
             {
            LastOT = OrderType();        //For testing
             }
           
        }
       } 

      else
         Print("OrderSelect returned the error of ",GetLastError());
     }

       Print("TicketN ",LastOT);
       Print("bars ",bars);
       Print("Timeorder=",TimeToStr(Timeorder, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
       Print("Timeahora=",TimeToStr(Timeahora, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
       Print("Times=",TimeToStr(Times, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
       Print("m ",m);
       Print("h ",h);
       Print("d ",d);

   return(LastOT); // Buy==0, Sell==1, Others==2 through 5
  }

Here where i find the error:

0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: bars 182
0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: Timeorder=2021.03.09 12:00:00
0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: Timeahora=2021.08.06 23:59:59
0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: Times=1970.05.31 11:59:59
0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: m 59
0   18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: h 11
**0 18:02:11.243    2021.08.06 23:59:59  BBReverse V1 EURUSD,H4: d 31**

0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: bars 8
0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: Timeorder=2021.03.09 12:00:00
0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: Timeahora=2021.08.08 23:05:00
0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: Times=1970.06.02 11:05:00
0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: m 5
0   18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: h 11
**0 18:02:11.243    2021.08.08 23:05:00  BBReverse V1 EURUSD,H4: d 2**
the function time keep summing up but the day value not...

thank you.

 
You assume 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 (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Use iBarShift.

 
William Roeder #:
You assume 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 (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Use iBarShift.

Thank you William. 

I will check it out. 

 

Willian, just to confirm that it worked! 

thank you for the tip!

Reason: