GlobalVariableCheck(): check for the existence of a global variable
For a person who has some experience of writing expert advisors the expression «global variables» may be somewhat confusing. The point is that there are two types of «global variables»:
- variables that are seen from any function of ONE expert advisor (i.e. they are global within this expert advisor); and
- variables that are common for ALL expert advisors.
I wrote earlier in the article «Global variables» about global variables of the first type – those that are common for all the functions of ONE expert advisor.
Now I will tell You about global variables of the second type — common for ALL expert advisors.
A global variable of the second type is a variable that can be addressed from any expert advisor. If during four weeks there has been no attempt to read the value of a global variable or to write a new value in it this global variable will be deleted automatically by the client terminal.
For work with global variables the following functions are used:
- GlobalVariableCheck()
- GlobalVariableDel()
- GlobalVariableGet()
- GlobalVariableName()
- GlobalVariableSet()
- GlobalVariableSetOnCondition()
- GlobalVariablesDeleteAll()
- GlobalVariablesTotal()
At first I’d like to consider the GlobalVariableCheck() function.
bool GlobalVariableCheck(string name)
This function returns true, if a global variable with the name name exists, and it returns false, if such a variable doesn’t exist.
In our function WaitBeforeTransaction() we store the time and date when the last trade was performed «LastTradeTime».
Before we know the time of the last trade having read the value of this variable we check if it exists:
// if a global variable LastTradeTime doesn’t exist,
// create it
if (!GlobalVariableCheck("LastTradeTime"))
{
... we create a variable ...
}
There are two places where we can check the existence of a global variable and create it in case it doesn’t exist: at initializing an expert advisor — in the init() function — or on each tick — in the start() function.
In spite of the fact that from the first sight it suggests itself to perform these actions in the init() function, I would recommend you to do it in the start() function. The point is that if a user deletes a global variable when the expert advisor is working while checking for its existence is performed only at the expert advisor’s initializing further behavior of such an expert advisor is difficult to forecast. Most probably such an expert advisor will simply stop trading and will return errors at addressing to the deleted global variable.
GlobalVariableSet() — setting of a new value of a global variable
Suppose a global variable doesn’t exist or we need to change its value. In this case we should use the GlobalVariableSet() function.
datetime GlobalVariableSet(string name, double value)
The GlobalVariableSet() function sets a new value value to a global variable with the name name and in case of a success it returns the time of the last access to the global variable. If an error occurs the function returns 0. The error code as usually one can get with the help of the GetLastError() function.
If a global variable with the name name doesn’t exist it is created and takes on the value value.
We can find an example of using the GlobalVariableSet() function in the function written by us WaitBeforeTransaction():
// if a global variable LastTradeTime doesn’t exist,
// create it
if (!GlobalVariableCheck("LastTradeTime"))
{
// if an error occurs at calling
// the GlobalVariableCheck() function, we’ll exit with an error
if (GetLastError()!=0)
{
Print("WaitBeforeTransaction(): error ",GetLastError(),
" at checking of global variable LastTradeTime");
return(-1);
}
// a global variable doesn’t exist, we’ll create it
if (GlobalVariableSet("LastTradeTime", 1)==0)
{
// an error occurred at creating a global variable
Print("WaitBeforeTransaction(): error ",GetLastError(),
" at creating a global variable LastTradeTime");
return(-1);
}
// global variable is created successfully
Print("WaitBeforeTransaction(): global variable ",
"LastTradeTime is created");
}
GlobalVariableSetOnCondition() — setting of a new value of the global variable if its current value equals to the set value
Fortunately the developers of MetaQuotes Language 4 are professional programmers and can foresee what a trader may need in the process of writing expert advisors. I’m very grateful to them that they have included in the list of functions of MetaQuotes Language 4 the GlobalVariableSetOnCondition() function.
Let’s consider why this function is so useful.
bool GlobalVariableSetOnCondition(string name, double value, double check_value)
First of all this function checks whether a global variable with the name name exists. In documentation on MetaQuotes Language 4 it is written that if there is no such a variable the function returns false and generates the following error ERR_GLOBAL_VARIABLE_NOT_FOUND (4058), which can be received with the help of the GetLastError() function.
But in experiments with the GlobalVariableSetOnCondition() function I failed to make the function return the error ERR_GLOBAL_VARIABLE_NOT_FOUND (4058). For some reason even in case the global variable doesn’t exist the function returns false and the error code ERR_NO_ERROR (i.e. 0). Here is the portion of the code which I used for this purpose:
string name = "MyGlobalVariable";
double value = 1;
double check_value = 2;
if (GlobalVariableSetOnCondition(name, value, check_value))
{
Print("Global variable ", name, " exists.",
" Its value has been changed from ", check_value, " to ", value);
}
else
{
int Err=0;
Err = GetLastError();
Print(Err);
switch (Err)
{
case 0: Print("Global variable ", name," hasn’t been changed",
" as its value isn’t equal ", check_value); break;
case 4058: Print("Global variable ", name,
" doesn’t exist"); break;
default: Print("Unknown error: ", Err);
}
}
If this is a bug I think it will be liquidated in the next versions of MetaTrader 4.
Let’s return to the description of the GlobalVariableSetOnCondition() function. If global variable name exists the function won’t change its current value and will return false, if the current value of this global variable isn’t equal to check_value. If later we request the code of the last error the GetLastError() function will return ERR_NO_ERROR (i.e. 0), as there really was no error.
If the current value of the global variable name equals to value check_value the GlobalVariableSetOnCondition() function will assign a new value value to this global variable.
Next article: "
Waiting for the trade flow to disengage"