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:
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
intwithintgives anint, except/, which always gives afloat(see Values and types). Anyfloatoperand gives afloat.boolandstringtake no part in arithmetic.%is the remainder. There is no power operator:x = close ^ 2gives extraneous input ’^’ expecting {<EOF>, NEWLINE}. Writemath.pow(x, 2).- An operand that is
na, or a division by zero, givesna.
Comparison and logic
==and!=work on numbers, strings and enum members;<><=>=on numbers.and,or,notare the same as&&,||,!. Both sides ofandandorare always evaluated (no short-circuit), and if either side isnathe result isna, which aniftreats as false.- An
iforwhilecondition is aboolor 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).