This article provides a summary of the operators in TradingView Pine. Follow the links to the different articles for more information and examples.

In this article:

Operators, operands, and expressions in TradingView Pine

Talking about programming is often talking with special jargon, and that’s no different with TradingView’s operators. An operator is a code element that performs a certain action on one or several values and, by doing so, creates a new value (Stephens, 2014). We call the values that an operator ‘operates on’ operands (Sharp, 2013).

This difference between operators and operands is highlighted in the image below:

Example of the difference between operators and operands

One statement (that is, a line of code) can contain one or several operators. Those operators, in turn, make up an expression, which is a piece of code that returns a value (Albahari & Albahari, 2012). Examples of expressions are 2 * 5 (returns 10), high[1] (returns the previous bar’s high), and rsi(close, 7) (returns the 7-period RSI of closing prices).

The assignment operator in TradingView Pine

The most common operator is the assignment operator (=). This operator requires two operands and puts the value on the right of = into the value on its left (Pine Script Language Tutorial, n.d.). We can use this operator with variables and keyword arguments.

The assignment operator stores values in variables like this:

closingPrice = 100
prevVolume   = volume[1]
emaValue     = ema(low, 20)

The odd thing about the assignment operator is that it cannot assign another value to an existing variable (Pine Script Language Tutorial, n.d.). So this won’t work:

highestHigh = highest(high, 10)
highestHigh = max(high, high[1])

The second use of the assignment operator is assigning a value to a function’s keyword argument, like this:

study(title="Example script", overlay=true)

Arithmetic operators in TradingView Pine

TradingView has four arithmetic operators for the standard mathematical operators of addition (+), subtraction (-), multiplication (*), and division (/) (Pine Script Language Tutorial, n.d.). These operators work as you’d expect:

Operator Description Example Result
+ Addition 10 + 12.0 22.0
- Subtraction 22 - 9 13
* Multiplication 12 * -0.5 -6
/ Division 4 / 3.3 1.2121...

The addition (+) and subtraction (-) operators can also be used unary (meaning, applied to just one operand). Let’s assume that x has a value of 10 and y holds -5. Placing + and - before these operators then has the following effect:

Operator Expression Result
Addition (+) +x 10
+y -5
Subtraction (-) -x -10
-y 5

A special feature of the addition operator (+) is that it can combine strings too (provided both of its operands are also strings). When one of its operands is a number, we can convert it to a string with tostring() (TradingView, n.d.).

The modulus operator (%) is another arithmetic operator. It works on two operands and returns the remainder of integer division (Pine Script Language Tutorial, n.d.; TradingView, n.d.). So for instance 5 % 3 returns 2 (5 / 3 = 1 with a remainder of 2) while 25 % 3 results in 1 (25 / 3 = 8, remainder 1). This way we can use the modulus operator to easily find every nth occurrence.

Getting previous values with the history referencing operator

The history referencing operator ([]) is an operator that’s placed behind a variable or function. It then returns that variable or function’s previous value for a certain number of bars ago that we specify with a positive number between square brackets (Pine Script Language Tutorial, n.d.; TradingView, n.d.). For example, close[1] returns the previous bar’s closing price and ema(close, 5)[2] returns the 5-period EMA of 2 bars ago.

Note that the history referencing operator only works with variables and functions that had values on previous bars. Furthermore, this operator returns NaN values (“not a number”) when we try to access a value that’s beyond the number of bars available on the chart (Pine Script Language Tutorial, n.d.).

Creating if/else-like behaviour in TradingView Pine

TradingView doesn’t have the typical if/else statements that programming languages have. Instead, we get that behaviour with the conditional ternary operator (?:). This operator evaluates a true/false condition (its first operand) and returns its second operand when that condition is true or its third operand when the condition is false (Pine Script Language Tutorial, n.d.). Its default pattern is:

condition ? result1 : result2

The iff() function is the same as the conditional ternary operator but more straightforward to use. It also returns result1 when the condition is true and result2 when it’s false:

iff(condition, result1, result2)

We can evaluate multiple conditions by nesting the ?: operator or iff() function so that one line of code contains several of these operators or functions. The very last value then acts as the default: when none of the conditions evaluate to true, this last value is returned.

The values that the conditional ternary operator and iff() function return need to be the same type – so when result1 is an integer, result2 needs to be an integer also. The condition evaluated can be a true/false expression (like close > open) or a numerical expression (like high - high[1]). In that latter case values of 0, NaN, and +Infinity and -Infinity are considered false, while any other number is seen as true (TradingView, n.d.).

