Quick Answer
In the Pine Editor you may see this message: "The function X should be called on each calculation for consistency. It is recommended to extract the call from this scope." It is a compiler warning, not an error: the script still runs. The TradingView documentation explains it. A function that uses past bar values, either through the [] operator or internally, was called inside a conditional structure or a loop, so it runs on some bars and not on others. Its history is then inconsistent, and the values can be different from what you expect.
The fix is the one the documentation gives: call the function in the global scope, where it runs on every bar, store the result in a variable, and use that variable inside the if. Functions that do not use past values, such as math.max(), do not cause the warning. This post shows the broken pattern, the fix, the places where the warning also appears (ternary expressions, and and or), and what to keep in mind when you convert the script to MQL5 or cTrader C#.
From the author: I convert Pine Script to MQL5 and cTrader C#. In MQL5 and C#, an indicator is usually calculated for every bar, so this problem does not exist there. In Pine it is easy to hide a
ta.emainside a condition and get a line that looks plausible and is wrong. A conversion then copies the wrong numbers, and the EA trades on them.
Key Facts
| What you wrote | Why it warns | Fix |
|---|---|---|
| if cond then x = ta.sma(close, 20) | ta.sma uses past values and runs only when cond is true | sma = ta.sma(close, 20) at the top, then use sma in the if |
| cond ? ta.ema(close, 14) : na | A ternary also runs the call conditionally | Call ta.ema first, then pick the value |
| a and ta.rsi(close, 14) > 50 | The right side of and may not run on every bar | Call ta.rsi first and compare the variable |
| A call inside a for loop | It runs more than once per bar, or on some bars only | Move it out of the loop |
| math.max(a, b) inside an if | No past values are used, no warning | Nothing to change |
The message, the explanation of the cause and the fix come from the TradingView Pine Script documentation page for this warning (code CW10003).
What the Warning Means
Many Pine functions keep an internal history. ta.sma(close, 20) needs the last 20 closes, ta.ema needs its own previous value, and ta.rsi needs both. For the history to be right, the function has to be called on every bar. If it is called only when a condition is true, it sees a gap: the bars where the condition was false never reached it. The result on the bars where it did run is then not the same as the value you would get from the full series.
Pine cannot always tell you that the numbers are wrong. It can only warn that the call is in a place where it may not run on every bar. That is the message in this post.
The Broken Script
//@version=6
indicator("Inconsistent call", overlay = true)
float signal = na
if close > open
signal := ta.sma(close, 20) // warning: called only on bars where close > open
plot(signal, "SMA on up bars", style = plot.style_linebr)
The moving average is calculated only on up bars. Its 20 values are not the last 20 bars of the chart but the last 20 bars on which the call ran, which is a different series.
The Fix: Call in the Global Scope
This is the pattern from the TradingView documentation: run the call on every bar, then reassign its result inside the conditional block.
//@version=6
indicator("Consistent call", overlay = true)
float smaValue = ta.sma(close, 20) // runs on every bar
float signal = na
if close > open
signal := smaValue // only the use is conditional
plot(signal, "SMA on up bars", style = plot.style_linebr)
The documentation notes that reassigning the result inside the condition does not change how the call runs. The moving average is now the normal 20 bar average, and the line is shown only on up bars.
The Same Warning in a Ternary or and / or
The documentation says that the warning can also appear for a ternary expression or an and / or expression that conditionally runs a history-dependent call. Two ways to write the same mistake:
//@version=6
indicator("Ternary and logic", overlay = true)
a = close > open ? ta.ema(close, 14) : na // call is inside the ternary
b = close > open and ta.rsi(close, 14) > 50 // right side may not run
The fix has the same shape: first calculate, then decide.
//@version=6
indicator("Ternary and logic, fixed", overlay = true)
emaValue = ta.ema(close, 14)
rsiValue = ta.rsi(close, 14)
a = close > open ? emaValue : na
b = close > open and rsiValue > 50
Functions You Wrote Yourself
A function that you define with f(x) => and that uses ta.* or the [] operator inside has the same property. If you call it inside an if, the warning names your function. Call it at the top level, or move the conditional logic inside the function so that the history-using parts always run.
Why This Matters When You Convert
A converted indicator copies the numbers that the script produces. If the Pine version has a moving average that skips bars, the MQL5 or C# version (which calculates every bar by default) gives different values, and you may spend time looking for a conversion bug that is really a Pine inconsistency. Fix the warning in Pine first and compare the two versions on the same bars. Related conversion topics: Pine Script ta.rsi and ta.ema vs MQL5 indicators, Pine Script ta.barssince and ta.valuewhen, which are also history-dependent. When results still differ, see why a Pine strategy gives different results in MQL5.
How to Find the Calls That Cause the Warning
- Read the warning: it names the function.
- Look at the indentation of that call: is it inside an
if, aforor a function? - Look for
? :,andandoron the same line. - Move the call above the condition and give it a variable name.
- Use the variable in the condition.
- Compare the plot before and after on the same chart. If the line changed, the old one was inconsistent.
Similar Pine Script Messages
| Message | What it means | Where to look |
|---|---|---|
| Function should be called on each calculation | A history-dependent call is conditional | This post |
| Cannot use plot in local scope | plot() is inside a block | Pine plot in local scope |
| Undeclared identifier | A name Pine does not know | Pine undeclared identifier |
| series int vs simple int | A function needs a constant length | series int, simple int |
Checklist: What to Check When You See This Warning
- Find the function in the warning.
- Check where it is called:
if,for, ternary,and/or, or a user function. - Move the call to the global scope and store the result.
- Use the variable inside the condition.
- Compare the plot before and after the change.
- Check other
ta.*calls in the script for the same pattern. - Convert only after the warning is gone, and compare the values of both versions on the same bars.
Common Mistakes
- Putting
ta.emaorta.rsiinside anifto "save work". - Putting the call on the right side of
and. - Ignoring the warning because the script still runs.
- Calling a history-using function from a loop.
- Moving only the plot and leaving the call inside the block.
- Assuming that a converted EA has the same numbers without a comparison.
FAQ
Related Guides
- Pine Script cannot use plot in local scope: the other common "local scope" problem.
- Pine Script ta.rsi and ta.ema vs MQL5 indicators: history-dependent functions in conversion.
- Pine Script ta.barssince to MQL5: another function that needs every bar.
- Pine Script ta.valuewhen to MQL5: the same family of functions.
- Pine Script var and varip to MQL5: keep state between bars.
- Why a Pine strategy gives different results in MQL5: where differences come from.
- Pine Script undeclared identifier: a name that Pine does not know.
- Pine Script to MQL5 guide: converting a script step by step.
Convert Without Guessing
If you want a first draft of your Pine Script in MQL5 or cTrader C#, try the CodeFlowOS converter. It produces a draft that you still have to review, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong logic, so always compare the result with the chart. If the converted code does not compile, paste it with the message into the Error Fixer: it suggests a minimal fix and shows what changed (1 credit, with up to 3 attempts included).
This article is educational. It does not describe trading performance and is not financial advice. Test every script on a chart or a demo account before you rely on it.