Arithmetic Operators (+, -, *, /, %)
- Take the remainder after dividing
Arithmetic operations above are binary. The type of result depends on the type of operands. If at least one of the operands is a series, then the result also will have a series type. If both operands are numeric, but at least one of these has the type float, then the result will also have the type float. If both operands are integers, then the result will also have the type integer.
Footnote: if at least one operand is NaN then the result is also NaN.
Comparison Operators (<, <=, !=, ==, >, >=)
- Less Than
- Less Than or Equal To
- Not Equal
- Greater Than
- Greater Than Or Equal To
Comparison operations are binary. The result is determined by the type of operands. If at least one of these operands has a series type, then the type of result will also be the series (a series of numeric values). If both operands have a numerical type, then the result will be of the logical type bool.
Logical Operators (not, and, or)
- Logical Conjunction
- Logical Disjunction
All logical operators can operate with bool operands, numerical operands, or series type operands. Similar to arithmetic and comparison operators, if at least one of these operands of an operator has a series type, than the result will also have a series type. In all other cases the operator’s type of result will be the logical type bool.
The operator not is unary. If an operator’s operand has a true value then the result will have a false value; if the operand has a false value then the result will have a true value.
and Operator truth table:
|a||b||a and b|
or Operator truth table:
|a||b||a or b|
Conditional Operator ? and the Function iff
Conditional Ternary Operator calculates the first expression (condition) and returns a value either of the second operand (if the condition is true) or of the third operand (if the condition is false). Syntax:
condition ? result1 : result2
If ‘condition’ will be calculated to true, then result1 will be the result of all ternary operator, otherwise, result2 will be the result.
The combination of a few conditional operators helps to build constructions similar to ‘switch’ statements in other languages. For example:
isintraday ? red : isdaily ? green : ismonthly ? blue : na
The given example will be calculated in the following order (brackets show the processing order of the given expression):
isintraday ? red : (isdaily ? green : (ismonthly ? blue : na))
First the condition ‘isintraday’ is calculated; if it is true then red will be the result. If it is false then ‘isdaily’ is calculated, if this is true, then green will be the result. If this is false, then ‘ismonthly’ is calculated. If it is true, then blue will be the result, otherwise it will be na. For those who find using the operator syntax ?: inconvenient, in Pine there is an alternative (with equivalent functionality) — the built-in function iff. The function has the following signature:
iff(condition, result1, result2)
The function acts identically to the operator ?:, i.e., if the condition is true then it returns result1, otherwise — result2. The previous example using iff will look like:
iff(isintraday, red, iff(isdaily, green, iff(ismonthly, blue, na)))
History Referencing Operator (Square Brackets )
It is possible to refer to the historical values of any variable of a series type (values which the variable had on the previous bars) with the  operator. For example, we will assume that we have the variable close, containing 10 values (that correspond to a chart with a certain hypothetical symbol with 10 bars):
Applying the operator  with arguments 1, 2, 3, we will receive the following vector:
When a vector is shifted, a special NaN value is pushed to vector's tail. NaN means that the numerical value based on the given index is absent. The values to the right, which do not have enough space to be placed in a vector of a line of 10 elements are simply removed. The value from the vector's head is ‘popped’. In the given example the index of the current bar is equal to 9.
- the value of the vector ‘close’ on the current bar will be equal to the previous value of the initial vector ‘close’
- the value ‘close’ will be equal to the value ‘close’ two bars ago, etc.
So the operator  can be thought of as the history referencing operator.
Footnote 1. Almost all built-in functions in Pine’s standard library return a series result, for example the function ‘sma’. Therefore it’s possible to apply the operator  directly to the function calls:
Footnote 2. Despite the fact that the operator  returns the result of the series type, it’s prohibited to apply this operator to the same operand over and over again. Here is an example of incorrect use:
close // Error: incorrect use of operator 
A compilation error message will appear.
In some situations, the user may want to shift the series to the left. Negative arguments for the operator  are prohibited. This can be accomplished using offset argument in plot annotation. It supports both positive and negative values. Note, though that it is a visual shift., i.e., it will be applied after all the calculations. Further details about plot and its arguments can be found here.
There is another important consideration when using operator  in Pine scripts. The indicator executes a calculation on each bar, beginning from the oldest existing bar until the most recent one (the last). As seen in the table, close has a value that is NaN on the first three bars. NaN represents a value which is not a number and using it in any math expression will result in also NaN. So your code should specifically handle NaN values using functions na and nz.
Priority of Operators
The order of the calculations is determined by the operators’ priority. Operators with greater priority are calculated first. Below are a list of operators sorted by decreasing priority:
|8|| + (unary)
- (unary) not
* / %
< >= <=
If in one expression there are several operators with the same priority, then they are calculated left to right.
If it’s necessary to change the order of calculations to calculate the expression, then parts of the expression should be grouped together with parentheses.