Storing data in variables


for example, forex

Any expert advisor operates with some data — prices, values of indicators, volumes, number of open positions etc. Those places where these data are stored in the expert advisor are called variables. Digits (0-9), Roman letters of upper and lower cases (a — z and A — Z, it should be remembered that letters ’a’ and ’A’ are completely different characters), underline character (_) can be used as names of variables. The first symbol can’t be a digit. Also the name of a variable mustn’t coincide with a reserved word (i.e. a word that has a special meaning in MetaQuotes Language 4 — but we will speak about it later).

It is desirable to use as a name of a variable such a name from which it is easy to understand at once the intended purpose of the variable. For example a variable containing the number pf open positions can have such a name: OpenPositionsNumber.

  • It is possible to create variables of different types:
  • int — integer (from -2147483648 to 2147483647);
  • bool — logical type (either false or true);
  • datetime — date and time (in the following format D’YYYY.MM.DD HH:MM:SS’);
  • color — color (I will write about the format in the next articles);
  • double — real number (-1.7 × 10-308 to 1.7 × 10308, accuracy — 15 significant digits);
  • string — a line delimited by the double quotes (for example, «this is a line»).

A variable must be declared before being used. This can be done in several ways:

type     name;
or
type    name = initial_value;

Examples of variable declarations :

int Count;
datetime InitialDate = D'2006.07.12 00:00';
string  ip_address = "127.0.0.1";

Later on in order to address to the value of the variable it will be necessary only to indicate its name:

i = 5 + Count;

Sometimes it is required to store not one value, but a certain number of interrelated values. For example the value of indicator on the current bar, on the previous bar, … , N-bars ago. For such purposes arrays are used. An array —is a data structure consisting of a group of elements that are accessed by indexing. Each element has the same data type.

Suppose that in our expert advisor there is the following description of the array Prices:

double Prices[50];

It means that the array variable Prices gives access to 50 elements of the type double. To access the i-th element one should indicate Prices[i]. Numeration of elements starts at zero and ends at 49-th element (in our case).

If you try to address an element out of range the following error will be registered ERR_ARRAY_INDEX_OUT_OF_RANGE (4002), which can be received with the help of the GetLastError() function.

In case of necessity not only oordinary arrays indexed by a single integer, but also multi-dimensional arrays can be used.

Here is an example of the description of a two-dimensional array, consisting of six arrays each of which consists of 50 elements.:

int mas[6] [50];

When you describe an array you can set the initial values for each element in braces:

int    a[4][4] = { 1, 1, 1, 1,  2, 2, 2, 2,  3, 3, 3, 3,  4, 4, 4, 4 };

Initial values of array elements can be only constants. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type.

Local and static variables

  • So there is only one unconsidered issue left – scopes of variables. The point is that variables can be of several types:

  • local variables,
  • static variables,
  • global variables,
  • external variables,
  • parameters of functions.

Local variables

A local variable is a variable that is declared within a function and such variables are visible only within aren’t accessible.

Every call of function executes the initialization (defining of an initial value) of local variables. Local variables can be initialized by any expression, and not just a constant.

An example of a local variable declaration and initialization:

int CalcFactorial(int n)
  {
    int i = 0;
    ...
  }

Static variables

Static variables are declared by directive static. They are initialized only once at the first call of the function and preserves their value even on exit from the function. Next time when the function is called they will have the same value which they have before exit from the function last time.

Static variables are declared within a function declaration and that’s why they are accessible only from the function in which they are declared.

An example of a static variable declaration:

int GetOpenPositionsNumber()
  {
    static int Count = 0;
    ...
  }

External variables

We have already dealt with external variables when we described such parameters of the expert advisor as MAPeriod and LotsNumber in the code our first expert advisor.

External variables are declared by the keyword extern:

   extern int       MAPeriod=13;
   extern double    LotsNumber=1.0;

External variables can’t be arrays.

External variables are an expert advisor’s parameters that can be changed.

«Attach» the expert advisor to the chart. For this click the right mouse-button on the name of the expert advisor in the «Navigator» window and in the appeared contextual menu choose «Attach to the chart». The window of the expert advisor’s properties will appear. In the tab «Input parameters» You can change the values of external variables, described in the expert advisor (see picture 1).

Picture 1. Expert Advisor's parameters
Picture 1. Expert Advisor's parameters

If the expert advisor has been already «attached» to the chart (in the top-right corner You can see a smiley or sad face), then in order to change its parameters — values of external variables — click the right mouse-button on the face and in the appeared contextual menu choose the item «Expert Advisors — Properties …». There is a quicker way – to use the hotkey F7. The window with the parameters of the expert advisor will appear (see picture 1), in which You will be able to change the values of external variables.

Global variables

For a person who has already written expert advisors, such expression as «global variables» can be a little bit confusing. The point is that in fact there are two types of «global variables»:

  • variables visible from any function of ONE expert advisor (i.e. they are global within this expert advisor); and
  • variables that are common for ALL expert advisors.

In this article we will speak about global variables of the first type – those that are common for all the functions of ONE expert advisor.

The second type of global variables will be considered in the following articles, when I will speak about functions with the help of which such variables are accessed, — GlobalVariable…().

If You want the value of a variable to be addressed from different functions of an expert advisor make it a global variable, i.e. define it at the same level as functions:

   //---- global functions
int CurrentState;
int MyOrderTicket;

//---- functions definitions
int init()
{
...
}

Initial values of global variables can be only constants (If there is no explicit initialization they are initialized by 0). Initialization of global variables is made only once before execution of "init()" function.


Next article: "Parameters of functions"

+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?