> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Arrays

> Lists of values: creating a typed array, adding and reading elements, looping over one, keeping one across bars, and arrays of drawings and of your own types.

An array is a list of values of one type that can grow and shrink while the script runs. Use one when you need to keep a varying
number of values: the last N prices, the swing points found so far, the labels you have drawn. The `array` namespace holds the
functions (see [array](https://gocharting.com/docs/scripting/reference/functions/array.md)); this page shows how they fit together.

## Create an array

`array.new<type>(size, initial)` creates an array of `size` elements, each set to `initial`; both arguments are optional, so
`array.new<float>(0)` is an empty list. The type in angle brackets may be any fundamental type (`int`, `float`, `bool`, `string`,
`color`), a drawing type (`label`, `line`, `box`, `linefill`, `polyline`, `table`), `chartPoint`, or a type of your own.

```lipi-fragment theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
prices = array.new<float>(0)
names = array.new<string>(3, "none")
marks = array.new<label>(0)
fromValues = array.from(1, 2, 3)
```

`array.new_float(0)`, `array.new_int(...)`, `array.new_label(...)` and their siblings are the same as the angle-bracket form, except that
`array.new_bool(3)` holds `false` where `array.new<bool>(3)` holds `na`; every other type holds `na` until set. To
declare the type of a variable, write `array<float>`:

```lipi-fragment theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
array<float> window = array.new<float>(0)
```

## Add, read and remove

* `array.push(a, x)` adds at the end; `array.unshift(a, x)` at the start.
* `array.get(a, i)` reads the element at index `i`; the first element is index 0 and the last is `array.length(a) - 1`.
  An index outside that range, including a negative one, stops the script when it runs with an error from `array.get`.
* `array.set(a, i, x)` replaces an element.
* `array.pop(a)` removes and returns the last element; `array.shift(a)` the first.
* `array.length(a)` is the number of elements. There is no `array.size`.
* An array holds at most 10,000 elements; growing it past that stops the script when it runs.

The functions can also be called on the array itself: `a.push(close)` is `array.push(a, close)`, and `a.length()` is
`array.length(a)`. A name the array namespace does not have is refused either way, for example `a.size()`: Could not find any
type function `size` for `a`.

`a[1]` is not the element at index 1: `[]` on an array is the history operator, so `a[1]` is the array as it was on the previous
bar. Use `array.get(a, 1)` for an element.

## Keep an array across bars

A plain declaration creates a new array on every bar. Declare it `static` to create it once and keep adding to it:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average of the last 5 closes")
static array<float> window = array.new<float>(0)
array.push(window, close)
if array.length(window) > 5 {
    array.shift(window)
}
plot(array.avg(window), title="Average of last 5")
```

## Loop over an array

`for x in a` visits each element in order; `a` must be an array variable, not an expression. Changing `x` inside the loop does
not change the element: use `array.set` for that. Prefer `for x in a` to a counted loop over the indexes: a counted loop does not
run its body when the start and the end are equal (see [Loops](https://gocharting.com/docs/scripting/language/repetition.md)), so `for i = 0 to
array.length(a) - 1` skips a one-element array.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Sum by loop")
static array<float> moves = array.new<float>(0)
array.push(moves, close - open)
float total = 0
for m in moves {
    total += m
}
plot(total, title="Sum of moves")
```

## Arrays of drawings

An array of drawings lets a script keep a fixed number of them and delete the oldest:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last three marks", overlay=true)
static array<label> marks = array.new<label>(0)
if barstate.isconfirmed and close > open {
    array.push(marks, label.new(bar_index, high, "Up"))
    if array.length(marks) > 3 {
        label.delete(array.shift(marks))
    }
}
```

For arrays of your own types, see [Custom types and enums](https://gocharting.com/docs/scripting/language/custom-types.md).

## Arrays are shared, not copied

Assigning an array to another name gives a second name for the same array: `b = a` and then `array.set(b, 0, x)` changes `a`
too. `array.copy(a)` returns an independent array with the same elements.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.