Comparing and combining for true/false values in TradingView

The comparison operators contrast two operands against each other and return true or false depending on the result (Pine Script Language Tutorial, n.d.). With these operators we create conditions so that our script doesn’t behave the same on every price bar.

The comparison operators in Pine are (Pine Script Language Tutorial, n.d.; TradingView, n.d.):

Operator Name Example and result
< Less than 10 < 3     True; 10 is less than 3.
close < close[2]     Returns true only when the current bar’s close is less than the close of two bars ago.
<= Less than or equal to close <= open     Returns only true when the bar’s close is lower than or equal to the bar’s open.
high <= high[1]     Only evaluates to true when the current bar’s high is less than or equal to the previous high.
!= Not equal volume != volume[1]     True for all price bars whose volume is different than the preceding bar; false for all other bars.
open != open[10]     Only returns true when the current bar’s open is unequal to the open of 10 bars ago.
== Equal high == high[1]     Results only in true when the current bar’s high is exactly the same as the high of the previous bar.
volume == sma(volume, 10)     Returns true when the current bar’s volume is identical to the average volume of the last 10 bars; returns false in all other cases.
> Greater than 10 > 3     False; 10 is not greater than 3.
close > ema(close, 30)     Returns only true when the current bar’s close is greater than the closing price’s Exponential Moving Average (EMA) of the last 30 bars.
>= Greater than or equal to high >= low[1]     Returns only true when the high of the current bar is greater than or equal to the previous bar’s low.
sma(close, 10) >= sma(close, 50)     Only returns true whenever closing price’s Simple Moving Average (SMA) is greater than or equal to the SMA of the last 50 closes.

There are also logical operators in TradingView. With those operators we combine several true/false values into a single true/false value. They allow us to code things like “did the current bar close higher and is the volume increasing?” and “did the bar gap down or did it close below the 10-period EMA?“.

The logical operators are (Pine Script Language Tutorial, n.d.):

Operator Name and meaning Example Result
and Logical conjunction x and y Only returns true when both x and y are true. Returns false when x is false, y is false, or both are false.
or Logical disjunction x or y Returns true when x is true, when y is true, or when both are true. Only returns false when both x and y are false.
not Negation not x Returns true when x is false, and returns false when x is true.

The next table shows the outcome of the different logical operators based on the values of two variables:

Values of x and y x and y x or y not x not y
x = true, y = true true true false false
x = true, y = false false true false true
x = false, y = true false true true false
x = false, y = false false false true true

Logical operators can, of course, also combine more than two values. And so we can check if three variables are all true (a and b and c) or if only one of them is true (a or b or c). With parentheses we can also evaluate if a and b are both true or that just c is true like this: (a and b) or c.

Creating TradingView functions with the function declaration operator

A TradingView function is reusable code that performs a specific action each time we use it. With the function declaration operator (=>) we can create our own single-line and multi-line functions (Pine Script Language Tutorial, n.d.).

Making a single-line function with => looks as follows:

FunctionName(comma-separated list of arguments) => expression

We create a multi-line function with this operator by indenting each line in the function with 1 tab and a space or with 5 spaces (see Pine Script Language Tutorial, n.d.), like this:

FunctionName(comma-separated list of arguments) =>
     statement
     statement
     ... etc
     expression, variable, or variable declaration

The last statement of a multi-line function is the value that’s returned by the function (Pine Script Language Tutorial, n.d.). That line of code can be an expression, variable, or variable declaration.

The priority of operators in TradingView Pine

When an expression has multiple operators, the order in which they are evaluated depends on the operators’ priority (Pine Script Language Tutorial, n.d.). Those rules determine which operator is evaluated first, which one is calculated next, and so on until all operators in an expression are computed.

The priority of TradingView’s operators is displayed below. Operators with a higher priority are evaluated before those with a lower priority, and operators are evaluated from left to right when they have the same 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

We change the operators’ priority with parentheses (Pine Script Language Tutorial, n.d.). Since they have the highest priority, expressions within parentheses are evaluated first. When parentheses are nested inside each other (like 9 - ((close * 2) + high)), then TradingView evaluates the innermost parentheses first (in other words, it works from the inside out).

The operators’ priority is the last topic in the chapter on TradingView operators. See all TradingView Pine programming tutorials to learn about other subjects.


References

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 https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

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 25, 2015, from https://www.tradingview.com/study-script-reference/