Skip to main content
The basic types are int, float, bool, string and color. Arrays, drawings (label, line, …) and your own types and enums are covered in Arrays and Custom types and enums.

Literals

color.new(color.red, 0.5) sets an opacity from 0 (invisible) to 1 (solid); a value outside that range is clamped, with a warning.

na

na means “no value”: the first bars of a moving average, or history that does not reach back far enough. Arithmetic with an na operand gives na, and so does a division by zero.
  • Test with na(x) (not x == na, which warns); replace with nz(x) (0) or nz(x, 5).
  • A variable needs a type to start as na: float x = na. Without one: Value with NA type cannot be assigned to a variable that was defined without type keyword.

Declaring and typing a variable

name = value declares a variable, and its type is the type of the first value: x = 0 is an int. Write the type to choose another: float x = 0. A typed declaration takes a compatible value: float takes an int, bool takes a number, int takes neither a float nor a bool. := gives an existing variable a new value of its type; +=, -=, *=, /=, %= do the same with the result of the operation (+= also joins strings).
The compiler says: Variable x was declared with const int type. Declared type int is not compatible with assigned type series float. Use math.floor(), math.round(), or math.ceil() if you need a whole number. Declare float x = 0 instead.

Whole numbers

/ always gives a float, even for two integers, so int half = length / 2 is refused with the message above (Declared type int is not compatible with assigned type input float). Write int half = math.floor(length / 2); those functions return an int. /= is refused on an int variable for the same reason.

Converting

  • int(x) drops the fraction toward zero (int(-2.9) is -2) and gives a typed na for na.
  • bool(x) turns a number into false when it is zero and true otherwise; most bool arguments take a number the same way (alertcondition does not). float(x) turns an int into a float. string(5) and int("5") are refused.
  • Number to text: str.tostring(x). Text to number: str.tonumber(s).