> ## 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.

# Keep the last values in an array

> Keep the last N closes in a static array, trim it as bars arrive, calculate from it, and loop over it.

An array holds a list of values that can grow and shrink while the script runs. In this tutorial we keep the last N closes in one,
calculate their average, highest and lowest, and count how many are above the average. Every step is a whole script: replace what
is in the editor, and check it.

### 1. collect the closes

`array.new<float>(0)` creates an empty array of floats and `array.push(window, close)` adds a value at its end. A plain declaration
creates a new array on every bar, so we declare it `static` to create it once and keep adding to it. We plot the length to watch it
grow.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes")
static array<float> window = array.new<float>(0)
array.push(window, close)
plot(array.length(window), title="Values kept")
```

The line rises by one per bar. An array holds at most 10,000 elements, so a script that only pushes would stop there.

### 2. keep only the last N

`array.shift(window)` removes the first, oldest, element. Make the window length an input and trim whenever the array is longer.

```lipi highlight={3,6-8} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes")
length = input.int(10, "Window", minval=2, maxval=100)
static array<float> window = array.new<float>(0)
array.push(window, close)
if array.length(window) > length {
    array.shift(window)
}
plot(array.length(window), title="Values kept")
```

The line rises to 10 and stays there: the array now holds the last 10 closes.

### 3. calculate from the array

`array.avg`, `array.max` and `array.min` work on the whole array. We move to the price bars with `overlay=true` and plot three lines.

```lipi highlight={2,9-11} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes", overlay=true)
length = input.int(10, "Window", minval=2, maxval=100)
static array<float> window = array.new<float>(0)
array.push(window, close)
if array.length(window) > length {
    array.shift(window)
}
plot(array.avg(window), title="Average", color=color.gray)
plot(array.max(window), title="Highest", color=color.green)
plot(array.min(window), title="Lowest", color=color.red)
```

The average sits between the highest and lowest close of the window, as a band that follows price.

### 4. loop over the array

`for value in window` visits each element in order, and does nothing on an empty array. We count the closes above the average and
shade the background when at least 70 percent of them are.

```lipi highlight={9-16,19} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes", overlay=true)
length = input.int(10, "Window", minval=2, maxval=100)
static array<float> window = array.new<float>(0)
array.push(window, close)
if array.length(window) > length {
    array.shift(window)
}
float avg = array.avg(window)
int above = 0
for value in window {
    if value > avg {
        above += 1
    }
}
plot(avg, title="Average", color=color.gray)
plot(array.max(window), title="Highest", color=color.green)
plot(array.min(window), title="Lowest", color=color.red)
bgcolor(above >= array.length(window) * 0.7 ? color.new(color.green, 0.85) : na)
```

## Two mistakes to avoid

**There is no `array.size`.** The count of elements is `array.length`:

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes")
static array<float> window = array.new<float>(0)
array.push(window, close)
plot(array.size(window))
```

The compiler says: Namespace `array` has no function `size`. Did you mean `slice()`?

**An index outside the array stops the script.** `array.get(window, 0)` on an empty array passes the check and stops the script when
it runs with an "Index out of bounds" error, and so does an index past `array.length(window) - 1`. Test the length first. Reading
`window[0]` is not an element either: `[]` on an array is the history operator, so use `array.get`.

## What we built

An indicator that keeps the last N closes in a `static` array, trims it with `array.shift`, plots the average, highest and lowest of
the window, and shades the background when most of the window sits above its average.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last closes", overlay=true)
length = input.int(10, "Window", minval=2, maxval=100)
static array<float> window = array.new<float>(0)
array.push(window, close)
if array.length(window) > length {
    array.shift(window)
}
float avg = array.avg(window)
int above = 0
for value in window {
    if value > avg {
        above += 1
    }
}
plot(avg, title="Average", color=color.gray)
plot(array.max(window), title="Highest", color=color.green)
plot(array.min(window), title="Lowest", color=color.red)
bgcolor(above >= array.length(window) * 0.7 ? color.new(color.green, 0.85) : na)
```

## Next

> [card] [Arrays](https://gocharting.com/docs/scripting/language/arrays.md)
>
> every array function and the rules for arrays of drawings and of your own types.

> [card] [Values that persist across bars](https://gocharting.com/docs/scripting/evaluation/persistence.md)
>
> `static` and `intra`.


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