Posted By:
30 Jul, 2014 9:20 pm PDT

Arrays in MetaTrader


Suppose you want to remember five prices. What should we do? Well, we can do this:

doubleprice1 = 1.2341;

doubleprice2 = 1.2321;

doubleprice3 = 1.2361;

doubleprice4 = 1.2411;

doubleprice5 = 1.2301;

We get five variables, which have one data type and describe one parameter – price. We can go another way, using an array. An array is a set of variables, which differ in indexes, but have one name. This is how we declare an array from five elements:


Common form:


(array type) (array name) [number of elements];


In our case: array type – double, name – price, number of elements – 5. Let’s see how we can refer to the array elements:

doubleprice[5];// declare an array of 5 elements
price[0] = 1.2341; // refer to the first element of the array and 
// assign a price to it. Note 
// that the index of the first element starts with 0 
// It is an important feature,
// you should get used to it.
price[1] = 1.2321;// refer to the second element 
price[2] = 1.2361;// and so on 
price[3] = 1.2411; 
price[4] = 1.2301;

We can perform any operations with the array elements, like with common variables. Actually, elements of the array are common variables:

price[0] = 1.2234; 
price[1] = 1.2421;
MessageBox("Middle price is " + (price[0] + 
price[1]) / 2.0,"middle price");

When declaring an array we can assign initial values to all elements:

doubleprice[2] = {1.2234, 1.2421};

We simply enumerate the initial values of elements comma separated in braces. In such a case you may let a compilator automatically put the number of elements, instead of writing it yourself:

doubleprice[] = {1.2234, 1.2421};



Integrated or Built-in Arrays

High[0];// refer to the last maximal price, 
// that the bar has achieved in the current timeframe 
// and current currency pair. Currency pair and 
// the timeframe depend on the chart, on which you 
// have started a script. It is very important to remember! 
Low[0];// minimal price of the last bar 
// in the current chart. 
Volume[0]; // value of the last bar in the current chart.
Was it useful?  

Please login to reply to this problem.

Related Problems on Fixoncloud