Operators
Introduction
Pine Script™ employs several operators to create expressions that return a result or assign values to variables. These operators can be categorized as follows:
Arithmetic operators
Comparison operators
Logical operators
?:
ternary operator[ ]
history-referencing operator=
assignment operator:=
reassignment operator
Note: In Pine Script™, the forms and types of these operators have a critical impact on determining the type of results that expressions yield, influencing the functions they can be used with. For instance, if you multiply an “input int” with a “series int”, the expression will produce a “series int” result. See more in the "Type system" page.
Arithmetic Operators
Pine Script™ supports five arithmetic operators:
+
for Addition and string concatenation-
for Subtraction*
for Multiplication/
for Division%
for Modulo (remainder after division)
The +
and -
operators can also serve as unary operators, and the +
operator is used for string concatenation.
Comparison Operators
Pine Script™ supports six comparison operators:
<
for Less Than<=
for Less Than or Equal To!=
for Not Equal==
for Equal>
for Greater Than>=
for Greater Than or Equal To
The result of comparison operations will be of type bool when both operands have a numerical value.
Logical Operators
Pine Script™ supports three logical operators:
not
for Negationand
for Logical Conjunctionor
for Logical Disjunction
The not
operator is unary, meaning it only needs one operand.
?:
Ternary Operator
?:
Ternary OperatorThe ?:
ternary operator is used to create conditional expressions. The operator returns a result that depends on the value of the condition, returning one value if the condition is true and another value if the condition is false or na.
[ ]
History-Referencing Operator
[ ]
History-Referencing OperatorThe [ ]
history-referencing operator allows for the referencing of past values of time series. It can be used after a variable, expression, or function call. The offset inside the square brackets refers to the historical value to be referenced. For example, to refer to the volume value two bars back from the current bar, one would use volume[2]
.
=
Assignment Operator
=
Assignment OperatorThe =
operator is used to assign a variable when it is declared. This denotes that this is a new variable to be used, initialized with a certain value.
:=
Reassignment Operator
:=
Reassignment OperatorThe :=
operator is used to reassign a value to an existing variable. Variables that have been declared, then reassigned using :=
, are referred to as mutable variables.
Operator Precedence
The order of calculations in Pine Script™ is determined by operator precedence. Operators with higher precedence are calculated first. If the precedence is equal, the operations are calculated from left to right. To alter this order, parts of the expression can be grouped together using parentheses.
Last updated