In this article we will continue to talk about variables, explaining their purpose and some of the nuances associated with their use.
Pre-defined variables
In MQL 4 built predefined variables (predefined variables), such as Ask, Bid, Bars, Close, Open, High, Low, Time and Volume.
These variables are "tied" to a particular schedule, any of the tools offered by you on each of its time intervals has its own set of predefined variables mentioned above. Access to the values of these variables from the user program, attached to the schedule of a time period can be made at any time. Change the value of these variables is not possible.
It should be noted that the variables Close, Open, High, Low, Time and Volume are not single values, and arrays containing the historical data for the reporting tool. Access to the latter (we call this value "the youngest") of any of these variables is carried out on the variable name with an indication of the zero index. For example, the most recent sale price traded instrument is equal to Ask [0].
Variables Bid and Ask are relevant only to the current bar, so they are not arrays. It stores information about the proposed price of the seller and requested a price on a purchase.
Price schedules adopted seen across some time periods - the bars.
Bar - this is a graphic representation of the price of one period. Bar has its price discovery (the Open) - the price, which has been on the market at the time of the interim period, depicts a bar, the maximum (the High) and minimum (the Low) price achieved in the time bar and the closing price of the bar (the variable Close) - the price of traded instrument on the market at the end of the interim period, depicts a bar. The following figure explains what the graph correspond to these variables.
As we already mentioned, the rooms are convenient to think of bars, as they age. The current bar has not yet been formed before the end, and therefore its age is equal to zero. Bar, which was formed at the previous time interval, is the age of one period, and so on. In the above figure the number of bars shown with blue numbers, and can be seen as right to left with increasing age of the bars are increasing their numbers.
To find out how many bars are available to us in the program, we can use predefined variables Bars. To understand better the figure missed the average bars (instead of them, you will see the three-point bold), and left the last few bars. As you can see, the first bar on the left (the most senior in age) has a number equal Bars-1, the second from the left bar is the number equal Bars-2, and so on.
Many were put to a standstill by the fact that the oldest bar number is equal Bars-1. This is due to the fact that the numbering of the bars is not with the unit, and from scratch. Suppose, with ten bars, numbered from zero, we get the following set of their indices:
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
As a result, the variable will contain a number of Bars bars (ten), and the most senior bar code will be equal Bars-1 (nine).
Array of Time contains the time periods depicted the opening bars.
The array contains the values of Volume ticks volume bars. They can be judged only on the number of transactions rather than on the actual volume of goods sold or purchased, whether the currency, stocks or other traded instruments.
Global Variables
Global variables (global variables) - a convenient tool for exchanging information between programs or to save the numerical values of your program at the time of shutdown or MetaTrader'a computer.
To create or change the value of global variable, you need to call GlobalVariableSet (). This function is create a global variable, if all of a sudden it does not already exist, as in the case when the variable already exists, change its value. The first parameter of this function is the name of global variable, the second parameter - a numeric value.
In order not to wonder whether or not already created a global variable with a certain name, you can use the GlobalVariableCheck (). Usually functions GlobalVariableSet () and GlobalVariableCheck () used together for the first initialization of a global variable as follows:
/ / If there is no global
/ / Variable with the name "g1", then
/ / Create it and assign it to
/ / Value 12345
if (! GlobalVariableCheck ( "g1")) (
GlobalVariableSet ( "g1", 12345);
)
Get the value of global variable using the function GlobalVariableGet ().
Remove a global variable using the function GlobalVariableDel (). If you need to delete all global variables that are present in the current system, you can do so by calling GlobalVariableDeleteAll ().
Variable scope
Devoting little attention to variables that are used to store the values when writing programs. In the article "Programming in MQL 4: Variables", printed in 21 issue of the journal Forex Magazine, we provide brief guidance on the application of the rules of naming, and now we have a little more space. Perhaps the readers have already noticed that in many programs, some variables have the prefix "g_", and some do not have this prefix. Try to explain what it involves.
When writing a program is necessary to take into account the so-called "scope" variable. The point is that in the original text of the program also have their global variables and local variables. Local variables are those declared inside any function. Scope of such variables is the function of the body enclosed in curly brackets, inside which they are declared. Global variables are the same - it is those who declared outside the body of a function, they are available to all functions of the file, which saved the program.
In order to facilitate understanding of the original text and the process of debugging programs, the names of global variables, ie variables that have a scope the entire file, beginning with the prefix "g_". This is not compulsory, but believe that adherence to this recommendation, you will greatly facilitate the process of writing the program and save your nerves when debugging. The example below shows the declaration of global and local variables:
/ / Global variable
int g_nLots;
void Function1 (int param1)
(
/ / Local variable
int nLots;
/ / ...
/ / Perform some action
/ / ...
);
By the way, the parameters passed to the function (in this case, param1), have the status of local variables and the area of visibility - the body functions.
If it does happen that the global and local variables will be named the same, the local variable is still in its visibility, it will be used. After exiting the scope of a local variable, the program will use a global variable.
/ / Declare a global
/ / Variable nLots
int nLots = 0;
void Function1 ()
(
/ / Declare a local
/ / Variable nLots
int nLots;
/ / ...
/ / Reference to the local
/ / Variable nLots
nLots = 100;
);
void Function2 ()
(
/ / Reference to global
/ / Variable nLots
nLots = 3;
)
It is clear that given the same name global and local variables are not best practice, we recommend you adhere to the data in this article additions to the naming of variables.
Alexander Ivanov
to Forex Magazine
to Forex Magazine
No comments:
Post a Comment