Features of the mql5 language, subtleties and tricks - page 338

 
Ryan L Johnson #:
'"[R]eleased into the public domain" means total freedom─anytime, anywhere, and for everyone.

I understand that, and know about "public domain". Thank you.

So how do I understand this:


Maybe I should open a new thread for this discussion.
 
amrali #:

High-performance time-handling functions applicable to the entire MQL5 date and time range (from 1 January 1970 to 31 December 3000):

(Based on the Neri–Schneider calendar conversion algorithm: see here)

The attached script file contains the source code and comprehensive tests.

Implemented in build 6002, available on MetaQuotes-Demo

2026.07.14 00:14:42.467 StructToTimeFast (GBPUSD,H1)    Compiler Version: 6002, AVX2 + FMA3
2026.07.14 00:14:42.468 StructToTimeFast (GBPUSD,H1)    AMD RYZEN AI MAX+ 395 w/ Radeon 8060 S, AVX512 + FMA3
2026.07.14 00:14:42.620 StructToTimeFast (GBPUSD,H1)     1.03 ns, checksum = 323500114770647190  /// MQL's StructToTime()
2026.07.14 00:14:42.641 StructToTimeFast (GBPUSD,H1)     1.06 ns, checksum = 323500114770647190   // StructToTimeFast


2026.07.14 00:17:37.406	TimeToStructFast (GBPUSD,H1)	1970.01.01 00:23:41 - 2999.12.31 23:28:37
2026.07.14 00:17:37.538	TimeToStructFast (GBPUSD,H1)	 6.57 ns, checksum = 6236374825958545665  /// MQL's TimeToStruct()
2026.07.14 00:17:37.582	TimeToStructFast (GBPUSD,H1)	 2.19 ns, checksum = 6236374825958545665   // TimeToStructFast

We even managed to beat it in the first instance.

Thanks for the example!

 
Renat Fatkhullin #:

Implemented in build 6002, available on MetaQuotes-Demo

We even overtook them in the first instance.

Thanks for the example!

Impressive!

Thank you.

 
Renat Fatkhullin #:
Implemented in build 6002
It’s about time they did something about iBarShift(...) – it’s a frequently used function.
 
Renat Fatkhullin # :

Implemented in build 6002, available on MetaQuotes-Demo

They even overtook us in the first instance.

Thanks for the example!

My TimeToStruct is faster!

I use unsigned types to optimise division and modulo operations as much as possible (here).

Also, I calculate the modulo time using subtraction:

   uint sec = (uint)(time - n * 86400 ULL);
 
Dominik Egert #:

I understand that, and know about "public domain". Thank you.

So how do I understand this:


Maybe I should open a new thread for this discussion.

The on-point language is:

A limited, non-exclusive permission is granted to:

    • Use, copy and modify the Software for personal use, whether commercial or non-commercial, without prior authorization.

There is a principle of legal interpretation, expressio unius est exclusio alterius, meaning that the expression of one thing is the exclusion of another. Therefore, the complete waiver that you have already received directly from the creator of the work authorizes you to do all things personal and commercial. This is the case even though the method of authorization is not discussed in that document. Again, "public domain" thermonuclearly destroyed all IP protection─including that of the Algo Forge.

 
amrali #:

My TimeToStruct is faster!

I use unsigned types to optimise division and modulo operations as much as possible (here).

Also, I calculate the modulo time using subtraction:

Yes, but your version does not check the value and always returns true.

Furthermore, the test produces random time values.
I think it is more likely that the values will be sequential.

If we add a parameter check to TimeToStructFast and modify the test code,
bool TimeToStructFast(datetime time, MqlDateTime& dt_struct)
  {
   if(time<0 || time>=91674063532800)
      return(false);
...
   datetime t[];
   ArrayResize(t, BENCHSIZE);
   for(int i = 0;i<BENCHSIZE;i++) {
      t[i] = i;
   }

the result on the 6002 would be as follows
2026.07.14 11:40:34.104 TimeToStructFast (EURUSD,H1)    Compiler Version: 6002, AVX2 + FMA3
2026.07.14 11:40:34.104 TimeToStructFast (EURUSD,H1)    AMD Ryzen 9 7950 X 16-Core, AVX512 + FMA3
2026.07.14 11:40:34.142 TimeToStructFast (EURUSD,H1)    1970.01.01 00:00:00 - 1970.08.20 11:33:19
2026.07.14 11:40:34.206 TimeToStructFast (EURUSD,H1)     3.19 ns, checksum = 4186118827073392896  /// MQL's TimeToStruct()
2026.07.14 11:40:34.315 TimeToStructFast (EURUSD,H1)     5.43 ns, checksum = 4186118827073392896   // TimeToStructFast

I believe it would be more appropriate to amend the TimeToStruct contract
void  TimeToStruct(datetime time, MqlDateTime& st);
and add a separate function to validate the time.
bool IsValidTimeValue(datetime time);
Since only the developer (the user) knows the quality of the source data and the need to validate the values.
 
Ilyas #:
Yes, but your version does not check the value and always returns true.

1. Nice to know that the numbers were obtained with parameter check active. The C++ version should be faster, anyway.

2. I agree that the parameter check inside the function is unnecessary step. No body checks for the return value of TimeToStruct as the source of input is the terminal. IsValidTime() is a very reasonable choice. 

3. The boost in performance (esp., in the strategy tester) is impressive with these optimized functions . Thank you.
 

A forum on trading, automated trading systems and the testing of trading strategies

Features of the MQL5 language, nuances and techniques

Ilyas, 14 July 2026 08:54

bool TimeToStructFast(datetime time, MqlDateTime& dt_struct)
  {
   if(time<0 || time>=91674063532800)
      return(false);

Why isn’t that right?

if ((ulong)time >= 9167406353280)
 
amrali #:
3. The increase in performance (particularly in the strategy tester) thanks to these optimised features is impressive. Thank you.
For the Strategy Tester, a fast `TimeToStruct` function should remember the values from the previous call. That should make it even faster.