Quick Answer
In Pine Script v6, an if or switch condition, and the first operand of a ternary, must be a real bool. The compiler error is "The condition of the 'X' statement must evaluate to a 'bool' value" (code CE10101 in the TradingView documentation). Two things cause it, and both come from old scripts: an int or float used as a condition, because v6 no longer turns numbers into booleans on its own, and a value tested for na by writing it alone as the condition.
The fixes are small. For a number, compare it (x != 0) or wrap it in bool(x). For an na test, use na(x) or not na(x). This post shows both causes with scripts, two more v6 changes that come with them (a bool can no longer be na, and and / or are evaluated lazily), and a checklist for moving a v5 script to v6 without silent changes in the signals. It also notes what to keep in mind if you convert the script to MQL5 or cTrader C# afterwards.
From the author: I convert Pine Script to MQL5 and cTrader C#. Old v5 scripts that use
if myNumberorif pivotare common, and v6 stops them at compile time, which is good: in v5 they worked by a rule that most authors did not know. When you convert such a script, you need to know what the rule was.
Key Facts
| What you wrote (v5 style) | v6 result | Fix |
|---|---|---|
| if ta.change(close) (a number as condition) | Error CE10101 | if ta.change(close) != 0 or if bool(ta.change(close)) |
| if pivot (a value that can be na) | Error CE10101 | if not na(pivot) |
| x ? a : b with x a number | Error | bool(x) ? a : b or a comparison |
| na(isLong) with a bool | Error: a bool cannot be na in v6 | Remove the na() call, or use an int with three states |
| cond and ta.rsi(close, 14) > 50 | No error, but the right side may not run | Call ta.rsi first, then use the variable |
The message and both causes come from the TradingView documentation page for CE10101 and from its migration guide to Pine v6.
Why It Changed
In Pine v5, a number was turned into a bool when it was used where a bool is needed: na, 0 and 0.0 were false, and every other number was true. The migration guide says that in v6 int and float values are no longer implicitly cast to bool, so a script must cast a number on purpose to use it as a condition.
Cause 1: A Number as a Condition
//@version=6
indicator("Broken numeric condition")
changed = ta.change(close)
if changed // error: the condition must be a bool
label.new(bar_index, high, "Price changed")
ta.change(close) is a number. Two fixes, depending on what you mean:
//@version=6
indicator("Fixed numeric condition")
changed = ta.change(close)
if changed != 0 // say what you mean
label.new(bar_index, high, "Price changed")
bool(changed) also compiles and gives the old rule (zero is false, a non-zero number is true), but the comparison is easier to read, and it makes you decide what to do on the bars where the value is na. The documentation names bool() as the cast.
Cause 2: Testing for na by Writing the Value Alone
In v5 and before, if pivot was a way to ask "is there a pivot?", because na counted as false. In v6 this is an error:
//@version=6
indicator("Broken na test", overlay = true)
pivot = ta.pivothigh(high, 5, 5)
if pivot // error: a float is not a bool
label.new(bar_index[5], pivot, "Pivot")
Test for na with the function made for it:
//@version=6
indicator("Fixed na test", overlay = true)
pivot = ta.pivothigh(high, 5, 5)
if not na(pivot)
label.new(bar_index[5], pivot, "Pivot")
This version also fixes a hidden problem of the old one: a pivot at a price of 0 would have counted as "no pivot" in v5. (ta.pivothigh returns the price of the pivot, or na when there is none.)
The Ternary Operator Has the Same Rule
The first operand of ? : is a condition, so the rule applies to it. The migration guide gives this v5 line and its v6 version:
// v5
color expr = bar_index ? color.green : color.red
// v6
color expr = bool(bar_index) ? color.green : color.red
In a real script you will usually want a comparison, such as bar_index > 100, which is already a bool.
Two More v6 Changes You Meet at the Same Time
A bool cannot be na in v6. According to the migration guide, na(), nz() and fixnan() no longer accept bool arguments, and an unspecified condition returns false instead of na. If your logic needs three states (long, short, flat), use an int:
int tradeDirection = if strategy.position_size < 0
-1
else if strategy.position_size > 0
1
else
0
and and or are lazy. In v6 an expression stops as soon as the result is known. That is not an error, but it can silently change values when the skipped part is a function that keeps history, such as ta.rsi(). The guide recommends extracting such calls to the global scope:
//@version=6
indicator("Lazy and, fixed")
float rsiVal = ta.rsi(close, 14) // runs on every bar
bool signal = false
if close > open and rsiVal > 50
signal := true
plot(signal ? 1 : 0)
This is the same rule as in the "should be called on each calculation" warning.
What to Check When You Convert the Script
MQL5 and C# are strict about types in their own way, and a converted if needs an explicit meaning for every number:
| Pine v6 | MQL5 | cTrader C# |
|---|---|---|
| if x != 0 | if(x != 0) | if (x != 0) |
| if not na(pivot) | if(pivot != EMPTY_VALUE) or your own empty marker | if (!double.IsNaN(pivot)) |
| bool(x) | x != 0 | x != 0 |
| int with -1, 0, 1 | An int or an enum | An int or an enum |
In MQL5 and C# a number in an if is not a bool either (C# gives an error; MQL5 accepts it), so write the comparison in both. The empty value of an indicator buffer depends on how you set it up, so check it on a chart. For the version differences in general, see Pine Script v6 to MQL5.
Similar Pine Script Messages
| Message | What it means | Where to look |
|---|---|---|
| Condition must evaluate to a bool | A number or na is used as a condition | This post |
| Function should be called on each calculation | A history function is called conditionally | CW10003 warning |
| Undeclared identifier | A name Pine does not know | Pine undeclared identifier |
| End of line without line continuation | A line is broken in the wrong place | Pine end of line error |
| series int vs simple int | A function needs a constant length | series int, simple int |
Checklist: Moving a v5 Script to v6
- Change the version line to
//@version=6. - Search for
iffollowed by a plain number, such asif changedorif pivot. - Write the comparison (
!= 0,> 0) orbool(x)for numbers. - Use
na(x)/not na(x)to test forna. - Search for
na(,nz(andfixnan(applied to abool. - Look at every
and/orthat contains ata.*call and extract the call. - Compare the plots of the old and the new version on the same chart.
- Convert to MQL5 or C# only after the v6 version compiles and matches.
Common Mistakes
- Using
bool()everywhere without deciding what zero andnashould mean. - Writing
if xfor annatest (an error in v6). - Calling
na()on abool. - Leaving a
ta.*call inside anand/orcondition. - Changing the version line and not reading the compiler output.
- Converting the script before the signals are compared.
FAQ
Related Guides
- Pine Script function should be called on each calculation: the lazy
and/orproblem in detail. - Pine Script v6 to MQL5: what changes between versions when you convert.
- Pine Script undeclared identifier: a name that Pine does not know.
- Pine Script end of line without line continuation: a line broken in the wrong place.
- Pine Script series int, simple int: a length that must be a constant.
- 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 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.