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.
An issue commonly arises 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 such cases, Pine will not be able to assign a proper buffer size for that function and the default buffer size will be used. Take a look at the code below. It will return the aforementioned error because:
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)
You can learn more about max_bars_back and the related error in our User Manual.