time between two ticks or accurate time in MQL4 test mode

Threepwood
28
Threepwood  

Hi everyone,

I had a MQL4 strategy based on the ticks but unfortunately I needed the duration between two ticks.

I thought that GetTickCount() did the job, but I have just understood the GetTickCount relies on the computer time, so at each run of the same test in the same interval of course I don't get the same times...

So all my strategy is to throw away...


Would you see or indicate to me a trick to get the time between two ticks in Test mode ?

Thanks a lot in advance !!!!


[EDIT] I was thinking about working at seconds instead of tick time using the TimeCurrent() function that's obviously accurate, the algorithm would be as follows:

at OnTick() event:

wait until a TimeCurrent() second changes as a start,

if the "seconds" value of TimeCurrent() has changed, do my job related on the N seconds elapsed...

What do you think of that? Do you think this is a good way to get a sufficient accuracy (1 second)?

Second question: how can I get the "seconds" part of  TimeCurrent() ?

Thanks  !

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
  • www.mql5.com
A purchased licensed version allows at least 5 activations. The number of activations is specified by seller. Seller may increase the allowed number of activations
William Roeder
23771
William Roeder  

Threepwood:


[EDIT] I was thinking about working at seconds instead of tick time using the TimeCurrent() function that's obviously accurate, the algorithm would be as follows:

if the "seconds" value of TimeCurrent() has changed, do my job related on the N seconds elapsed...

What do you think of that? Do you think this is a good way to get a sufficient accuracy (1 second)?

Second question: how can I get the "seconds" part of  TimeCurrent() ?

  1. Think again. If there is no tick, nothing has changed. What are you going to do now that you didn't do on the previous tick? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific).

  2. If you insist, use the timer.

  3. int seconds = TimeCurrent() % 60;

Threepwood
28
Threepwood  
William Roeder:
  1. Think again. If there is no tick, nothing has changed. What are you going to do now that you didn't do on the previous tick? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific).

  2. If you insist, use the timer.


That makes perfectly sense, agree with that; I know exactly what to do now.

In the meantime I have based my strategy on TimeCurrent() function, checking whether more than 1 minute had elapsed since the last tick and in that case deduce the market were closed/the servers or internet connection were broken. I did not expect more than 1 minute between ticks in some cases as you mentioned, so I need to adjust (rewrite one part of) my code. Thanks a lot for this information.

Do you agree with the following condition: if more than 1h has elapsed since the previous tick => deduce the market was closed/other malfunction from servers or internet connection. I believe there's no way to have more than 1h?

Note: the timer function does not work in Test mode ;-)

lippmaje
1442
lippmaje  
void OnTick()
  {
   static datetime lasttime=0;
   datetime ticktime=TimeCurrent();
   int seconds=(int)(ticktime-lasttime);
   lasttime=ticktime;
   if(seconds>=3600)
     {
      return; // market was closed, or first tick on startup
     }
   Print("seconds elapsed since last tick: ",seconds);
  }
Note that in MT5 you can also get the milliseconds part of the tick time, see MqlTick and SymbolInfoTick().
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
  • www.mql5.com
This is a structure for storing the latest prices of the symbol. It is designed for fast retrieval of the most requested information about current prices. The parameters of each tick are filled in regardless of whether there are changes compared to the previous tick. Thus, it is possible to find out a...
Threepwood
28
Threepwood  
lippmaje:
Note that in MT5 you can also get the milliseconds part of the tick time, see MqlTick and SymbolInfoTick().

This is the kind of code I have implemented (actually yours does not work, and do not see how it could, but I get the big picture, which was the same as mine).

I understand you validate the fact that "if elpasted time > 1h: deduce the market was closed" :-)

Thanks a lot for your help!

lippmaje
1442
lippmaje  
This is the best you can get at with MT4 if you want 100% reproducible test conditions. If you want something that "works", in your terms, then move on to MT5. The "big" picture is that your strategy is not testable in MT4 if you need sub-second resolutions. Hope that's clear now.
Threepwood
28
Threepwood  
lippmaje:
This is the best you can get at with MT4 if you want 100% reproducible test conditions. If you want something that "works", in your terms, then move on to MT5. The "big" picture is that your strategy is not testable in MT4 if you need sub-second resolutions. Hope that's clear now.

This is perfectly clear, and it has already been clear from the beginning, I have never said the opposite.

Threepwood
28
Threepwood  
The trick
(int) TimeCurrent()
is brillant :-) It will help me a lot. Thanks!
lippmaje
1442
lippmaje  
Also, OnTick may be missing ticks. I don't know if it helps but I'd recommend to check if CopyTicks can be used to catch any missed out tick. It's MT5 only.
Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
  • www.mql5.com
[in]  The number of requested ticks. If the 'from' and 'count' parameters are not specified, all available recent ticks (but not more than 2000) will be written to ticks_array[]. The first call of CopyTicks() initiates synchronization of the symbol's tick database stored on the hard disk. If the local database does not provide all the requested...