Skip to main content
An expression combines values with operators. Use parentheses whenever two operators of different kinds meet.

Precedence

not binds tighter than a comparison: not a > b is (not a) > b, so write not (a > b). Comparisons do not chain, because a < b is a bool and the next < wants a number:
The compiler says: Cannot call operator < with argument expr0=low<close. An argument of series bool type was used but a series int is expected. Write low < close and close < high.

Arithmetic

  • int with int gives an int, except /, which always gives a float (see Values and types). Any float operand gives a float. bool and string take no part in arithmetic.
  • % is the remainder. There is no power operator: x = close ^ 2 gives extraneous input ’^’ expecting {<EOF>, NEWLINE}. Write math.pow(x, 2).
  • An operand that is na, or a division by zero, gives na.

Comparison and logic

  • == and != work on numbers, strings and enum members; < > <= >= on numbers.
  • and, or, not are the same as &&, ||, !. Both sides of and and or are always evaluated (no short-circuit), and if either side is na the result is na, which an if treats as false.
  • An if or while condition is a bool or a number (zero is false); a string is refused.

The conditional expression

condition ? a : b is a when the condition is true and b otherwise (na counts as false). Only the chosen branch runs, which matters when it calls a talib function (see Calls that must run on every bar). It nests to the right: x > 0 ? 1 : x < 0 ? -1 : 0. The branches must have compatible types.

The history operator

x[n] is the value x had n bars ago; x[0] is the current value. It works on series values, your own variables, call results and parenthesised expressions, not on a literal or na. The index rules and the 500-bar reach are in Referencing previous bars.
"Bar " + bar_index is refused: + joins two strings, so write "Bar " + str.tostring(bar_index).