Converts a number to true or false: 0 and na give false, everything else gives true.
bool(x) is the explicit way to treat a number as a condition. Version 6 does not turn numbers into booleans on its own, so if volume fails to compile and if bool(volume) works.
Its other use is bool(na), which declares a boolean variable that starts empty.
Syntax
bool(x: simple int/float/bool) -> const bool
bool(x: input bool) -> input bool
bool(x: simple bool) -> simple bool
bool(x: series int/float/bool) -> series boolArguments
| Name | Type | Default | Description |
|---|---|---|---|
| x | simple int/ | required | The value to convert. A number or a boolean. |
Returns
const/input/simple/series bool: false when x is 0 or na, true for any other number, and a boolean unchanged.
Remarks
- bool(na) is false.
- On Sidechair: bool() uses the same rule, so 0, na and false give false and any other number gives true.
Example
//@version=6
indicator("bool cast")
hasVolume = bool(volume)
bgcolor(hasVolume ? na : color.new(color.red, 80))
plot(hasVolume ? 1 : 0, "Has volume")See also
Runs on Sidechair