External variable is a variable, the value of which is available from a program properties window. An external variable is declared outside all functions and is a global one - its scope is the whole program. When declaring an external variable, modifier 'extern' should be indicated before its value type:
extern int Number; // External variable of integer type
External variables are specified in the program head part, namely before any function that contains an external function call. Use of external variables is very convenient if it is needed from time to time to start a program with other variables values.
Obviously, this Problem implies the necessity to change settings, because today's prices differ from ones that were yesterday; as well as tomorrow we will have different prices. To provide the option of changing settings in the Expert Advisor externvar.mq4 external variables are used:
//--- // externvar.mq4
// The code should be used for educational purpose only.
//---
In this program external variables are set up in the lines:
extern double Level = 1.2500; // External variable extern int n = 1; // External variable
Values of external variables are available from a program parameters window. The asset of these variables is that they can be changed at any moment - on the stage of attaching the program to a security window and during the program operation.
Problem 23. Create a program, in which the following conditions are implemented: if a price reached a certain Level and went down this level in n points, this fact should be once reported to a trader.
Page 3 of 5
Types of Variables - Variables - MQL4 Tutorial
Fig. 54. Program properties window; here values of variables can be changed.
At the moment of attaching the program to a security window, variable values contained in the program code will be indicated in a program parameters window. A user can change these values. From the moment when a user clicks OK, the program will be started by the client terminal. The values of external variables will be those indicated by a user. In the operation process these values can be changed by the executed program.
If a user needs to change values of external variables during the program's operation, setup window should be opened and changes should be made. It must be remembered that program properties toolbar can be opened only in the period when the program (Expert Advisor or indicator) is waiting for a new tick, i.e. none of special functions is executed. During the program execution period this tollbar cannot be opened. That is why if a program is written so, that it is executed long time (several seconds or tens of seconds), a user may face difficulties trying to access the parameters window. Values of external variables of scripts are available only at the moment of attaching the program to a chart, but cannot be changed during operation If the parameters window is open, Expert Advisor does not work, control is performed by the client terminal and is not passed to a program for starting special functions.
The client terminal starts successively the execution of the special function deinit(), then the special function init(), after that when a new tick comes - start(). At the execution of deinit() that finishes a program, external variables will have values resulted from the previous session, i.e. those available before the EA settings tollbar was opened. Before the execution of init() external variables will get values setup by a user in the settings toolbar and at the execution of init() external variables will have new values set by a user. Thus new values of external variables become valid from the moment of a new session (init - start - deinit) of an Expert Advisor that starts from the execution of init().
The fact of opening a setup window does not influence values of global variables. During all the time when the window is open and after it is closed, global variables preserve their values that were valid at the moment preceding the toolbar opening.
In the program externvar.mq4 also one local and two global variables are used.
bool Fact_1 = false; // Global variable bool Fact_2 = false; // Global variable
double Price = Bid; // Local variable
Algorithmically the problem solution looks like this. Two events are identified: the first one is the fact of reaching Level, the second - the fact that the alert (about getting lower than Level in n points) has been shown. These events are reflected in the values of variables Fact_1 and Fact_2: if the event did not happen, the value of the corresponding value is equal to false, otherwise - true. In the lines:
if (NormalizeDouble(Price,Digits) >= NormalizeDouble(Level,Digits)) Fact_1 = true; // Event 1 happened
the fact of the first events happening is defined. The standard function NormalizeDouble() allows to conduct calculations with values of actual variables at a set accuracy (corresponding to the accuracy of a security price). If the price is equal to or higher than the indicated level, the fact of the first event is considered to be fulfilled and the global variable Fact_1 gets the true value. The program is constructed so that if Fact_1 once gets the true value, it will never be changed into false - there is no corresponding code in the program for it.
Please note, that when an EA properties window is open and a user is making decision about external variable values, the EA (or indicator) does not work. Having set the values of external variables and clicking OK the user starts the program once again.
Page 4 of 5
Types of Variables - Variables - MQL4 Tutorial
In the lines:
if (Fact_1 == true && NormalizeDouble(Price,Digits)<=
NormalizeDouble(Level-n*Point,Digits))
My_Alert(); // User-defined function call
the necessity to show an alert is defined. If the first fact is completed and the price dropped by n points (less or equal) from an indicated level, an alert must be shown - user-defined function My_Alert() is called. In the user-defined function after the alert the fact of already shown alert is indicated by assigning 'true' to the variable Fact_2. Then the user-defined function and after it the special function start() finish their operation.
After the variable Fact_2 gets the true value, the program will each time finish its operation, that's why a once shown alert will not be repeated during this program session:
if (Fact_2==true) // If there was an Alert..
return; //..exit
Significant in this program is the fact that values of global variables can be changed in any place (both in the special and the user-defined functions) and are preserved within the whole period of program operation - in the period between ticks, after changing external variables or after changing a timeframe.
In general case values of global variables can be changed in any special function. That is why one should be extremely attentive when indicating operators that change values of global variables in init() and deinit(). For example, if we zero the value of a global variable in init (), at the first start() execution the value of this variable will be equal to zero, i.e. the value acquired during the previous start() execution will be lost (i.e. after changing external program settings).
Predefined Variables and the Function RefreshRates GlobalVariables
Page 5 of 5
Types of Variables - Variables - MQL4 Tutorial
GlobalVariables
Several application programs can operate in the client terminal at the same time. In some cases the necessity may occur to pass some data from one program to another. Specially for this MQL4 has global variables of the client terminal.
Global variable of Client Terminal is a variable, the value of which is available from all application programs started in a client terminal (abbreviated form: GV).