Skip to content

Operators ​

How does a script combine values? With operators: + adds, > compares, and joins conditions, ?: picks a value, [1] reads the bar before, and := changes a variable. A na on either side of an arithmetic operator gives na, and makes a comparison false.

pine
//@version=6
indicator("Range, as a share of the close")
rng = high - low                    // arithmetic
pct = rng / close * 100
wide = pct > ta.sma(pct, 20) * 1.5  // comparison
plot(pct, "Range %", wide and close > close[1] ? color.green : color.gray) // logic, history and the ternary

Arithmetic ​

OperatorDoesExample
+Adds numbers, or joins strings."Close " + str.tostring(close)
-Subtracts, or negates one value.-change
*Multiplies.close * 2
/Divides, keeping the fraction: 5 / 2 is 2.5.int(5 / 2) is 2
%The remainder, with the left side's sign: -7 % 3 is -1.bar_index % 5 == 0

A number divided by 0 is an infinity, and 0 divided by 0 is na. A literal division by 0, such as x / 0, is refused in strict Pine, as TradingView refuses it. An infinity counts as na in na(), nz() and every ta.* window. See dividing.

Comparison ​

OperatorTrue when
==The two sides are equal.
!=They differ.
>, <The left side is greater, or less.
>=, <=The left side is greater or equal, or less or equal.

Two floats within 1e-10 of each other are equal, at any size, so 0.1 + 0.2 == 0.3 is true and a series of tiny values can read as flat. A na or an infinity on either side makes every comparison false, != included: test for na with na(x), since x == na is AmenoScript only. See when two numbers are equal.

Logic ​

OperatorTrue when
andBoth sides are true.
orEither side is true.
notIts one side is false.

and and or stop as soon as the left side decides: false and f() and true or f() never call f. A na where a bool is read counts as false.

The ternary ​

condition ? a : b is a where the condition is true, else b. Only the branch it picks runs, so a ta.* call in a branch sees only the bars that branch ran on: compute it before the ternary when you need it on every bar. See a call in a branch. Writing the ternary across lines with ? at the start of a line is AmenoScript only.

History ​

x[k] is x as it was k bars ago: close[1] is the last bar's close. It counts bars, not calls, and reads na before the first bar. A fraction in k is cut toward zero, so close[7.5] is close[7]. Keep k at 0 or more, and read back a name or an expression, not a literal: here a negative k reads na and 5[1] reads 5, but TradingView stops a script at a negative k and refuses 5[1] when it compiles. See reading back with x[k].

Inside a function, x[k] of a variable from outside it reads the values x had on the bars the function ran, not on every bar. See a function's copy of an outer name.

Assignment ​

OperatorDoes
=Declares a variable and gives it its first value.
:=Gives a declared variable a new value.
+=, -=, *=, /=, %=Changes a declared variable by the operation: n += 1 is n := n + 1.

Declaring a name twice in one block stops the script. In a nested block, such as an if's, = with a name from outside declares a new variable there, which leaves the outer one as it was: see a second variable by the same name. [a, b] = f() takes a function's tuple apart.

Coalescing (AmenoScript) ​

a ?? b is a, or b where a is na. Unlike nz, it keeps an infinity. See AmenoScript's extensions.

ameno
indicator("Last close, or the open")
prev = close[1] ?? open // the first bar has no last close
plot(prev)

Precedence ​

Operators higher in the table bind first. Operators in one row go from left to right.

BindsOperators
First[] history, . fields and methods, calls
Unary -, +, not
*, /, %
+, -
<, >, <=, >=, ==, !=
and
or
?? (AmenoScript)
Last?:

== and != bind as tightly as < and > here

TradingView binds == and != after the other comparisons, so a > b == c > d compares two conditions there. Here it reads ((a > b) == c) > d, a different answer. Put each comparison in parentheses: (a > b) == (c > d).

AmenoScript is compatible with Pine Script v6. Pine Script is a trademark of TradingView, Inc., which has no part in AmenoScript.