Cause
A function that depends on past bars (ta.highest() here) is called inside an if block, so it only runs on some bars. Its internal history then covers only those bars, and its result isn't what you expect.
Broken code
This script compiles, but TradingView shows the warning on line 5.
//@version=6
indicator("Breakout level", overlay = true)
float level = na
if close > open
level := ta.highest(high, 20)
plot(level)Fixed code
The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.
//@version=6
indicator("Breakout level", overlay = true)
hh = ta.highest(high, 20)
float level = na
if close > open
level := hh
plot(level)Why it works
Called on every bar at the top level, ta.highest() keeps a complete history, so hh is always the highest high of the last 20 chart bars. The if block only decides when to use the result. The same applies to ta.ema(), ta.crossover(), ta.change(), ta.rsi() and to your own functions that use [] or var: calculate them globally, use them conditionally.
This is a warning, not an error. The script compiles and runs, but the values are usually not what you meant.
Ternaries and and/or
- In a ternary such as
cond ? f(close) : 0, the warning ends with "extract the call from the ternary operator or from the scope". Same fix. - In Pine Script v6,
andandorevaluate lazily: the right side is skipped when the left side already decides the result.close > open and ta.crossover(close, ta.sma(close, 20))triggers a related warning:The "ta.crossover()" call inside the conditional expression might not execute on every bar. Store the crossover in its own variable first, then combine.
Questions
Is "should be called on each calculation for consistency" an error?
No, it's a compiler warning and the script still runs. It's worth fixing because the function only sees the bars where it was called, which silently changes its result.
Does this warning apply to and / or in Pine Script v6?
Yes. In v6, and and or skip the right side when the left side already decides the result, so a ta.* call on the right side can miss bars. Calculate it in its own variable first.
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.