MQL4 Book  Variables  Predefined Variables and RefreshRates Function

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. Let's illustrate updating of predefined variables on the following example (Expert Advisor predefined.mq4):

//--------------------------------------------------------------------
// predefined.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
int start() // Special funct. start
{
Alert("Bid = ", Bid); // Current price
return; // Exit start()
}
//--------------------------------------------------------------------

Starting this program it is easy to see that values of the variable Bid displayed in alerts will be equal to the current price each time. The same way you can check values of other variables depending on the current conditions. For example, the variable Ask also depends on the current price. The value of the variable Bars will also change if the number of bars changes. This may happen at a tick, at which a new bar is formed in a chart window. The value of Point depends on a security specification. For example, for EUR/USD this value is 0.0001, for USD/JPY it is 0.01. Value of Digits for these securities is equal to 4 and 2 respectively.

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.



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

Let's illustrate RefreshRates() execution on an example.

Problem 21. Count the number of iterations that a cycle operator can perform between ticks (for the nearest five ticks).

This problem can be solved only using RefreshRates() (script countiter.mq4 ):

//--------------------------------------------------------------------
// countiter.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
int start() // Special funct. start()
{
int i, Count; // Declaring variables
for (i=1; i<=5; i++) // Show for 5 ticks
{
Count=0; // Clearing counter
while(RefreshRates()==false) // Until...
{ //..a new tick comes
Count = Count+1; // Iteration counter
}
Alert("Tick ",i,", loops ",Count); // After each tick
}
return; // Exit start()
}
//--------------------------------------------------------------------

According to the problem conditions, calculations should be made only for the nearest five ticks, that is why we can use a script. Two variables are used in the program: i - for counting number of ticks and Count - for counting iterations. The external cycle for is organized in accordance with the number of processed ticks (from 1 to 5). At the for-cycle start the counter of iterations (performed in the cycle while) is cleared, at the end an alert with a tick number and amount of its iterations is shown.

Internal cycle 'while' will operate while value returned by RefreshRates() is equal to false, i.e. until a new tick comes. During operation of 'while' (i.e. in the interval between ticks) the value of Count will be constantly increased; thus the wanted number of iterations in 'while' cycle will be counted. If at the moment of condition checking in 'while' value returned by RefreshRates() is 'true', it means there are new values of predefined variables in the client terminal, i.e. a new tick has come. As a result control is returned back outside 'while' and iterations counting is thus finished.

As a result of countiter.mq4 script execution a number of alerts characterizing MQL4 performance can appear in a security window:


Fig. 53. Results of countiter.mq4 operation in EUR/USD window.

It is easy to see that for 1 second (interval between the fourth and the fifth ticks) the script has performed more than 3 million iterations. Analogous results can be acquired by simple calculations for other ticks as well.

Let's get back to the previous example (Expert Advisor predefined.mq4). Earlier we saw that if RefreshRates() is not executed in Expert Advisor 2, values of local copies of predefined variables will remain unchanged during the whole period of start() execution, i.e. for example during the period t 2 - t 32. If after the third tick (that comes when start() is being executed) function RefreshRates() is executed, for example at the moment t 31, values of local copies will be updated. So, during the remaining time starting from t 31 (RefreshRates() execution) to t 32 (end of start() execution), new values of local copies of predefined variables equal to values defined by a client terminal at t 3 will be available to the Expert Advisor 2.

If in Expert Advisor 2 RefreshRates is executed at the moment t 11 or t 21 (i.e. in the period when the last known tick is the one that has started the execution of start()), local copies of predefined variables will not be changed. In such cases current values of local copies of predefined variables will be equal to last known, namely to those that were defined by the client terminal at the moment of the last start of the special function start().