# History (/scripting-v2/history)



Every exact source selector has its own retained buffer.

## Retained Records [#retained-records]

```python
records = history.source("btc@candles@binancef:timeframe=60")
```

The list is ordered oldest to newest. An unknown selector or empty buffer returns `[]`.

Returned dictionaries and lists are copies. Changing them does not modify Market Lab's internal history.

## Indexed Records [#indexed-records]

```python
latest = history.source("btc@candles@binancef:timeframe=60", 0)
previous = history.source("btc@candles@binancef:timeframe=60", 1)
missing = history.source("btc@candles@binancef:timeframe=60", 99)
```

Index `0` is newest and `1` is previous. An unavailable index returns `None`. Indexes must be non-negative integers.

## Retention [#retention]

The manifest's `lookback` sets capacity independently for every exact selector:

```python
script = {
    "name": "lead-lag",
    "version": "2",
    "lookback": 200,
}
```

The effective range is 2 to 5,000 records, with 5,000 as the default. `lookback` is a capacity, not a preload request.

Live jobs begin with empty buffers. Backtests begin at `--from`. Always check that enough records exist:

```python
candles = history.source("btc@candles@binancef:timeframe=60")
if len(candles) < 21:
    return
```

## Multiple Sources [#multiple-sources]

```python
BTC = "btc@candles@binancef:timeframe=60"
ZEC = "zec@candles@binancef:timeframe=60"


def on_data(ctx, history):
    if ctx.symbol != "btc":
        return

    btc = history.source(BTC, 0)
    zec = history.source(ZEC, 0)
    if btc is None or zec is None:
        return
```

Backtests merge configured sources into timestamp order. A record enters history immediately before its own `on_data` call, so future records are never visible. Configured source order breaks same-timestamp ties.

Candle-like updates with the same timestamp replace the current record instead of shifting the buffer. Index `1` therefore remains the previous completed period.

See [Sources](/scripting-v2/sources) for selectors and [Data Types](/scripting-v2/data-types) for record shapes.
