Discussion of article "MQL5 Programming Basics: Global Variables of the Terminal" - page 3

 
Dmitry Fedoseev:
It can be solved through prefixes of global variables.
It can be solved through prefixes, but it's not a nice solution :-( there is a question of generating unique prefixes and in the review of variables by F3 it's a mess...really variables lack namespace. If we have to generate a prefix from DC, account, account, chart...and remember that the length of the name is limited, we can't do much with prefixes :-)
 

Good articles for beginners.

The GlobalVariableTemp() function creates a temporary global variable (that exists till the terminal is stopped). In a few years that I develop EAs on MQL5, I have never faced the need for such a variable. Moreover, the very concept of a temporary global variable contradicts the basic principle of their application — long-term data storage not affected by the terminal relaunches.

That's not the only usage of Global Variables of the Terminal. They can also be used to exchange information between different EAs or indicators. So in such case it could be a feature to have data in memory only and not kept of terminal restart.

The mutex part is very interesting, I have one question and one remark.

It may happen that some EA is removed from a chart during the OnTick() function execution but the Mutex_Release() function is not executed.

How an EA could be removed and the Mutex_Release() function not executed ? If you remove an EA from a chart in a normal way (close chart, remove EA, or close terminal), it will never stop the execution flow. I am missing something ?

So my remark, the only way Mutex_Release() could not be executed is with an "hard" stop, power or hardware failure etc..., so that makes your mutex global variables a perfect candidate to be created as a temp using GlobalVariableTemp().

 

Do global variables work during Strategy Testing?

For example, when an indicator stores values to a GV and an EA reads them, will the Strategy Tester fetch the values during simulation on time?

 
Admiral Thrawn:

Do global variables work during Strategy Testing?

For example, when an indicator stores values to a GV and an EA reads them, will the Strategy Tester fetch the values during simulation on time?

Yes

 
"It is possible for global variables to disappear when the computer is abruptly de-energised."

I have global variables disappeared after closing mt5 terminal bild 2361. GlobalVariablesFlush() is executed on 2 different MT5 terminals. Nonsense. Terminals are launched with the /portable key, maybe this is the reason?

I tried to create by hand, opened and closed the terminal, it seems to be saved. I had the results of auto optimisation written to the globals, and this is a very long process. So it's not such a reliable storage. I will have to add a procedure for writing global variables to the file through structures.

Документация по MQL5: Основы языка / Переменные / Глобальные переменные
Документация по MQL5: Основы языка / Переменные / Глобальные переменные
  • www.mql5.com
Глобальные переменные создаются путем размещения их объявлений вне описания какой-либо функции. Глобальные переменные определяются на том же уровне, что и функции, т. е. не локальны ни в каком блоке. Область видимости глобальных переменных - вся программа, глобальные переменные доступны из всех функций, определенных в программе...
 
Konstantin Efremov:

I have global variables disappeared after closing mt5 terminal bild 2361. GlobalVariablesFlush() is executed on 2 different MT5 terminals. Nonsense. Terminals are launched with the /portable key, maybe that's the reason?

I tried to create by hand, opened and closed the terminal, it seems to be saved. I had the results of auto optimisation written to the globals, and this is a very long process. So it's not such a reliable storage. I will have to add a procedure for writing global variables to the file through structures.

In the terminal or in the tester? There is a difference in MT5.

 
Dmitry Fedoseev:

In the terminal or in the tester? There is a difference in MT5.

In the terminal.

In general, I solved the problem in the following way. In the class that deals with auto-optimisation for the Expert Advisor, besides creating global variables, I also wrote identical data into files for each currency pair (preliminary files are deleted each time when starting the class, as the class is called only for recalculation, so in any case the data must be updated). The files are ordinary text files, the names of the files are encoded in a particular instance of the Expert Advisor. During initialisation, the EA calls the function of reading optimisation data from files and creating or updating global variables.

It is also convenient because the files can be backed up and used when running in the strategy tester (the files naturally lie in a common folder of terminals), since the tester creates its own instances of global variables, and auto optimisation itself is a long process and loading ready data from the files significantly reduces the testing time.

Документация по MQL5: Основы языка / Переменные / Глобальные переменные
Документация по MQL5: Основы языка / Переменные / Глобальные переменные
  • www.mql5.com
Глобальные переменные создаются путем размещения их объявлений вне описания какой-либо функции. Глобальные переменные определяются на том же уровне, что и функции, т. е. не локальны ни в каком блоке. Область видимости глобальных переменных - вся программа, глобальные переменные доступны из всех функций, определенных в программе...
 
I've read it all, it's a great benefit, but the last part is a bit complicated, temporary variables are still useful I have a trading panel in the EA, the panel includes a button to open the trailing stop function, the temporary variable can remember the value of the variable and then switch cycles I can get the status of the original button and redraw it Thank you for your article!
 

Hi  Dmitry,

I realise your article is a few years old, but I thought I might share something and hopefully also ask a question!

So you mention that you have not found use of the Temp Global Var.  I'm developing something that requires that exactly.  I was racking my head trying to figure a way to implement sharing IO Completion Port handle through Global Variables, in particular on crash, or normal restart.  I'm designing an order handling system using Windows IO Completion Ports. Since we can create Win32 threads from MQL5, I cannot create a thread pool while creating the IOCP Server, so my workaround using Services is;

  1. Create an IOCP Server, instead of creating threads(which we cannot), I save the IOCP Handle to a Global Var
  2. I then open at least 2 other Services, which act as Worker Threads, and they read the IOCP Handle from Global Var, and register themselves to the IOCP

In the event of the terminal restarting, abnormally or otherwise, I want that IOCP Handle to have disappeared on terminal start, so I can create a new IOCP Handle for a fresh start. It is makes for a much cleaner implementation.

I was also wondering, how fast are Global Variables, and how reliable are they?

Thanks for the great article.  It was a great read,

Shep


 
MetaQuotes:

New article MQL5 Programming Basics: Global Variables of the Terminal has been published:

Author: Dmitry Fedoseev

This what the Python integration needs to send data to and from Python to MLQ EA's. Then an EA developed in Python could be tested in the Strategy Tester, unless there is some impossibility I don't know about.