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

# State

> Values kept between bars: cells, lists, maps, series and tallies.

Values that persist between runs. Declared at the top level only. A local variable inside a handler starts fresh on every run. Anything that must persist is declared here, with its lifetime, so the engine can roll it back correctly.

## state

<ResponseField name="bar()" type="<T extends StateValue>(initial: T): Cell<T>">
  Kept across bars; rolled back before each update of the forming bar (Pine's `var`).
</ResponseField>

<ResponseField name="live()" type="<T extends StateValue>(initial: T): Cell<T>">
  Kept across bars and across updates of the forming bar (Pine's `varip`).
</ResponseField>

<ResponseField name="list()" type="<T extends StateValue>(capacity: number, lifetime?: Lifetime): StateList<T>">
  A list of at most `capacity` values with a lifetime, default `'bar'`.
</ResponseField>

<ResponseField name="map()" type="<T extends StateValue>(capacity: number, lifetime?: Lifetime): StateMap<T>" />

## StateValue

```ts theme={"theme":"github-dark"}
type StateValue = number | boolean | string | null;
```

## Cell

<ResponseField name="value" type="T" />

## Series

A number with history, made with `series(lookback)`.

<ResponseField name="set()" type="(value: number): void">
  Sets the value for the current bar. Unset bars are `na`.
</ResponseField>

<ResponseField name="value" type="number">
  The current bar's value.
</ResponseField>

<ResponseField name="ago()" type="(n: number): number">
  The value `n` bars ago, `n` at most the declared lookback.
</ResponseField>

## Tally

<ResponseField name="add()" type="(amount: number): void" />

<ResponseField name="value" type="number">
  The sum so far in the current bar.
</ResponseField>

<ResponseField name="count" type="number">
  How many amounts were added in the current bar.
</ResponseField>

## TallyBy

<ResponseField name="add()" type="(key: string, amount: number): void">
  Add to `key`'s sum for this bar. A new key past the capacity is dropped for the rest of the bar.
</ResponseField>

<ResponseField name="get()" type="(key: string): number">
  `key`'s sum so far in this bar, 0 when it has none.
</ResponseField>

<ResponseField name="size" type="number">
  How many keys have a sum this bar.
</ResponseField>

<ResponseField name="entries()" type="(): IterableIterator<[string, number]>">
  Every key with its sum, in the order the keys were first added.
</ResponseField>

## StateList

<ResponseField name="length" type="number" />

<ResponseField name="get()" type="(index: number): T">
  The value at `index`, from 0 at the oldest.
</ResponseField>

<ResponseField name="set()" type="(index: number, value: T): void" />

<ResponseField name="push()" type="(value: T): void">
  Add at the end. Past the capacity the script stops (OX2004): `shift` the oldest first.
</ResponseField>

<ResponseField name="pop()" type="(): T">
  Take from the end.
</ResponseField>

<ResponseField name="shift()" type="(): T">
  Take from the start, the oldest.
</ResponseField>

<ResponseField name="unshift()" type="(value: T): void">
  Add at the start.
</ResponseField>

<ResponseField name="clear()" type="(): void" />

<ResponseField name="toArray()" type="(): T[]">
  A copy as a plain array, oldest first.
</ResponseField>

## StateMap

<ResponseField name="size" type="number" />

<ResponseField name="get()" type="(key: string): T | undefined">
  The value for `key`, or `undefined` when there is none.
</ResponseField>

<ResponseField name="set()" type="(key: string, value: T): void">
  Set the value for `key`. A new key past the capacity stops the script (OX2004).
</ResponseField>

<ResponseField name="delete()" type="(key: string): boolean">
  Remove `key`; true when it was there.
</ResponseField>

<ResponseField name="has()" type="(key: string): boolean" />

<ResponseField name="clear()" type="(): void" />

<ResponseField name="entries()" type="(): IterableIterator<[string, T]>" />

## Lifetime

```ts theme={"theme":"github-dark"}
type Lifetime = 'bar' | 'live';
```

## series()

A number with history: `lookback` bars kept, at most 5,000.

```ts theme={"theme":"github-dark"}
function series(lookback: number): Series;
```

## tally()

A sum of what event handlers add during a bar. Starts at 0 on each new bar; never rolled back.

```ts theme={"theme":"github-dark"}
function tally(): Tally;
```

## tallyBy()

A sum per key (such as a taker order id) during a bar, at most `capacity` keys. Empty on each new bar.

```ts theme={"theme":"github-dark"}
function tallyBy(capacity: number): TallyBy;
```

## Next step

Continue with [Indicators (ta)](/terminal/reference/ta).


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