[go: up one dir, main page]

0% found this document useful (0 votes)
98 views8 pages

Variables: List of Simple Predefined Names of Variables

This document discusses different types of variables used in MQL4 programming language. It describes predefined variables that reflect the state of the current chart and are automatically updated. These include values like Ask, Bid, Bars, etc. It also discusses how each program run by the terminal has its own copies of predefined variables. The document outlines functions like RefreshRates() that can update these variable values. Finally, it summarizes the two types of user-defined variables - local variables accessible only within their function, and global variables accessible program-wide.

Uploaded by

danymiz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views8 pages

Variables: List of Simple Predefined Names of Variables

This document discusses different types of variables used in MQL4 programming language. It describes predefined variables that reflect the state of the current chart and are automatically updated. These include values like Ask, Bid, Bars, etc. It also discusses how each program run by the terminal has its own copies of predefined variables. The document outlines functions like RefreshRates() that can update these variable values. Finally, it summarizes the two types of user-defined variables - local variables accessible only within their function, and global variables accessible program-wide.

Uploaded by

danymiz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Variables

For creating programs in any algorithmic language knowing different variable types is very important. In this section we will analyze all types of variables used in MQL4.

Predefined Variables and RefreshRates Function


There are variables with predefined names in MQL4 language. Predefined variable is a variable with a predefined name, the value of which is defined by a client terminal and cannot be changed by program methods. Predefined variables reflect the state of a current chart at the moment of program start (Expert Advisor, script or custom indicator) or as a result of RefreshRates() implementation.

List of Simple Predefined Names of Variables


Ask - last known sell-price of a current security; Bid - last known buy-price of a current security; Bars - number of bars on a current chart; Point - point size of a current security in quote currency; Digits - number of digits after a decimal point in the price of a current security.

List of Predefined Names of Arrays-Timeseries


Time - opening time of each bar on the current chart; Open - opening price of each bar on the current chart; Close - closing price of each bar on the current chart; High - maximal price of each bar on the current chart; Low - minimal price of each bar on the current chart;

Volume - tick volume of each bar on the current chart. (concepts of "arrays" and "arrays-timeseries" are described in the section Arrays).

Properties of Predefined Variables


Predefined variable name cannot be used for identifying user-defined variables. Predefined variables can be used in expressions equally with other variables in accordance with the same rules, but the value of a predefined variable cannot be changed. When trying to compile a program containing an assignment operator, in which a predefined variable is placed to the right of an equality sign, MetaEditor will show an error message. In terms of visibility predefined variables refer to global, i.e. they are available from any program part (see Types of Variables). The most important property of predefined variables is the following: Values of all predefined variables are automatically updated by a client terminal at the moment when special functions are started for execution. Previous and current values of predefined variables may be equal, but the value itself will be updated. At the moment of a special function's start values of these variables are already updated and are available from the first program lines. Here is another important property of predefined variables: Client terminal creates a set of local copies of predefined variables separately for each started program. Each started program works with its own historic data copies set. In one client terminal several application programs (Expert Advisors, scripts, indicators) can run at the same time, and for each of them the client terminal will create a separate set of copies of all predefined variables' values - historic data. Let's analyze in details reasons for this necessity. Fig. 52 shows the possible operation of Expert Advisors with different execution length of the special function start(). For simplicity let's assume that in the analyzed Expert Advisors there are no other special functions and both Expert Advisors operate on the same timeframes of the same security.

#### picture Fig. 52. Operation time of start() can be larger or smaller than a time interval between ticks.

Expert Advisors differ in the time of start() execution. For common middle-level Expert Advisors this time is approximately equal to 1 up to 100 milliseconds. Other Expert Advisors

may be executed much longer, for example, several seconds or tens of seconds. Time interval between ticks is also different: from several milliseconds to minutes and sometimes even tens of minutes. Let's analyze on the given example how frequency of ticks receipt influences the operation of Expert Advisor 1 and Expert Advisor 2 that have different start() execution time. At the time moment t 0 Expert Advisor 1 is attached to a client terminal and switches to the tick-waiting mode. At the moment t 1 a tick comes and the terminal starts the special function start(). Together with that the program gets access to the updated set of copies of predefined variables. During execution, the program can refer to these values, they will remain unchanged within the start() operation time. After start() finishes its operation the program will enter the tick-waiting mode. The closest event at which predefined variables may get new values is a new tick. Start() execution time T1 of the Expert Advisor 1 is considerably shorter than a waiting time between ticks, for example interval t 1 - t 2 or t 2-t 3, etc. So, during the analyzed Expert Advisor 1 execution period there is no situation in which values of predefined variables could become old, i.e. differ true (from last known) values of the current moment. In the operation of Expert Advisor 2 there is another situation, because its start() execution period T2 sometimes exceeds the interval between ticks. The function stat() of Expert Advisor 2 is also started at the moment t 1. Fig. 52 shows that interval t 1 - t 2 between ticks is larger than start() execution time T2, that is why during this period of the program's operation predefined variables are not updated (in this period new values do not come from a server, so their true values should be considered values that appeared at the moment t 1). Next time start of Expert Advisor 2 is started at the moment t 2 when the second tick is received. Together with that set of copies of predefined values is also updated. In Fig. 52 we see that the moment of t 3 tick coming is within the period when start() is still being executed. A question arises: What will be the values of predefined variables available to Expert Advisor 2 in the period from t 3 when the third tick comes to t 32 when start() finishes its operation? The answer can be found in accordance with the following rule: Values of predefined variables copies are saved during the whole period of special functions' operation. These values may be forcibly updated using the standard function RefreshRates(). Thus (if RefreshRates() has not been executed) during the whole period of start() execution, Expert Advisor 2 will have access to the local copies set of predefined variables that was created when the second tick was received. Though Expert Advisors operate in the same windows, starting from the moment of t 3 ticket receipt each EA will operate with different values of predefined variables. Expert Advisor 1 will work with its own local copies set of historic data, values of which are defined at the moment t 3, and Expert Advisor 2 will work with its own data copies, values of which are equal to t 2. The larger application program execution time is and the shorter the interval between ticks is, the larger the probability is that the next tick will come during the program execution period. The set of local copies of historic data provides conditions for each program that guarantee constancy of predefined variables through the whole execution time of a special function.

