Features of the mql5 language, subtleties and tricks - page 310
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
The appearance of implicit copy constructor allows making such records.
However, the use of this feature loses compatibility with MQL4.
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.
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
If you run this script multiple times, you’ll see increasing numbers like:
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)
Undocumented constant:
MQL_GLOBAL_COUNTER
Thanks for the information!
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?
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".
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.
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.
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.