Articles on MQL4
Global Variables

Global Variables


for example, forex
There is only one disadvantage in our advisor. In block init() there is a procedure of file opening and reading its data. In case of optimisation two problems occur:
  1. speed of optimisation decreases as reading from files is much slower than reading from the operative memory;
  2. multiple operations of files opening/close are undesirable for the hard disk.
Programs in MQL-IV have global variables declared in the body of the code out of any function. These values are preserved while the program works and they are available from any point (function) of this program. Some other decision is required for strategies optimiser to preserve variables values between programs launch. In the optimiser we start the same program-advisor several times and sometimes it is required to remember the state of some variables between some separate launches.

For such purposes there are some special variables, Global Variables. Unlike global variables on the level of a code/program, these variables are global on the level of the terminal. Thus, we can create some global variable in the script and set its value (GlobalVariableSet()), and then check its value in the indicator or advisor (GlobalVariableGet()).

Unlike other types of variables these global variables are kept within 4 weeks when the terminal is closed. Let's suppose that our advisor should open only a position a day. If the fact of opening a position is kept in some common variable then when the terminal is closed or supply is off the value of this variable (let's call it TradeOpen=true) is not preserved. Then when the terminal is launched again variable TradeOpen will take the default value (false). And the advisor will open a new position automatically, which is forbidden by the algorithm. Usage of a global variable solves this problem.

Let's create a script to understand how the method of global variables usage works. This script will create a global arrays of values. It is important: global variables can have only type double. There is no possibility to create an array of global variables, though we can solve this problem easily. Functions of global variables check and assignment use the name of this variable, set in the text format. That is why we may set the value for the array by the index as Array_name+index.

Let's name the script as GlobalArray.mq4. Instead of the global array we will create the array of global variables:

 
 


The code shows a typical work with the global variables, at first we check the presence of the required variable (GlobalVariableCheck(Variable_name)), then we set its value. Let's run the script in any chart, press F3 and see a window with a list of global variables.

 
 


There are three columns: name of the variable, its value and the date of its creation. Now we should adapt it for the purposes of our advisor. Signals from the file should be changed into clear global variables. The simplest way is to code the time of the signal in the name of the global variable and the type of the signal in the value of its variable. Then on each new bar in the advisor we will check the presence of the global variable, containing the bar opening time, if there is such a variable the only thing to do is to check the type of the signal and open a correct position.

The required functions are given in the Help section of MetaEditor. We do not need function GlobalVariableSetOnCondition() yet, the other functions are clear.

 
 


Let's save the advisor from the previous article as CTL+GV.mq4(Global Variables). Change function init() a bit. At first check the presence of the global variable with name FileOpened (the file has been already open), if there is no such a variable we should open a file, create a number of global variables, which contain signals to open positions.

 
 


We also change a bit the function of the signal detection:

 
 


As you see minimum changes were required. Let's compile the advisor and launch the tester. Once testing is finished press button F3. Newly open global variables appear.

 
 


Now we can optimise our log on, i.e. choose the size of a stop and other parameters of trailing without speeding down of optimisation.

 
 


Important: global variables are the same both for the terminal and the tester. That is why if it is necessary for you to optimise global variables values in the tester when using these global variables in the on-line charts it is better to install two copies of MT4. In one of them testing will be done, the other one will work with your demo or real account.

There are several variants to use global variables, here are some of them:
      a) to remember the time of the last signal (of position opening, indicator value);
      b) to remember the current number of the profitable deals in succession;
      c) to import some parameters from one advisor to another (e.g. probability of breakout of some important level on the other instrument);
      d) optimal parameters of the last optimisation in the tester etc.


The files are available here
Go to article «Price Extremums Search».

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?