I see 'Pine cannot determine the referencing length of a series. Try using max_bars_back' error

When a Pine script is calculated, it creates a historical buffer of a certain size for each variable or function in the code. That buffer contains information about previous values of the variable/function and is used when the code refers to the past values using the [] history-referencing operator. The size of the buffer specifies how far into the history this value can be requested. 

Pine automatically determines the required buffer size for all variables and functions by analyzing past references made while calculating the script on the first 244 bars. If no past references are detected, a default buffer size is assigned to the variable or function. For variables, the default buffer size is 300 bars, for functions it is one bar. 

In some cases, Pine is not able to assign a proper buffer size for that function and the default buffer size is used instead. This can happen: 

  • In branches of conditional statements (if, iff, or ?) when past references to a variable or function inside the conditional statement are first executed when the 244th bar has already passed.
  • In functions that support dynamic length, when the length value passed to the function after the 244th bar is greater than any value that was passed to it before (when the buffer was being calculated).

Take a look at the code below. It will return the aforementioned error because:

  • The var1 variable value is not known at the time of compilation
  • The test variable does not request any past data on the first 244 bars of the chart

Due to this, the test variable gets assigned a default buffer of 300 bars. When the script requests the value of the 301st bar in the past, which is outside of the variable's buffer, an error occurs. 

//@version=4
study("max_bars_back var",overlay=true)
var1 = input(301)
test = 0.0
//max_bars_back(test, 301)
if bar_index > 244
    test := test[var1]
plot(test)

The max_bars_back parameter and the max_bars_back() function exist to work around this. They allow you to specify the correct buffer size for variables and functions when the default buffer doesn’t suffice. Uncomment the max_bars_back() function call in the code above. A buffer of 301 will be assigned to the test variable and as a result, the script will calculate properly.

Here is an example of a function call to which the default one-bar buffer size is attributed because it is not invoked in the first 244 bars. While no explicit reference to past values using the [] operator is used, the function nonetheless requires the past 20 values to calculate. The script will thus return the max_bars_back error:

//@version=4
study("Requires max_bars_back")
test = 0.0
if bar_index > 1000
    test := vwma(close, 20)
plot(test)

 The max_bars_back() function cannot be used to assign a specific buffer size to a function. When you need to do so, or when you want to set the default buffer size for all variables and functions in a script, add the max_bars_back parameter to the script’s study or strategy declaration statement. Note that using the parameter will increase the script's resource usage, so this method should only be used when needed:

//@version=4
study("Requires max_bars_back", max_bars_back=20)
test = 0.0
if bar_index > 1000
    test := vwma(close, 20)
plot(test)

You may also resolve the issue by taking the problematic expression out of the conditional branch, in which case the max_bars_back parameter is not required:

//@version=4
study("Requires max_bars_back")
test = 0.0
vwma20 = vwma(close, 20)
if bar_index > 1000
    test := vwma20
plot(test)

The error may also arise when using functions allowing dynamic lengths, i.e., series values. The following script will throw the max_bars_back error because during the first 300 bars, sma() is calculated with a length of 50, and thus only requested 50 bars of historical data, limiting its buffer to 50. After the 300th bar, however, the length changed to 100, which is out of bounds of its established buffer:

//@version=4
study("Out of bounds")
series_length = bar_index > 300 ? 100 : 50
plot(sma(close, series_length))

To work around this issue, you can generate a larger buffer in the beginning of the indicator’s calculations by using an intentionally large length value. In the following code, we request 1000 bars of historical data on the first bar of our indicator’s calculations. This creates a permanent 1000-bar buffer for our sma(), and as a result we never go out of bounds:

//@version=4
study("Not out of bounds")
series_length = bar_index > 300 ? 100 : 50
passed_length = bar_index == 0 ? 1000 : series_length 
plot(sma(close, passed_length))


You can learn more about max_bars_back and the related error in our User Manual.

Hem Aktie-screener Forex-screener Krypto-screener Ekonomisk kalender Om Diagramfunktioner Priser Tipsa en vän Ordningsregler Hjälpcenter Webbsidor och mäklarlösningar Widgets Diagramlösningar Lightweight Charting Library Blogg och nyheter Twitter