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

 

Not exactly on the topic, but decided to post here nevertheless.


In the Terminal Logs it works to search for a terminal line by its first letters, just by typing (ragistrophically) them on the keyboard.

This is not as convenient in the Logs as in the Market Watch and Navigator (I use it all the time in the Favourites tab to find the EX5 I need).

 

A little interesting observation about ENUM_TIMEFRAMES:

// fast calculation
string TimeframeToString(ENUM_TIMEFRAMES tf)
  {
   static const string ids[] = {
    "PERIOD_M", "PERIOD_H", "PERIOD_W", "PERIOD_MN"};
   return ids[tf / 16384] + IntegerToString(tf % 16384);
  }

void OnStart()
  {
   Print( TimeframeToString(PERIOD_M1 ) );
   Print( TimeframeToString(PERIOD_M2 ) );
   Print( TimeframeToString(PERIOD_M3 ) );
   Print( TimeframeToString(PERIOD_M4 ) );
   Print( TimeframeToString(PERIOD_M5 ) );
   Print( TimeframeToString(PERIOD_M6 ) );
   Print( TimeframeToString(PERIOD_M10) );
   Print( TimeframeToString(PERIOD_M12) );
   Print( TimeframeToString(PERIOD_M15) );
   Print( TimeframeToString(PERIOD_M20) );
   Print( TimeframeToString(PERIOD_M30) );
   Print( TimeframeToString(PERIOD_H1 ) );
   Print( TimeframeToString(PERIOD_H2 ) );
   Print( TimeframeToString(PERIOD_H3 ) );
   Print( TimeframeToString(PERIOD_H4 ) );
   Print( TimeframeToString(PERIOD_H6 ) );
   Print( TimeframeToString(PERIOD_H8 ) );
   Print( TimeframeToString(PERIOD_H12) );
   Print( TimeframeToString(PERIOD_D1 ) );
   Print( TimeframeToString(PERIOD_W1 ) );
   Print( TimeframeToString(PERIOD_MN1) );
 }
 
amrali #:

A small interesting observation about ENUM_TIMEFRAMES:

return ids[tf >> 14] + IntegerToString(tf & 0x3FFF);
 
fxsaber #:
The compiler will optimize div, modulo to shift, AND if we cast to (uint)tf. The first version is readable. Check it in Compiler Explorer. 
 

Since the thread often touches on ways to speed up the code, I am posting here a link to a universal mechanism that allows you to get rid of expensive OrdersTotal-cycle.

There is a third-party confirmation of performance increase on an EA with a large number of open positions/orders (martin/gridder).

This simple mechanism allows you to write the most productive TS of this type.

 
fxsaber #:

There is third-party confirmation of increased performance on an EA with a large number of open positions/orders (martin/gridder).

This simple mechanism allows you to write the most productive TS of this type.

Forum on trading, automated trading systems and testing trading strategies.

Libraries: Virtual

fxsaber, 2025.03.05 09:22 pm.

As a consequence, the OnTrade function is required to accelerate in MT5-Tester in a real martin/gridder environment.

I have never used this function in Tester.

Any other examples of accelerating EA in MT5-Tester using OnTrade?

 

datetime-type is a signtype- it works correctly with negative values.

template <typename T>
bool IsSign() { return((T)(-1) < 0); }

void OnStart()
{
  Print(IsSign<datetime>()); // true
}

This is convenient.

 
fxsaber #:

datetime-type is a signtype- it works correctly with negative values.

This is convenient.

Yes, you can assign negative values to datetime. In other programming languages this indicates dates before 1.1.1970, but in MQL a negative datetime value is INVALID.
 
Print(datetime(-1));

 
amrali #:
Yes, you can assign negative values to datetime. In other programming languages this indicates dates before 1.1.1970, but in MQL a negative datetime value is INVALID.
 
Print(datetime(-1));

See this post: https://www.mql5.com/en/forum/479009#comment_55521044
 
amrali #:
Yes, you can assign negative datetime values. In other programming languages, it means dates before 1.1.1970, but in MQL, a negative datetime value is INVALID.

So it's just the result of TimeToString and TimeToStruct. The good thing is that datetime is a signed long-number, which allows you to represent time much BEFORE the birth of the Universe.

I needed full arithmetic operations with datetime and comparison operators. So I had to check signed/unsigned.