Using Arrays

An array is a data structure that holds a collection of values of a single type. The individual elements of an array can be accessed by indexing. Multi-dimensional arrays can be used as well in ActFX. They are accessed using more than one index: one for each dimension.

 

In order to use an array in your strategy, you must declare it in the variables section. See examples below:

 

Example1:

 

var

A: array of Double;

 

In this example, the array has only 1 element, indexed 0.

 

Example2:

 

var

A: array [1..7] of Double;

 

In this example, the length of an array is established when the array is created. The array has 7 elements, indexed from 1 to 7.

 

The SetLength Function can be used to change the length of an array, by augmenting the end value of the index. The first index remains as  is, while the last index is changed into the indicated value.

 

Example: We need to add the close values of the candles on the chart to the array when a new candle appears in the chart. We will dynamically cahgne the size of an array depending on the number of used candles. The script should include the following lines:

 

procedure OnNewCandle;

var

A: array of Double;

i: Integer;

 

begin

 SetLength(A, History.Count);

 

 for i:=0 to History.Count-1 do

  A[i]:= History.Last(i).Close;

end;