Coding scripts in TradingView require operators. But what are operators and which ones are there?

In this article:

What are operators, operands, and expressions?

Before looking at this chapter’s articles, let’s discuss some terminology first. An operator is a code element that performs a certain action on one or several values to create a different value (Stephens, 2014). The values that an operator ‘operates on’ are called operands (Sharp, 2013).

So when we for example add two numbers together, then the plus sign (+) is the operator that performs an action (addition in this case) on several values (our two numbers). The new value that’s created by the addition operator is the sum of the two numbers.

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

Visual explanation of operators and operands

As we can see, one statement (a line of code) can have several operators. Which operator is evaluated first depends on the operators’ priority.

Another term that you’ll see in this chapter and in the TradingView documentation (see TradingView, n.d.) is expression. An expression is a piece of code that returns a value (Albahari & Albahari, 2012), and these often contain an operator. Examples of expressions are 45 * 9.12 (returns 410.4), ema(close, 20) (returns the 20-period Exponential Moving Average of closing prices), and (high - low) / 2. That latter example has two expressions in fact; high - low returns the bar’s range and (high - low) / 2 returns the value of the bar’s range divided by two.

Classifying operators based on their number of operands

Operators can be classified as unary, binary, or ternary depending on the number of operands that an operator acts upon (Albahari & Albahari, 2012; Pine Script Language Tutorial, n.d.; Sempf, Sphar, & Davis, 2010):

  • An unary operator works on a single value. There are three operators in TradingView that can be used unary: addition (+), subtraction (-), and the history referencing operator ([ ]). -close, for example, returns the negative value of the closing price.
  • Most TradingView operators are binary, which means that they operate on two values: one on the left and one on the right. Examples of these operators are the multiplication operator (like close[1] * 2) and the greater than comparison operator (close > open).
  • There’s one ternary operator in TradingView that takes three operands: the conditional ternary operator (?:). This operator’s first operand is a condition that causes the second operand to be returned when true and the third returned when it evaluates to false.

Now that we’ve the terminology clear, let’s look at this chapter’s articles.

Standard TradingView operators: assignment and arithmetic

We start by exploring TradingView’s assignment operator that stores values in variables and assigns values to a function’s keyword arguments. Next is a closer look at the arithmetic operators:

Accessing values from previous bars in TradingView

Practically every script uses historical data in its calculations. That’s possible with the history referencing operator:

  • In retrieving data from previous bars we discuss how this history referencing operator ([ ]) works and how it retrieves historical values from built-in variables, user-made variables, built-in functions, and custom functions.

Creating if/else statements in TradingView Pine

TradingView doesn’t have if/else statements that programming languages typically have (see Pine Script Language Tutorial, n.d.). We can, however, imitate if/else behaviour with an operator and a function:

  • The conditional ternary operator (?:) has three operands: a true/false condition and two other operands of which one is returned depending on the condition. That makes ?: act like a concise if/else statement.
  • While not technically an operator, the iff() function in TradingView behaves the same as the conditional ternary operator but is easier to work with.

TradingView’s operators for true/false values

In the chapter’s next part we discuss how to work with Boolean true/false values in TradingView Pine:

Creating custom functions in TradingView Pine

After that we explore creating functions so that we can program repeating calculations with less code:

TradingView’s order of operations

We end the chapter with the following:

We conclude the chapter with a summary of operators in TradingView where we repeat the most important parts.


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/

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.

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