Pine Script max_bars_back error: cannot determine referencing length
Fix the Pine Script error "Pine cannot determine the referencing length of a series" with max_bars_back, or move the history reference out of the branch.
The full TradingView message is "Pine cannot determine the referencing length of a series. Try using max_bars_back in the indicator or strategy function". It means your script looks back in history with [] from inside a conditional branch, so Pine cannot tell how many past bars it needs to keep. The usual fix is to tell it: add max_bars_back = N to indicator() or strategy().
What causes it
Every series in Pine keeps a buffer of past values so that close[1], close[50] and so on can be read. Pine normally sizes that buffer on its own. When the history reference sits inside a conditional branch that runs only some of the time, Pine cannot work out the size, and it stops with this error instead of guessing.
A history reference inside a conditional branch
Here the deep lookback x[2500] only runs once the chart has more than 3,000 bars. This code FAILS on TradingView:
//@version=6
indicator("MBB")
x = close
y = bar_index > 3000 ? x[2500] : x
plot(y)
The ? : operator counts as a branch, the same as an if block. The reference to x[2500] lives on only one side of it, and that side is not taken on the early bars, so the buffer is never sized for it.
ta.barssince on rare events
The same problem is common with ta.barssince() when the event it counts from is rare. The number of bars since a rare event can get very large, and when a script uses a count like that to reach back in history from inside a condition, Pine has no way to know in advance how much history to hold.
How to fix it
You have three options. The first two tell Pine the size of the buffer; the third removes the guesswork.
Set max_bars_back for the whole script
Add max_bars_back to the indicator() or strategy() call, with a number at least as large as the deepest lookback you need. Tested version:
//@version=6
indicator("Look back far", max_bars_back = 3000)
x = close
y = bar_index > 2600 ? x[2500] : x
plot(y)
This is the simplest fix and the one the error message itself suggests. It applies to every series in the script.
Set max_bars_back for one series
If only one series needs the long history, use the max_bars_back() function on that series instead. It sets the buffer for that one variable and leaves the rest alone. Tested version:
//@version=6
indicator("Look back far, one series")
x = close
max_bars_back(x, 3000)
y = bar_index > 2600 ? x[2500] : x
plot(y)
In the tested version the call comes right after the series is declared and before the branch that reads from it.
Move the reference out of the branch
The third option is to read the past value on every bar, outside the if or ? :, store it in a variable, and use that variable inside the branch. Pine then sees the lookback on every bar and can size the buffer itself. This often makes the code easier to read as well, because the lookback is no longer buried in a condition.
Picking the number
Use the deepest offset your code can ask for. In the examples the lookback is 2,500 bars, so 3,000 leaves room. If the offset comes from ta.barssince(), think about how far back the event can realistically be on the charts you use, and pick a value that covers it.
What SuperCharting's Script Editor shows
SuperCharting's engine is more lenient here and runs this code. The broken example above compiles and runs on sample bars without the max_bars_back error. If the script must also work on TradingView, fix it the way shown above. Both tested fixes also run in SuperCharting, so one version of the script serves both.
To see this for yourself:
- Open a chart and open the Script Editor with Ctrl + E.
- Paste the script into the code area.
- Read the console, then press Add to chart to run it on the chart's bars.
Because SuperCharting does not stop on this pattern, check any [] reference that sits inside an if or ? : before you take the script to TradingView. The Script Editor page covers the console and the Save and Add to chart buttons.
Related errors
Two other errors come from Pine's limits on what a script may do on each bar. Loop is too long appears when a loop takes more than its time budget on one bar. Simple int expected, series int used is about lengths that change from bar to bar. To catch problems early, run your code through the Pine Script code checker.
Frequently asked questions
What number should I put in max_bars_back?
A number at least as large as the deepest [] offset your script uses. If the code reads x[2500], a value such as 3,000 is enough.
Should I use the indicator() argument or the max_bars_back() function?
Use the indicator() or strategy() argument when you are not sure which series needs the history. Use max_bars_back(series, N) when you know it is one series, so the rest of the script keeps its normal buffers.
Does this error appear in strategies too?
Yes. The message names both functions, and strategy() accepts max_bars_back the same way indicator() does.
Why does a ? : expression count as a branch?
Pine treats both if blocks and ? : expressions as conditional branches. A history reference on one side of either one runs only on the bars where that side is taken, which is what stops Pine from sizing the buffer.
TradingView and Pine Script are trademarks of TradingView, Inc. SuperCharting is independent and not affiliated with it.