Casting

The tools library provides custom casting methods that expand Pinescript's casting capabilities for boolean, float, integer, string, and color object types.

Boolean

The _bool() method extends the functionality of the Pinescript's bool() function, allowing the conversion of string, integer, and float values to boolean values.

// Values that returns false

""._bool()          // empty string.
0._bool()           // literal 0 value.
0.0._bool()         // float 0.
"0"._bool()         // string "0".
"no"._bool()        // string "no"  (letter casing doesn't matter).
"na"._bool()        // string "na"  (letter casing doesn't matter).
"NaN"._bool()       // string "nan" (letter casing doesn't matter).

// Values that returns true

"non-empty"._bool() // any non-empty string (other than 0, no, na, nan).
100._bool()         // any float or integer value other than 0.

Float

The _float() method provides a convenient way to convert into floating point number.

"20.408"._float()             // returns float 20.408.
"20.408"._float(precision=2)  // returns float 20.40.

Integer

The _int() method provides a convenient way to convert into integer values.

"201"._int() // returns integer 201.

String

The _str() method provides a convenient way to convert a value to it's string representation. The method works with string, integer, boolean, float and color object types. If a color value is passed, the method converts it to the color's RGBT values and returns those values as a string separated by pipes ("|"). There is also a convenient method _colorToString() which basically does the same thing.

true._str()                             // returns the string "true".
20.408._str()                           // returns the string "20.408".
201._str()                              // returns the string "201".
#FFFFFF._str()                          // returns the string "255|255|255|0".
color.new(#FFFFFF, 0)._str()            // returns the string "255|255|255|0".
#FFFFFF._colorToString()                // (alternative) returns the string "255|255|255|0".
color.new(#FFFFFF, 0)._colorToString()  // (alternative) returns the string "255|255|255|0".

Color

The _clr() method provides a convenient way to convert a string of RGBT values into a color ("R|G|B|T"). This method only works with properly converted RGBT strings. There is also a convenient method _colorFromString() which basically does the same thing.

"255|255|255|0"._clr()              // returns the #FFFFFF color.
"255|255|255|0"._colorFromString()  // (alternative) returns the #FFFFFF color.