Array#
An array can be thought of as a container that stores multiple values of the same Type. Each value is called an element. From the first element to the last element, each is given an index in order, starting from 0 and incrementing.
| Index | 0 | 1 | 2 | … | N-1 |
|---|---|---|---|---|---|
| Array | 1st element | 2nd element | 3rd element | … | Nth element |
Arrays, like Variables, are divided into declaration and use phases.
Arrays are divided into static arrays (Static Array) and dynamic arrays (Dynamic Array):
| Static Array | Dynamic Array | |
|---|---|---|
| Declaration | Specify dimensions, e.g., [8], [5, 6] | Do not specify dimensions, e.g., [] |
| Size | Cannot be changed after declaration | Can be changed via Array_SetMaxIndex |
| Dimensions | Can be one-dimensional or multi-dimensional | Can only be one-dimensional |
| Initial Size | Specified at declaration | 1 (index only has 0) |
Declaration#
Declare the name, size, and dimensions of the array, and assign an initial value to all elements. The initial value determines the Type.
Syntax#
| |
| Syntax Element | Optional | Description |
|---|---|---|
Array | Required | Declares an array. Synonym: Arrays |
IntraBarPersist | Optional | Recalculates the element value on every tick (update by tick). By default, recalculates only at the end of each bar (update by bar) |
ArrayName | Required | The name of the array. Can consist of letters, underscores, numbers, and periods (.). Not case-sensitive. Cannot start with a number or period (.) |
D | Optional | Dimension, specifies the size of a static array. Index starts from 0. Specifying multiple dimensions (D1, D2, D3…) represents a multi-dimensional static array. If no dimension is specified, declares a dynamic array |
InitialValue | Required | The initial value of all elements. The initial value determines the Type, which can be Numerical, String, or TrueFalse |
DataN | Optional | Specifies the Data the array is bound to. By default, binds to the default Data |
Use#
Use ArrayName[I1, I2, I3] to specify which element of the array to use.
| |
| Syntax Element | Description |
|---|---|
ArrayName | The name of the array |
I | Index. I1 is the first dimension index, I2 is the second dimension index, I3 is the third dimension index, and so on |
Example#
One-Dimensional Static Array#
Declare Factor as a static array with a size of 9 (index 0~8) and an initial value of 0. Use a for loop to set each element’s value to its index.
| |
Two-Dimensional Static Array#
Declare Level as a two-dimensional static array with a size of 6×7 (index 0~5 × 0~6) and an initial value of 0. Use a for loop to set each element’s value to the sum of its two dimension indices.
| |
Dynamic Array#
Declare Alpha as a dynamic array with an initial value of 0 (initial size is 1). Use Array_SetMaxIndex to change the size to 9 (index 0~8), then use a for loop to set each element’s value to its index.
| |
Common Error: Array Bounds. Wrong Index Value#
When the index used does not exist in the array, the error Array bounds. Wrong index value: x is generated, where x is the index value where the error occurred.
Example: Index Out of Range#
Declare Factor as a static array with a size of 9 (index 0~8). Use a for loop to access indices 0 to 10. When execution reaches index 9, it exceeds the range (0~8), generating Array bounds. Wrong index value: 9.
| |
Example: Dynamic Array Size Not Set#
Declare Alpha as a dynamic array with an initial value of 0 (initial size is 1, index only has 0). Without using Array_SetMaxIndex to set the size, when the for loop executes to index 1, it exceeds the range (0), generating Array bounds. Wrong index value: 1.
| |
Reference#
https://www.multicharts.com/trading-software/index.php?title=Array
https://www.multicharts.com/trading-software/index.php?title=Array_SetMaxIndex