Edgoro
Runtime error · Runtime limits

Pine Script max_bars_back error: historical buffer limit

Fix the Pine Script runtime error "The requested historical offset is beyond the historical buffer's limit" with max_bars_back(), and size it correctly.

Runtime error on the chart
The requested historical offset (X) is beyond the historical buffer’s limit (Y)

Cause

The script reads a value further back with [] than the history Pine kept for that series. Pine sizes each history buffer from the offsets it sees on historical bars; a larger offset that only appears later, typically on realtime bars, falls outside it.

Broken code

This script compiles, but stops on the chart with the error above.

Pine Script v6
//@version=6
indicator("Close N bars ago")
offset = barstate.isrealtime ? 1000 : 500
plot(close[offset])

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("Close N bars ago")
offset = barstate.isrealtime ? 1000 : 500
plot(close[offset])
max_bars_back(close, 1000)

Why it works

max_bars_back(close, 1000) tells Pine to keep at least 1,000 past values of close from the start, so the realtime offset fits. The call can go anywhere in the script. Size it to the largest offset the script actually uses: oversized buffers slow the script down and use more memory. Most series can be buffered up to 5,000 bars back.

Other fixes, and the older message

  • The max_bars_back parameter of indicator() and strategy() sets the buffer for every series in the script. It is convenient when many series need it, but heavier than sizing only the one that does.
  • Referencing the largest offset you need once on the early bars also makes Pine size the buffer accordingly.

Older Pine versions reported the same problem as Pine Script cannot determine the referencing length of a series. Try using max_bars_back in the indicator or strategy function. The fixes are the same.

Questions

Why does my indicator break only in real time or in Bar Replay?

Pine sizes history buffers automatically while the script loads on historical bars, and doesn't resize them afterwards. An offset that first appears on a realtime bar can exceed the buffer. Set the buffer explicitly with max_bars_back().

Should I set max_bars_back = 5000 everywhere?

No. Larger buffers cost speed and memory. Use the max_bars_back() function on the specific series that needs it, with the smallest size that covers your largest offset.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.