◆ New — Strategy Validator, Code Fixer & Prop Compliance Checker are live. Try one free →
Home / Pine errors / Series/history
Pine Script v6 error · Series/history

Pine Script "The requested historical offset is beyond the historical buffer's limit" — cause & fix

The error
The requested historical offset (N) is beyond the historical buffer's limit (M).

The script asked for a past value further back than the historical buffer allocated for that series, raising a runtime error. Educational only — this reference explains the buffer limit and na-safe history access; no profitability is implied.

Why it happens

Each series keeps a bounded historical buffer (up to about 5000 bars for most series, larger for built-ins like open/high/low/close). Referencing a value with [] at an offset deeper than that buffer — or, commonly, anchoring a drawing to a bar far in the past that only becomes reachable on the first realtime tick — throws this runtime error. Reading history on early bars where the value is still na (offset larger than the number of bars elapsed) is the other frequent trigger.

How to fix it

Keep offsets within the buffer and guard early bars. Clamp the offset against bar_index so you never read past the start of the dataset, and handle na with na()/nz() before using the value. If you legitimately need a deep, fixed lookback, raise the buffer with the max_bars_back parameter of indicator()/strategy() or with max_bars_back(series, N). Gate any signal that depends on the historical value with 'and barstate.isconfirmed'.

What triggers it

//@version=6
indicator("Offset beyond buffer", overlay = true)
// On early bars there are fewer than 5000 bars of history, and the
// requested offset exceeds the buffer -> runtime error.
oldClose = close[5000]
plot(oldClose)

The fix, in code

✓ Validated clean by our own engine

//@version=6
indicator("Safe historical offset", overlay = true, max_bars_back = 500)

offset = input.int(300, "Lookback bars", minval = 1, maxval = 500)

// Clamp the offset to available history so it never exceeds the buffer,
// and guard the na case on early bars with nz().
int safeOffset = math.min(offset, bar_index)
float pastClose = nz(close[safeOffset], close)

plot(pastClose, "Past close", color = color.teal)

plotchar(close > pastClose and barstate.isconfirmed,
     title = "Above lookback", char = "▲", location = location.abovebar, color = color.green)

Related errors

Educational, code-correctness reference only — not financial advice. Trading involves substantial risk of loss.

Catch the bug that compiles.Run auditGet Pro