Global variables

Hi folks,

Today we are going to study a set of MQL4 functions which needs to be clearer for the new comers to the language. We are going to study the Global variables and the MQL4 functions that handle them.

 

What are the global variables?

 

The global variables are places to store data between the instances of MQL4 programs and MetaTrader launches too.

In MQL4 you use the normal variables to store the data temporary and it exists only at the time that program is running and vanishes with the un-initialization of the program. But the global variables exist when you un-initialize the program and even if you shut down the terminal itself. The terminal can store the global variables for four weeks from the last store.

 

How useful the global variables?

 

The main job of the global variables is enabling the inner communication between the experts advisor.

 

Assume that you have created two expert advisors:

The first expert advisor works on daily time frame and buys the EURUSD when the Moving Averages of the price of the last 10 days crosses upward the Moving Averages of the price of the last 80 days.

And it sells the EURUSD when the Moving Averages of the price of the last 10 days crosses downward the Moving Averages of the price of the last 80 days.

 

And the second expert advisor works on 1 hour time frame and buys the EURUSD when the Moving Averages of the price of the last 10 hours crosses upward the Moving Averages of the price of the last 80 hours.

And it sells the EURUSD when the Moving Averages of the price of the last 10 hours crosses downward the Moving Averages of the price of the last 80 hours.

 

You don't want the two expert advisors to take conflicted decisions. You don't want the first expert advisor to sell the EURUSD while the second expert advisor is buying the EURUSD.

 

Any of the two expert advisors doesn't know anything about the other. How can they communicate?

They can communicate with the aid of writing/reading global variables.

For instance the first expert advisor can write a global variable when it open sell/buy order while the second expert advisor can read this variable to be sure that it will not open a conflicting orders.

 

Of course the possibilities of using global variables are not limited. Our example today will use the global variables in a very useful way.

 

Our example:

When you attach an expert advisor to more than one chart/pair and want to change one of the expert advisor inputs values for all the charts you have to change this input for every chart.

If your expert advisor attached to ten pairs it will be a real problem to make the change 10 times.

Our program today will use the global variables to make the changes available to all the charts hold our expert advisor.

 

 

Global variables functions:

There are 6 functions in MQL4 responsible of handling the global variables. These are the global variables functions:

 

GlobalVariableCheck:

Syntax:

bool GlobalVariableCheck (string name)

Description:

The GlobalVariableCheck function checks if there's a global variable with the name passed to, it returns true if it exists and false if not exists.

You can use GetLastError to get the error information if any error has been occurred will you calling this function.

Parameters:

This function takes only one parameter:

string name

The global variable name the function will check is it exists or not.

Example:
 

if(!GlobalVariableCheck("ge_TakeProfit "))

Alert("ge_TakeProfit global variable is not exist");

 
 

GlobalVariableDel:

Syntax:
 

bool GlobalVariableDel (string name)

Description:
 

The GlobalVariableDel function deletes the global variable you passed its name and return true in success and false if it failed to delete it.

You can use GetLastError to get the error information if any error has been occurred will you calling this function.
 

Note: If the global variable not found the returned value will be 0 while the error code will be 4057 which means global variables processing error.
 

Parameters:

This function takes only one parameter:

string name

The global variable name the function will delete.
 

Example:
 

Alert(GlobalVariableDel("ge_TakeProfit "));

 
 

GlobalVariableGet:

Syntax:
 

double GlobalVariableGet (string name)

 

Description:
 

The GlobalVariableGet function returns the value of the global variable name passed to it. This value must to be double data type.

You can use GetLastError to get the error information if any error has been occurred will you calling this function.

 

Note: You can store in the global variable and data type but the double data type. And if you tried to store any data type other than the double data type, MQL4 will try to convert it to double data type.
 

Note: If the global variable not found the returned value will be 0 while the error code will be 4058 which means global variable not found. So, you have to use GlobalVariableCheck function before using GlobalVariableGet if your variable maybe 0.

 

Parameters:

This function takes only one parameter:

string name

The global variable name the function will return its stored value.

Example:
 

if(GlobalVariableCheck("ge_TakeProfit"))

   TakeProfit = GlobalVariableGet("ge_TakeProfit");

 
 

GlobalVariableSet:

Syntax:
 

datetime GlobalVariableSet (string name, double value)

 

Description:
 

The GlobalVariableSet function sets the variable name passed to it with the value passed to it.

If the global variable name does not exist the function will create it and set it to the passed value. If it exists the function will change its old value to the new one.

The returned value is a datetime data type which is the time of the last access time of the global variable if the function successes and if it failed the return value will be 0.

You can use GetLastError to get the error information if any error has been occurred will you calling this function.
 

Parameters:

This function takes two parameters:

string name

The global variable name the function will set its value (or create it and sets its value).

double value

The value of the global variable you want to set (or create). This value must be double (numeric) data type.
 

Example:
 

Alert(GlobalVariableDel("ge_TakeProfit "));

 

 

GlobalVariableDel:

Syntax:
 

bool GlobalVariableDel (string name)

 

Description:
 

The GlobalVariableDel function deletes the global variable you passed its name and return true in success and false if it failed to delete it.

You can use GetLastError to get the error information if any error has been occurred will you calling this function.
 

Note: If the global variable not found the returned value will be 0 while the error code will be 4057 which means global variables processing error.
 

Parameters:

This function takes only one parameter:
 

string name

The global variable name the function will delete.

 

Example:
 

Alert(GlobalVariableDel("ge_TakeProfit "));