Sidechair

bool()

Browse the reference

Converts a number to true or false: 0 and na give false, everything else gives true.

Try it in the editor

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

Pine Script
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 bool

Arguments

NameTypeDefaultDescription
xsimple int/float/bool or input bool or simple bool or series int/float/boolrequiredThe 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

Pine ScriptHas volumeOpen in editor
//@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