Different time between iTime() and TimeCurrent()

 

Hello,

I have a case :

Here is mycode

datetime hour_now=iTime(Symbol(),PERIOD_H1,0);
if(timer!=hour_now)
 {
  Print("Hour now = ",TimeToStr(hour_now),", Current Time = ",TimeToStr(TimeCurrent()));
  timer=iTime(Symbol(),PERIOD_H1,0);
 }

Then at 00:00 I have a result :

Hour now = 2019.10.29 00:00, Current Time = 2019.10.29 02:00

How come they have 2 hours difference at the same time?

This happened at 00:00 only. At the other hours, working good.

Anyone can help me?

Thank you

Documentation on MQL5: Date and Time / TimeCurrent
Documentation on MQL5: Date and Time / TimeCurrent
  • www.mql5.com
Returns the last known server time, time of the last quote receipt for one of the symbols selected in the "Market Watch" window. In the OnTick() handler, this function returns the time of the received handled tick. In other cases (for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on) this...
 

iTime is the start time of the reference candle. In your case (H1/0,) top of the hour. After the start of a new candle, TimeCurrent will be increasing.

There should never be a 2 hours difference. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

 
William Roeder:

iTime is the start time of the reference candle. In your case (H1/0,) top of the hour. After the start of a new candle, TimeCurrent will be increasing.

There should never be a 2 hours difference. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

Thank you for your answer.

But why it (the error) happen only at 00:00? While at the other hours it is working normally. So at that time the iTime and TimeCurrent have (or print) the same time exactly.

 

Hello @William Roeder,

I use the code in the link above, but it still have the same problem.

#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
#define SYMBOL string

datetime timer=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetMillisecondTimer(500);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(IsTesting())
      OnTimer();
  }
//+------------------------------------------------------------------+
void OnTimer()
  {
//Check history
   while(!download_history(PERIOD_H1))
     {
      Print("Failed to download history of Period H1");
      Sleep(1000);
      RefreshRates();
     }
//check next session
   datetime hour_now=iTime(Symbol(),PERIOD_H1,0);
   if(timer!=hour_now)
     {
      Print("Hour now = ",TimeToStr(hour_now),", Current Time = ",TimeToStr(TimeCurrent()));

      timer=hour_now;
     }
  }
//+------------------------------------------------------------------+
bool download_history(ENUM_TIMEFRAMES period=PERIOD_CURRENT)
  {
   return download_history(_Symbol, period);
  }
//+------------------------------------------------------------------+
bool download_history(SYMBOL symbol, ENUM_TIMEFRAMES period=PERIOD_CURRENT)
  {
   if(period == PERIOD_CURRENT)
      period = (ENUM_TIMEFRAMES)_Period;
   ResetLastError();
   datetime other = iTime(symbol, period, 0);
   if(_LastError == 0 && other != 0)
      return true;
   if(_LastError != ERR_HISTORY_WILL_UPDATED
      && _LastError != ERR_NO_HISTORY_DATA
     )
      PrintFormat("iTime(%s,%i) Failed: %i", symbol, period, _LastError);
   return false;
  }
//+------------------------------------------------------------------+

And I had a result like this :

When it is initialized,
Hour now = 2019.11.01 22:00, Current Time = 2019.11.03 22:22.

It means that when it print the journal, the Market Watch = 2019.11.03 22:22. So, the time for the H1 candle should be 2019.11.03 22:00, right? Then it has 2 days different.

 
Donald Reeves Sihombing:

Hello @William Roeder,

I use the code in the link above, but it still have the same problem.

And I had a result like this :

When it is initialized,
Hour now = 2019.11.01 22:00, Current Time = 2019.11.03 22:22.

It means that when it print the journal, the Market Watch = 2019.11.03 22:22. So, the time for the H1 candle should be 2019.11.03 22:00, right? Then it has 2 days different.

As you said TimeCurrent() is the Market Watch time, it concerns ALL symbols, and it is the time of the last received tick, it can be from an OTHER symbol. You are using a Timer, so you probably don't have a tick yet for your CHART symbol. iTime(0) is last candle, no tick, no new candle, so the difference.
 

Thank you @Alain Verleyen,

I think I will try to use OnTick and see the difference.

 

I still get the same result.

Looks like I have to find the other way.

 
Donald Reeves Sihombing:

I still get the same result.

Looks like I have to find the other way.

Show your code.

What broker and symbol are you using ?

 
Alain Verleyen:

Show your code.

What broker and symbol are you using ?

Hi @Alain Verleyen

It was my fault.

The problem is my Start Time and End Time.

I just realize that when I set the Start Time 00:01 --> it is based on my local computer time, and the fact is that the Market watch is 5 hours behind me.

This cause error for every new day (of my computer time), while the Market is still 1 day behind, that is why I missed the last 5 hours of the H1 candle.

I just found this yesterday and test it all the night and it is working good.

Thank you!

Reason: