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

 
The appearance of an implicit copy constructor allows such entries to be made.
void OnStart()
{
  MqlTick Tick;
  
  // Two different ways to call MqlTick::MqlTick(const MqlTick&)
  const MqlTick Tick1 = Tick;
  const MqlTick Tick2(Tick); // b5200-: function not defined, b5200+: OK.
}
 
fxsaber #:
The appearance of implicit copy constructor allows making such records.
class A {};

A Func()
{
  A a;
  
  return(a); // b5200-: object of 'class A' cannot be returned, copy constructor 'A::A(const A &)' not found, b5200+: OK.
};

However, the use of this feature loses compatibility with MQL4.

 
fxsaber #:
Pay attention to such constructs in your sources. After b5200 they may stop working as before. The compiler will not report anything about it.

Here is an example of such danger of different execution BEFORE and AFTER b5200.

struct A
{
  int Tmp;
  
  void operator =( const A&) { Print(__FUNCSIG__); }
  
  A f()
  {
    return(this); // Return object.
  }
};

void OnStart()
{
  A a;
  
  a.f(); // b5200-: void A::operator=(const A&), b5200+: NULL.
}

That's why you should look carefully at functions, where the object is returned.

 

Undocumented constant:

MQL_GLOBAL_COUNTER

  • It returns a unique counter value that is incremented each time any MQL program (Expert Advisor, Script, Indicator) calls MQLInfoInteger(MQL_GLOBAL_COUNTER).

  • Think of it as a monotonically increasing global launch counter across the whole MetaTrader terminal session, not just within one EA or chart.

  • The counter starts at 1 when the terminal is launched and increases every time a new MQL program instance calls MQLInfoInteger(MQL_GLOBAL_COUNTER).

  • The counter period is a 32-bit signed integer in the range [1, 2147483647]

Use cases

  • Seeding a PRNG with a value that is different for each program launch (to avoid identical sequences when multiple EAs/scripts are started simultaneously).

  • Creating unique identifiers across all scripts/EAs without risk of collision.

Example

void OnStart()
{
   Print("Global counter = ", MQLInfoInteger(MQL_GLOBAL_COUNTER));
}

If you run this script multiple times, you’ll see increasing numbers like:

Global counter = 25
Global counter = 26
Global counter = 27

Important:

  • It is global to the terminal session, not reset per chart or EA.

  • Once you restart MetaTrader, the counter resets back to 1 .

So in short:
MQLInfoInteger(MQL_GLOBAL_COUNTER) gives you a unique, incrementing integer each time an MQL program calls the MQLInfoInteger function.

(How it was detected: see https://www.mql5.com/en/code/56055)

 
amrali #:

Undocumented constant:

MQL_GLOBAL_COUNTER

Thanks for the information!

 
amrali #:

MQL_GLOBAL_COUNTER

Thanks for the information!

I wonder if atomic access to this counter is implemented. If 2 or more indicators are running on the same instrument, will they get different numbers?

 
Andrey Khatimlianskii #:
If 2 or more indicators work on the same instrument, do they get different numbers?

Different. There are 2 Buttons indicators attached to the chart, the first one has a unique ID "Buttons01" at the top, the second one has "Buttons02".

2025.08.29 12:59:36.819 Buttons (GBPCAD,H2)     Global counter Buttons02 = 50
2025.08.29 12:59:36.831 Buttons (GBPCAD,H2)     Global counter Buttons01 = 51
2025.08.29 13:00:35.086 Buttons (GBPCAD,H1)     Global counter Buttons02 = 52
2025.08.29 13:00:35.113 Buttons (GBPCAD,H1)     Global counter Buttons01 = 53
 
Andrey Khatimlianskii #:

Thanks for the information!

I wonder if atomic access to this counter is implemented. If 2 or more indicators are running on the same instrument, will they get different numbers?

Indicators for a given instrument are running on the same thread, so they are executed in series not in parallel.

 
Andrey Khatimlianskii #:

will they get different numbers?

I'm sure that, for example, GetMicrosecondCount can always be used to get guaranteed different values with any reasonable number of concurrently running programmes.

 
fxsaber #:

I am sure that, for example, GetMicrosecondCount can always be used to get guaranteed different values for any reasonable number of concurrently running programmes.

I seem to have come across non-unique values. Probably when starting the terminal or connecting to an account. But I don't remember exactly.