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

Pine Script v6: if condition must evaluate to a bool (fix)

Pine Script v6 no longer treats numbers as true or false. Fix "The condition of the if statement must evaluate to a bool value" with a comparison.

Pine Editor console · line 3
The condition of the "if" statement must evaluate to a "bool" value.

Cause

The condition is a number (volume), not a bool. v5 converted numbers to bool automatically (0 and na were false, anything else true); v6 requires an explicit bool.

Broken code

TradingView reports the error on line 3.

Pine Script v6
//@version=6
indicator("Volume marker", overlay = true)
if volume
    label.new(bar_index, high, "Vol")

Fixed code

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

Pine Script v6
//@version=6
indicator("Volume marker", overlay = true)
if volume > 0
    label.new(bar_index, high, "Vol")

Why it works

A comparison returns a bool. Write the test you mean: volume > 0, not na(x), x != 0. To keep the exact v5 behavior, wrap the number in bool(): bool(volume) is false for 0 and na, and true otherwise.

The same rule in ternaries and loops

A number used as the condition of a ternary fails too: plot(volume ? 1 : 0) gives Cannot call "operator ?:" with argument "expr0"="volume". An argument of "series float" type was used but a "const bool" is expected. The fix is the same: plot(volume > 0 ? 1 : 0). while conditions follow the same rule.

Questions

Does Pine Script v6 convert int to bool?

No. v6 removed the implicit conversion from int and float to bool. Use a comparison such as x > 0 or x != 0, or convert explicitly with bool(x).

What does bool(x) return for na?

false, the same as for 0. Every other number gives true, which matches how v5 treated numbers in conditions.

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.