Download MetaTrader 5

TimeCurrent() and iTime() bug/problem

To add comments, please log in or register
Find out how to install MetaTrader on Linux
Paul B
175
Paul B 2013.12.12 08:59 

Hi guys,

Just come across an interesting problem/bug and not sure if it's been identified before or even if it's broker specific.

Try using the following in some indicator code, have the indicator running at startup and note the values when you first run MT4 (make sure MT4 hasn't been running for at least 10 mins prior to restarting).

Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));
Print(TimeToStr(iTime(NULL,PERIOD_M1,0),TIME_DATE|TIME_MINUTES));

You will note the times returned are actually the time that MT4 was last running.

In order to get round this I've had to introduce a delay in my code, which is not ideal for an indicator.

Was wondering if anyone has any more eloquent solutions. I would ideally wan't the correct MT4 server time returned at startup each time.

Regards

PaulB

Tjipke de Vries
6315
Tjipke de Vries 2013.12.12 09:06  
Paul_B:

Hi guys,

Just come across an interesting problem/bug and not sure if it's been identified before or even if it's broker specific.

Try using the following in some indicator code, have the indicator running at startup and note the values when you first run MT4 (make sure MT4 hasn't been running for at least 10 mins prior to restarting).

You will note the times returned are actually the time that MT4 was last running.

In order to get round this I've had to introduce a delay in my code, which is not ideal for an indicator.

Was wondering if anyone has any more eloquent solutions. I would ideally wan't the correct MT4 server time returned at startup each time.

Regards

PaulB


This ??
Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
Paul B
175
Paul B 2013.12.12 09:19  

Hi deVries,

you could use TIME_SECONDS too, doesn't really matter, as long as the code is in init{} or at the beginning of start{}.

The point is, you get the last time MT4 was running, which could potentially be hours or days ago.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.12 10:02  
Paul_B:

Hi guys,

Just come across an interesting problem/bug and not sure if it's been identified before or even if it's broker specific.

Try using the following in some indicator code, have the indicator running at startup and note the values when you first run MT4 (make sure MT4 hasn't been running for at least 10 mins prior to restarting).

You will note the times returned are actually the time that MT4 was last running.

In order to get round this I've had to introduce a delay in my code, which is not ideal for an indicator.

Was wondering if anyone has any more eloquent solutions. I would ideally wan't the correct MT4 server time returned at startup each time.

Regards

PaulB

Isn't the documentation 100% clear on this point ? "Returns the last known server time (time of incoming of the latest quote) as number of seconds elapsed from 00:00 January 1, 1970." if the last tick was 10 minutes ago then TimeCurrent() will return a datetime based on that time . . .
cyclops993
115
cyclops993 2013.12.12 10:32  
RaptorUK:
Isn't the documentation 100% clear on this point ?

Not really, no it isn't.

There's an important distinction between EAs and indicators which isn't covered in the documentation: EAs only get a call to start() if there's a broker connection and a new tick, whereas indicators always get an initial call to start() before or without a broker connection being established. Therefore, TimeCurrent() can have different meanings in EAs and indicators.

If you want a definitely-refreshed value for TimeCurrent() in an indicator, then you can probably use IsConnected() to check whether TimeCurrent() may refer to the last-known time in a previous session.

Paul B
175
Paul B 2013.12.12 11:39  

As an additional point, I've even tried

https://docs.mql4.com/windows/RefreshRates

before calling TimeCurrent() and it made no difference. It's certainly something to keep in mind.

I'm using it as part of some code to get the offset between MT4 server time and GMT, and it's throwing subsequent calculations out.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.12 12:34  
cyclops993:

Not really, no it isn't.

There's an important distinction between EAs and indicators which isn't covered in the documentation: EAs only get a call to start() if there's a broker connection and a new tick, whereas indicators always get an initial call to start() before or without a broker connection being established. Therefore, TimeCurrent() can have different meanings in EAs and indicators.

I don't see that as being relevant bearing in mind what the documentation says . . . if you check TimeCurrent() before a new tick arrives you get the time of the last tick. Isn't that what the documentation says ?
cyclops993
115
cyclops993 2013.12.12 12:42  
RaptorUK:
I don't see that as being relevant bearing in mind what the documentation says . . . if you check TimeCurrent() before a new tick arrives you get the time of the last tick. Isn't that what the documentation says ?

It's still misleading, but if you wanted to be pedantic you could say that the problem is not in the definition of TimeCurrent() but instead in the definition at https://docs.mql4.com/runtime/start. That says "At incoming of new quotes, the start() function of the attached experts and custom indicators will be executed". There's a clear implication that start() is only called in relation to new ticks, as is the case with EAs, and that, implicitly, TimeCurrent() therefore cannot return the time of the last tick in a previous session of using the MT4 software.

[Again, one answer to all this is to ignore calls to start() in an indicator if IsConnected() returns false.]

cyclops993
115
cyclops993 2013.12.12 12:46  
cyclops993:

It's still misleading, but if you wanted to be pedantic [...]

Being even more pedantic, if you have a brand new MT4 installation, and copy into it a chart file with an indicator attached before starting MT4 for the first time, then presumably the indicator would get a call to start() with TimeCurrent() reported as 1/1/1970. Therefore, the documentation of TimeCurrent() ought to say "...the last known server time, or 1/1/1970 if there has been no server connection".

Alain Verleyen
Moderator
30724
Alain Verleyen 2013.12.12 12:51  
cyclops993:

Being even more pedantic, if you have a brand new MT4 installation, and copy into it a chart file with an indicator attached before starting MT4 for the first time, then presumably the indicator would get a call to start() with TimeCurrent() reported as 1/1/1970. Therefore, the documentation of TimeCurrent() ought to say "...the last known server time, or 1/1/1970 if there has been no server connection".

No need to made a fuss. Just wait the first/next tick, TimeCurrent() is updated and all is ok. An indicator on history prices only is useless.
Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.12 13:07  
angevoyageur:
No need to made a fuss. Just wait the first/next tick, TimeCurrent() is updated and all is ok. An indicator on history prices only is useless.
I think the issue is that the first time start() is called for an Indicator may not be as a result of a new tick . . . in that case TimeCurrent() will report the incorrect time. I haven't verified this . . .

In this case I would simply ignore the first tick . . . and continue normally from tick 2 onwards.
1234
To add comments, please log in or register