TradingView has several operators that we can use when programming scripts, including arithmetic operators, comparison operators, and the conditional ternary operator. But if one line of code has several operators, in what order are they calculated?

In this article:

TradingView operators and the operators’ priority

TradingView Pine scripts can perform complex operations thanks to operators. An operator is a code element that performs a certain action on one or several values (Stephens, 2014). Those values that an operator ‘operates on’ are what we call operands (Sharp, 2013). And a piece of code that returns a value is called an expression (Albahari & Albahari, 2012), and these often contain operators. Expressions include close + high (returns the sum of the bar’s close and high) and open > open[1] (returns true when the open is above the previous open).

Operators evaluate in a certain order when an expression has several of them. Pine determines that order of calculations with the operators’ priority (Pine Script Language Tutorial, n.d.). Those rules regulate which operator is evaluated first, which one calculates next, and so on until all operations in an expression have been performed.

Due to the operator’s priority, Pine evaluates expressions in a predictable way. It also clarifies unclear expressions like 2 + 10 * 5 (does the programmer want to add before multiplying or multiply before adding?). Since multiplication has a higher priority than addition, this calculates to 52 (5 x 10, then plus 2) and not 60 (2 + 10, times 5).

The priority of calculations in TradingView Pine

The priority of operators in TradingView is displayed below. Operators with a higher priority are calculated before those with a lower priority (Pine Script Language Tutorial, n.d.):

Priority Operator Name Example
10 ( ) parentheses; overrides operators’ priority ((34 - 3) + (8 - close[2])) / 5
9 [ ] history referencing operator close[2], myVariable[9]
8 + addition operator (unary); leaves operand unchanged +mom(close, 10), +volumeChange
- subtraction operator (unary); returns operand’s opposite -ema(high, 3), -maxLoss
not logical not operator; returns logical opposite not (high > high[1]), not enterLong
7 * multiplication operator hl2 * 2, 10 * volumeDifference
/ division operator low / high, 9 / 2
% modulus operator; returns remainder of integer division 9 % 3, n % 20 == 0
6 + addition operator (binary) 10 + 6, (close + close[1]) / 2
- subtraction operator (binary) high - low, ema(close, 10) - ema(close, 3)
5 > greater than operator 10 > 9, high > high[1]
< less than operator 9 < 1, mom(close, 10) < mom(close, 10)[1]
>= greater than or equal to operator close <= sma(close, 10), open <= close
<= less than or equal to operator high <= high[5], 19 <= 20
4 == equality operator high = highest(high, 20), low = low[2]
!= unequal to operator close != close[4], myVariable != 100
3 and logical and operator newHigh and volumeIncrease, 10 > 2 and 9 != 8
2 or logical or operator enterLong or stopTriggered, not (9 < 3) or 500 > 8
1 ?: conditional ternary operator highestHigh == true ? 200 : 3, close < open ? close : close

As the table shows, we can use the addition (+) and subtraction (-) operators in unary and binary form. When an operator acts on a single operand, it’s called unary (like -close to get the negative of the closing price); when the operator acts on two values, it’s named binary (like high - low) (Albahari & Albahari, 2012).

What’s also clear from the table is that several operators have the same priority. When an expression has several operators with the same priority, then they are calculated from left to right (Pine Script Language Tutorial, n.d.). For example, addition (+) and subtraction (-) have the same priority and so the expression 9 - 3 + 22 - 3 returns 25 (9 - 3 is 6, adding 22 gives 28, and subtracting 3 gives 25).

Tip: The operator priority rules don’t need to be memorised; when in doubt, just use parentheses to clarify the expression (also see below).

Changing the priority of operators with parentheses in TradingView

We can change the operators’ priority by grouping parts of the expression with parentheses (Pine Script Language Tutorial, n.d.). Because parentheses have the highest priority, TradingView evaluates expressions inside parentheses first.

For example, 10 + 9 * close first multiplies 9 with close and then adds 10 to the result because multiplication has a higher priority than addition. But (10 + 9) * close first adds 10 and 9 together and then multiplies that result with close.

We occasionally need to use parentheses to get the correct calculation, like in this script where we try to calculate the bar’s midpoint:

study(title="Bar midpoint", overlay=true)

plot(series=high + low / 2, color=blue, linewidth=2)

Here we use study() to set the indicator’s properties, and then plot a series of data on the chart with plot() (TradingView, n.d.). The plotted value is high + low / 2 in an attempt to plot the bar’s midpoint. But when added to a chart, this example looks like:

Example of TradingView programming error

Something is clearly off here. The issue is that we ignored the operators’ priority with the high + low / 2 expression. Since division (/) has a higher priority than addition (+), we don’t get the bar’s midpoint. Instead, we divided the bar’s low with 2 and then added the high.

To fix this error, we need to change the operators’ priority with parentheses. And so we put high + low inside parentheses like this:

study(title="Bar midpoint", overlay=true)

plot(series=(high + low) / 2, color=blue, linewidth=2)

When we add this updated example to a chart, it correctly plots the bar’s midpoint:

Using parentheses in a TradingView script's source code

Nesting parentheses in TradingView Pine

Sometimes we have complex expressions that require multiple parentheses nested inside each other. In that case, TradingView Pine evaluates the innermost parentheses first (in other words, it works from the inside out).

Let’s say we calculate the x variable as follows:

x = (7 % 3) * (4 + (6 / 2))

Since the innermost parentheses are evaluated first, Pine first calculates 6 / 2. The next step then becomes:

x = (7 % 3) * (4 + 3)       // 6 / 2 = 3

Each set of parentheses is then evaluated in turn like this:

x = 1 * 7           // (7 mod 3) = 1, (4 + 3) = 7

And so the variable ends with a value of 7:

x = 7

See arithmetic operators, the modulus operator, and the assignment operator to learn more about the operators used in this article’s examples.


How TradingView Pine evaluates multiple operators in an expression depends on two things: the operators’ priority and their order. Operators with a higher priority are evaluated before those with a lower priority. And operators that have the same priority are evaluated from left to right. Parentheses have the highest priority of all, and so we can use them to change the operators’ priority.


Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

TradingView (n.d.). Script Language Reference Manual. Retrieved on September 22, 2015, from