How to reset an MQL variable on a running EA [advanced]

Q: Can I reset an MQL variable in a running EA?

A: Not easily, but one way I can think of is to use the MQL Global Variable functions.

I’ve attached a VTS system and EA to show how this can be done.

Note: Normally in programming when we use the word “global”, it means the variable can be accessed from anywhere in the program.  MQL’s use of the term is a little different: these global variables maintain their value between separate runs of the EA. (Basically, the MetaTrader platform reads and writes the value to a file.)

I use the MQL Global Variable functions in the plug-ins, but they are bit complex for most traders (and their use does not come up that often), so in VTS, I placed them under the “Advanced” menu in the Function’s toolbox.

Here is some info from the MQL documentation:

Global Variables of the Client Terminal

There is a group set of functions for working with global variables. Global variables of the client terminal should not be mixed up with variables declared in the global scope of the mql4 program. Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.




Checks the existence of a global variable with the specified name


Returns time of the last accessing the global variable


Deletes a global variable


Returns the value of a global variable


Returns the name of a global variable by its ordinal number in the list of global variables


Sets the new value to a global variable


Forcibly saves contents of all global variables to a disk


Sets the new value to a global variable, that exists only in the current session of the terminal


Sets the new value of the existing global variable by condition


Deletes global variables with the specified prefix in their names


Returns the total number of global variables

The attached VTS system:

  • Uses GlobalVariableCheck to see if the variable exists. If it does not, create it.
  • Uses GlobalVariableGet to get the value and store it in a normal variable (or you can use it directly)

Here is a screenshot of the VTS drawing:

Global variables can be access in the MetaTrader platform at Tools->Global Variables (or F3):

From this window you can edit the global variable:

In this way, you can reset, or change the value of any MQL variable in a running EA.

Here is the VTS system and Expert Advisor to help get started with this technique.



To get notified about the latest questions and answers, follow us!