bool() - Pine Script Function
bool()
Section titled “bool()”Overview
Section titled “Overview”bool() casts a value to the boolean type. It returns false when the value is na, false, an empty string, or a numeric value equal to 0; any other value becomes true. Use it when you need to convert user input or numeric results to flags for control flow.
Syntax
Section titled “Syntax”bool(x) → const boolParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
x | simple int/float/bool/string | Value to convert to boolean. |
Returns
Section titled “Returns”const bool — result of the cast.
Remarks
Section titled “Remarks”- Strings convert to
falseif they are empty (""), otherwisetrue. - Casting arrays/objects is not supported; use boolean expressions instead.
- When working with user inputs (
input.bool,input.int, etc.), values are already the correct type—casting is only needed when you consume a generic source (e.g., CSV data).
Examples
Section titled “Examples”//@version=5indicator("bool() example", overlay=false)
floatVal = input.float(0.0, "Float value", step=0.5)strVal = input.string("", "String value")
isFloatTrue = bool(floatVal)isStringTrue = bool(strVal)
plot(int(isFloatTrue), "Float cast", color=color.blue)plot(int(isStringTrue), "String cast", color=color.orange)
label.new(bar_index, close, text = "bool(float)=" + str.tostring(isFloatTrue) + "\nbool(string)=" + str.tostring(isStringTrue), style=label.style_label_left, textcolor=color.white, bgcolor=color.new(color.blue, 70))