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:

  1. Arithmetic operators

  2. Comparison operators

  3. Logical operators

  4. ?: ternary operator

  5. [ ] history-referencing operator

  6. = assignment operator

  7. := 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 Negation

  • and for Logical Conjunction

  • or for Logical Disjunction

The not operator is unary, meaning it only needs one operand.

?: Ternary Operator

The ?: 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

The [ ] 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

The = 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

The := 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