MultiCharts | PowerLanguage Tutorials | CH7: Array

Array

An array can be thought of as a container that stores multiple values of the same type.
Each value is called an element and each element is given an index.
The index starts from 0 and in a sequency from frist element to the last increases by 1 each time.
Hence, the index of the first element is 0, the index of the second element is 1, the index of the third element is 2, … , the index of the Nth element is N- 1.

Index012N-1
Array1st element2nd element3rd elementNth element

Similar to variables, arrays are also divided into two phases: declaration and use.

Array can be divided into static array and dynamic array.
The size of static array cannot be changed after declaration, while the dynamic array can be changed after through the function Array_SetMaxIndex.
The dimension of the array specified when declared determines it is a static array or a dynamic array.
If the dimension is specified, it is a static array.
Otherwise, it is a dynamic array.

Declaration

Declare the name, size, and dimensions of the array, give all elements an initial value which determines their types (Numerical, String, or TrueFalse).

Array:<IntraBarPersist>ArrayName1[D1,D2,D3,etc.](InitialValue1<,DataN>)
      ,<IntraBarPersist>ArrayName2[D1,D2,D3,etc.](InitialValue2<,DataN>)
      ...;

Array: Declare an array.

IntraBarPersist – Recalculate the element value every time a trade is filled (update by tick).
This parameter can be omitted. If it is not specified, the default is to recalculate the element value at the end of bar (update by bar).

ArrayName – The name of the array. The name can be composed of letters, underscores, underscores, numbers, and periods.
The name is not case-sensitive.
The name cannot start with a number or a period.

– Specify the size and dimension of the static array.
The value of D starts from 0.
Specify multiple dimension values (D1, D2, D3…) to represent multi-dimensional static arrays .
If dimension is not specified, it means to declare a dynamic array.
The initial size of the dynamic array is one and the dimension of the dynamic array will only be one.
Use the function Array_SetMaxIndex to re-size the dynamic array.

InitialValue – The initial value of all elements. The initial value determines the types (Numerical, String, or TrueFalse).

DataN – Specifies the DataN the array is reference to.
If it is not specified, the default DataN is referenced.

The reserved word array is equivalent to arrays.

Use

ArrayName[I1,I2,I3] specify which element of the array to use.

ArrayName[I1,I2,I3]..

I1 – index of 1st dimension, I2 – index of 2nd dimension, I3 – index of 3rd dimension, and so on.

Example

Declare Factor is a static array of size is 9 and initial vlaue is 0.
Using the for loop, the index value is assigned to the (index+1)th element of the Factor.
The 1st element is 0, the 2nd element is 1, … , the 9th element is 8.

Array: Factor[8](0);
var: index(0);
for index=0 to 8 begin
	Factor[index]=index;
end;

Declare Level is a two-dimension static array of size is 6×7 and initial value is 0.
Using the for loop, the d1_index+d2_index value is assigned to the ((d1_index+1)x(d2_index+1))th element of the Level.
The 1x1th element is 0, the 1x2th element is 1, … , the 6x7th element is 11.

Array:Level[5,6](0);

var: d1_index(0), d2_index(0);
for d1_index=0 to 5 begin
	for d2_index=0 to 6 begin
		Level[d1_index,d2_index]=d1_index+d2_index;
	end;
end;

Declare Alpha is a dynamic array of size is 1 and initial value is 0.
Use Array_SetMaxIndex to change the size of Alpha to 9.
Using the for loop, the index value is assigned to the the (index+1)th element of the Alpha.
The 1st element is 0, the 2nd element is 1, … , the 9th element is 8.

Array: Alpha[](0);
var: index(0);
Array_SetMaxIndex(Alpha,8);
for index=0 to 8 begin	
        Alpha[index]=index;
end;

Common error: Array bounds, Wrong index value

A common error with an array is Array bounds, Wrong index value : x.
This error occurs when the index value used does not exist in the array.
x – Integer representing the index value where the error occurred.

Example

Declare Factor is a static array of size is 9 and initial vlaue is 0.
Using the for loop, access the Factor elements sequentially from index 0 to 10.

The index range of the Factor is 0~8.
While the for loop execute to the index 9, the error: Array bounds, Wrong index value : 9 is generated because the index 9 is not within the index range 0~8.

Array: Factor[8](0);
var: index(0);
for index=0 to 10 begin
	Factor[index]=index;
end;

Example

Another common mistake leading to Array bounds, Wrong index value is that after the dynamic array is declared, does not call Array_SetMaxIndex to re-sized to the correct size.

Declare Alpha is a dynamic array of size is 1 and initial value is 0.
Using the for loop, access the Alpha elements sequentially from index 0 to 8.

However, because Array_SetMaxIndex is not used to modify the size of the Alpha, the index range of the Alpha is 0.
While the for loop execute to the index 1, the error: Array bounds, Wrong index value : 1 is generated because the index 1 is not within the index range 0.

Array: Alpha[](0);
var: index(0);
//Array_SetMaxIndex(Alpha,8); //not use Array_SetMaxIndex
for index=0 to 8 begin	
        Alpha[index]=index;
end;

Reference

https://www.multicharts.com/trading-software/index.php?title=Array

https://www.multicharts.com/trading-software/index.php?title=Array_SetMaxIndex

Leave a Reply