Compiler warningCW10003

Fix CW10003: function should be called on each calculation

Evaluate history-dependent functions on every calculation, then apply the condition to their result. Calling `ta.sma()` only when `close > open` builds an inconsistent internal history because skipped bars are missing from that call’s series. Moving the call to global scope gives it consecutive bar data. The conditional `plot()` then controls display without changing when the moving average is calculated.

This link opens the Pine Script AI Coding Agent overview. It does not run or change code on this page.

Before and after

Make the smallest reviewable change

Both artifacts include the version declaration and the code needed to inspect the stated problem. The diff lists the lines that change between them.

Broken code.pine
//@version=6
indicator("Conditional calculation demo")
float average = na
if close > open
    average := ta.sma(close, 20)
plot(average)
Fixed code.pine
//@version=6
indicator("Conditional calculation demo")
float average = ta.sma(close, 20)
plot(close > open ? average : na)
Diff.pine
--- broken.pine
+++ fixed.pine
@@
-float average = na
-if close > open
-    average := ta.sma(close, 20)
-plot(average)
+float average = ta.sma(close, 20)
+plot(close > open ? average : na)

Root causes

  • A history-dependent function runs only on bars that satisfy a condition.
  • A call inside a loop executes a different number of times across calculations.
  • A ternary or short-circuit Boolean expression conditionally skips a history-dependent call.

How the fix was verified

Method
Pineify UAT grammar checker via check-pine-script-grammar.mjs
Broken result
Returned the call-on-each-calculation warning at line 5, column 16.
Fixed result
Returned no grammar or consistency diagnostics.
Limit
Moving the call changes its calculation history by design. Review the resulting series on representative chart data.

Bring the actual script, not just the message

Open the Coding Agent overview to paste editable Pine Script, explain the intended behavior, and request a review. Compile and test the result in TradingView before using it.

Paste your code to fix this error