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.
//@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.
//@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.