Starting from the moment t 4 when the next tick comes, both EAs will be started again, each of them having access to its own copies set of predefined variables, values of which are formed at the moment t 4 when the fourth tick comes.

RefreshRates() Function
bool RefreshRates()

The standard function RefreshRates() allows to update values of local historic data copies. In other words this function forcibly updates data about a current market environment (Volume, server time of the last quote Time[0], Bid, Ask, etc.).This function may be used when a program conducts calculation for a long time and needs updated data. RefreshRates() returns TRUE, if at the moment of its execution there is information about new historic data in the terminal (i.e. if a new tick has come during the program execution). In such a case the set of local copies of predefined variables will be updated. RefreshRates() returns FALSE, if from the moment of a special function execution start historic data in the client terminal have not been updated. In such a case local copies of predefined variables do not change. Please note that RefreshRates() influences only the program in which it is started (not all programs working in a client terminal at the same time).

Types of Variables
An application program in MQL4 can contain tens and hundreds of variables. A very important property of each variable is the possibility to use its value in a program. The limitation of this possibility is connected with the variable scope. Variable scope is a location in a program where the value of the variable is available. Every variable has its scope. According to scope there are two types of variables in MQL4: local and global.

Local and Global Variables


Local variable is a variable declared within a function. The scope of local variables is the body of the function, in which the variable is declared. Local variable can be initialized by a constant or an expression corresponding to its type. Global variable s a variable declared beyond all functions. The scope of global variables is the entire program. A global variable can be initialized only by a constant corresponding to its type (and not expression). Global variables are initialized only once before stating the execution of special functions.

If control in a program is inside a certain function, values of local variables declared in another function are not available. Value of any global variable is available from any special and user-defined functions.

Static Variables
On the physical level local variables are presented in a temporary memory part of a corresponding function. There is a way to locate a variable declared inside a function in a permanent program memory - modifier 'static' should be indicated before a variable type during its declaration:
static int Number; // Static variable of integer type

Static variables are initialized once. Each static variable can be initialized by a corresponding constant (as distinct from a simple local variable that can be initialized by any expression). If there is no explicit initialization, a static variable is initialized by zero. Static variables are stored in a permanent memory part, their values are not lost at exiting a function. However, static variables have limitations typical of local variables - the scope of the variable is the function, inside which the variable is declared, as distinct from global variables whose values are available from any program part. All arrays are initially static, i.e. they are of static type, even if it is not explicitly indicated (see Arrays).

External Variables
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; integer type // External variable of

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. 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. 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. 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. 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 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).

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).

Note, global variable of client terminal and global variable are different variables with similar names. The scope of global variables is one program, in which the variable is declared; while the scope of global variables of client terminal is all programs launched in the client terminal.

Properties of GlobalVariables
As distinct from other variables, GV can be not only created from any program, but also deleted. GV value is stored on a hard disk and saved after a client terminal is closed. Once declared GV exists in the client terminal for 4 weeks from the moment of the last call. If during this period none of programs called this variable, it is deleted by the client terminal. GV can be only of double type. There is a set of functions in MQL4 for working with GV. Note: GlobalVariable name can be calculated in an executable program (names of other variables are set by a programmer at the stage of program creation). There is an option in the client terminal to open the toolbar "Global Variables" where in real time mode one can see all currently open GlobalVariables and their values. This toolbar is available via client terminal menu Service >> Global Variables (F3 key): After all EAs have been detached, this toolbar does not contain any records about open global variables of client terminal. Errors in Using GlobalVariables Such algorithmic errors are dangerous because they are not always obvious and it is hard to detect them. But this does not mean that a user should refuse using GlobalVariables. However, it means that a code of any program must be constructed correctly taking into account all events that can influence the program performance. Using global variables in practical work may be very helpful: for example, this helps to inform about critical events on another security (reaching of a certain price level, its breaking, etc.), about attachment of another Expert Advisor (with the purpose of sharing authority), conduct synchronized trading upon several securities at the same time. Global variable of client terminal can also be created from an indicator that calculates some important events; the value of such a variable can be used by any operating Expert Advisor or script

Arrays
A large part of information processed by application programs is contained in arrays.

Concept of Arrays
Array is an arranged set of values of one-type variables that have a common name. Arrays can be one-dimensional and multidimensional. The maximum admissible amount of dimensions in an array is four. Arrays of any data types are allowed. Array element is a part of an array; it is an indexed variable having the same name and some value.

Indexing
Array element index is one or several integer values indicated in the form of a constant, variable or expression enumerated comma-separated in square brackets. Array element index uniquely defines place of an element in an array. Array element index is indicated after a variable identifier (array name) and is an integral part of an array element. In MQL4 indexing starting form zero is used. The way of specifying indexes when each index is in square brackets is also acceptable:

You might also like