Edgoro
Compile error · Old code in v6 (v4 and v5 to v6)

Pine Script v6: bool cannot be na in a ternary (fix)

Pine Script v6 rejects na as a bool value, in ternaries and in declarations like var bool x = na. How to rewrite it, and how to model a third state.

Pine Editor console · line 4
Cannot call "operator ?:" with argument "expr2"="na". An argument of "simple na" type was used but a "simple bool" is expected.

Cause

The ternary returns true, false or na. In v6, a bool can only be true or false, so na isn't a valid bool value.

Broken code

TradingView reports the error on line 4.

Pine Script v6
//@version=6
indicator("Trend state")
ma = ta.sma(close, 50)
isUp = close > ma ? true : close < ma ? false : na
plot(isUp ? 1 : 0)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("Trend state")
ma = ta.sma(close, 50)
// 1 = above the average, -1 = below, 0 = on it
int trend = close > ma ? 1 : close < ma ? -1 : 0
plot(trend, "Trend", trend > 0 ? color.teal : trend < 0 ? color.red : color.gray, style = plot.style_columns)

Why it works

An int can hold as many states as you need, and "unknown" becomes an explicit value (0 here) instead of na. If two states are enough, drop the na branch and return false.

Declaring a bool with na

var bool active = na fails with Cannot assign a value of the "simple na" type to the "active" variable. The variable is declared with the "const bool" type. Start it at false: var bool active = false.

Also note that in v6 an if or switch that returns a bool gives false, not na, when no branch runs.

Questions

Why can't a bool be na in Pine Script v6?

The third state caused confusing behavior in v5: na acted as false in conditions but wasn't equal to false when compared. v6 removed it, so every bool is either true or false.

What should I use instead of a bool that can be na?

An int with one value per state, for example 1, -1 and 0, or a bool plus a second bool that says whether the first one has been set.

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.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.