Arithmetic, comparison, assignment, ternary, history and function-arrow operators of Pine, with precedence, na handling and v6 division rules.
Arithmetic uses + - * / %. The result is na when either number is na, and in v6 dividing two ints gives a float: 5 / 2 is 2.5. % returns the remainder, and takes the sign of the left operand, so -7 % 3 is -1. + also joins strings, and a number beside a string needs str.tostring first. Comparisons < <= > >= == != give a bool and are never na in v6; a comparison against na is false, so test for na with na(x).
= declares a variable, := changes one that already exists, and += -= *= /= %= update in place: x += 1 is x := x + 1. ?: picks between two values from a bool and evaluates only the chosen side. x[n] reads the value n bars back, and is na when n reaches before the first bar. => ends the header of a function and separates a switch case from its block.
Precedence from tightest to loosest: [], unary + - and not, * / %, + -, < <= > >=, == !=, and, or, ?:, then the assignments. Use brackets rather than rely on it for anything subtle.
Operators
| Operator | Syntax | Description |
|---|---|---|
+ | expr1 + expr2 || + expr | Adds two numbers, or joins two strings. Unary + returns the operand unchanged. |
- | expr1 - expr2 || - expr | Subtracts the second number from the first. Unary - negates its operand. |
* | expr1 * expr2 | Multiplies two numbers; na if either is na. |
/ | expr1 / expr2 | Divides the first number by the second. In v6, two ints give a float (5 / 2 is 2.5). Guard a divisor that can be 0. |
% | expr1 % expr2 | Remainder of the division. The sign follows the left operand, so -7 % 3 is -1. |
< | expr1 < expr2 | True when the first value is smaller than the second. |
<= | expr1 <= expr2 | True when the first value is smaller than or equal to the second. |
> | expr1 > expr2 | True when the first value is larger than the second. |
>= | expr1 >= expr2 | True when the first value is larger than or equal to the second. |
== | expr1 == expr2 | True when the two values are equal. Comparing with na gives false; use na(x). |
!= | expr1 != expr2 | True when the two values differ. |
= | <var_name> = <initial_value> | Declares a new variable and gives it a value. A declared name cannot be declared again in the same scope. |
:= | <var_name> := <new_value> | Gives a variable that already exists a new value. Works on variables from an outer scope, and on var and varip variables. |
+= | expr1 += expr2 | Adds to a variable and stores the result: x += 1 is x := x + 1. |
-= | expr1 -= expr2 | Subtracts from a variable and stores the result. |
*= | expr1 *= expr2 | Multiplies a variable and stores the result. |
/= | expr1 /= expr2 | Divides a variable and stores the result. |
%= | expr1 %= expr2 | Stores the remainder of dividing the variable by the right side. |
?: | expr1 ? expr2 : expr3 | Ternary: gives expr2 when expr1 is true and expr3 otherwise. Only the chosen side is evaluated. Chain with brackets or another ?: for more cases. |
[] | expr1[expr2] | History reference: the value of expr1 as it was expr2 bars ago. close[1] is the previous close. Na before the first bar. |
=> | <identifier>([<parameter_name>[=<default_value>]], ...) =>
<local_block>
<function_result> | Ends a function or method header and starts its body. Inside switch, it separates a case from its block, and a bare => marks the default. |
Remarks
- A ternary or and/or may skip a ta.* call on the side that is not chosen, which breaks the call's history. Compute it on its own line and use the result in the expression.
- x[n] takes an offset that can itself be a series, but cannot be negative; Pine cannot look at future bars.
- On Sidechair: % and the arithmetic operators follow the rules above; x / 0 gives Infinity or -Infinity and 0 / 0 gives na; != is false whenever either side is na, and an na string beside a string reads as an empty string.
Example
//@version=6
indicator("Operators", overlay = true)
len = 20
mid = (high + low) / 2
move = close - close[1]
stepped = move % 2
var float total = 0.0
total += move
band = close > ta.sma(close, len) ? color.green : color.red
plot(mid, "Mid", band)
plot(total, "Cumulative move", color.new(color.blue, 50))
plotshape(close >= open and close != close[1], "Up and changed", shape.circle, location.bottom, color.gray)
label.new(bar_index, na, "x" + str.tostring(stepped), yloc = yloc.belowbar, size = size.tiny)See also
Runs on Sidechair