The condition of the “X” statement must evaluate to a “bool” value

This compilation error occurs if one or more of the conditions that control the flow of a conditional structure (an if or switch statement) returns a value that is not of the “bool” type. These structures cannot use values other than true and false as conditions.

The following sections explain the usual causes of this error and the typical solutions.

Using numeric conditions

In some programming languages, including earlier versions of Pine Script® up to v5, numeric values of the “int” and “float” types are implicitly converted to true or false when the script passes them to expressions that require “bool” values. The typical rules for such conversions are as follows:

  • A value of 0, 0.0, or na converts to false.
  • Any other nonzero, non-na value converts to true.

Although this logic might offer a marginal amount of convenience for experienced programmers in some rare cases, it can also easily lead to unintended results. Therefore, as of v6, Pine Script no longer implicitly casts numeric values to “bool” values in operations or function calls.

To work around this limitation, programmers can do either of the following:

  • Use the bool() function to explicitly cast “int” or “float” values to the “bool” type based on the above rules.
  • Write conditional expressions that compare numeric values and return “bool” results.

For example, the script below does not compile because it attempts to use the newMonth variable, which holds a “series int” value, as the condition in an if statement:

//@version=6 indicator("Invalid numeric condition demo") //@variable Holds the one-bar change in the value of the `month` variable ("series int"). // The value is nonzero only on bars where the month changed in the exchange time zone. newMonth = ta.change(month) // This code causes the error. The `if` statement requires "bool" conditions for its control criteria. // It cannot accept an "int" value or a value of any other type. if newMonth label.new(bar_index, high, "New month started")

To resolve the error, we can pass the newMonth variable to a bool() function call, and then use that call’s “series bool” result as the if statement’s condition. For example:

//@version=6 indicator("Explicitly casting a numeric condition demo") //@variable Holds the one-bar change in the value of the `month` variable ("series int"). // The value is nonzero only on bars where the month changed in the exchange time zone. newMonth = ta.change(month) // This code does not cause the error. The `bool()` call casts the `newMonth` value to the "bool" type. // The call returns `true` if the variable's value is a nonzero number, and `false` otherwise. if bool(newMonth) label.new(bar_index, high, "New month started")

Note that:

  • The bool() call casts the value retrieved from the variable to true or false, but it does not change the variable’s type to “bool”. The script can still use the newMonth variable in other code that accepts “series int” values.

Implicitly testing for ​na​ values

Values or references of most types can be na, which means undefined. Programmers sometimes tested for na in Pine v5 and earlier versions by using implicit “bool” casting behaviors in conditional logic. Such logic does not compile in Pine v6. Furthermore, such tests do not distinguish between numeric values that are na or 0.

Programmers can explicitly test for na instances of most available types and retrieve a “bool” result by using the na() function. A call to the function returns true if its argument is na, and false otherwise.

Consider the following script, which tries to use the result of a ta.pivothigh() function call to control an if statement. The call returns a “float” price value if it confirms a pivot high point, or na if no pivot high is confirmed. This script does not compile, because the if statement cannot use a “float” value or any na value as a condition:

//@version=6 indicator("Invalid test for `na` demo", overlay = true) //@variable Holds a non-na price value ("series float") if a pivot high is detected, and `na` otherwise. pivot = ta.pivothigh(10, 10) // This code causes the error. The `if` statement requires a "bool" condition. It can't use a "float" value. // It also cannot use `na` in any case because "bool" values are never `na`. if pivot label.new(bar_index[10], pivot, "Pivot High")

We can resolve the error and achieve our script’s intended result by replacing pivot in the if statement with the expression not na(pivot), which returns true if the variable’s value is not na, and false otherwise. For example:

//@version=6 indicator("Valid test for `na` demo", overlay = true) //@variable Holds a non-na price value ("series float") if a pivot high is detected, and `na` otherwise. pivot = ta.pivothigh(10, 10) // This code does not cause the error, because the expression used as the condition returns a "bool" result. if not na(pivot) label.new(bar_index[10], pivot, "Pivot High")