FAQ

The function ‘XXXX’ should be called on each calculation for consistency. It is recommended to extract the call from this scope

The function ‘XXXX’ should be called on each calculation for consistency. It is recommended to extract the call from this scope

Ever come across the following warning message in Pine Script? It happens quite often for some developers.

The function ‘XXX’ should be called on each calculation for consistency. It is recommended to extract the call from this scope.

This is not an error per se but rather a warning, which means your script will still be executed and added to the chart. However, it’s a sign that the computation in your script might be wrong. So it is important that you are careful with it.

Why is This Warning Produced?

Here is an example of the code that produces this warning:

//@version=5
indicator("My script", overlay = true)

val = close > open ? ta.highest(high, 10) : ta.lowest(low, 10)
    
plot(val)

The idea behind this code is pretty simple – in the green bar I want to display highest high for the last 10 bars, while in the red bars I want to plot the lowest low for the last 10 bars.

However, by carefully looking at the output of this script, you quickly realize that it works all wrong and outputs values that are completely wrong.

Why is this? Because of the way Pine Script works with these functions. It runs the majority of “technical analysis” functions in a “series way”. For this reason, calling them in a conditional operator might cause some pretty weird issues.

How to Resolve This Warning

To resolve this warning, you need to remove computation from the conditional operator to the global scope and then use computed variables in your condition.

Here is a fixed version of code snippet above:

//@version=5
indicator("My script", overlay = true)

ll = ta.lowest(low, 10)
hh = ta.highest(high, 10)
val = close > open ? hh : ll
    
plot(val)

Thanks to the correction, the code doesn’t show any warning and the values printed make much more sense:


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png
Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news