Topics like health, travel, finance, home, living, education and careers | Media Search Home

Introduction to TradingView"s operators

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

IN THIS ARTICLE:

  • What are operators, operands, and expressions?
  • Classifying operators based on their number of operands
  • Standard TradingView operators: assignment and arithmetic
  • Accessing values from previous bars in TradingView
  • Creating if/else statements in TradingView Pine
  • TradingView"s operators for true/false values
  • Creating custom functions in TradingView Pine
  • TradingView"s order of operations
# 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 operandsVisual 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:

  • First we examine the addition (+), subtraction (-), division (/), and multiplication (*) arithmetic operators and look at how + and - can be used on a single operand.
  • Then we discuss how strings can be joined together with + in adding strings with the addition operator.
  • In working with the modulus operator we use the % operator to return the remainder of integer division.
# 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:

  • In TradingView"s comparison operators we discuss how to compare numbers against each other (like greater than or less than or equal to) such that we end up with a true/false value.
  • Then in combining true/false values with logical operators we look at how multiple true/false expressions can be combined into a single true or false value.
# Creating custom functions in TradingView Pine

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

  • The function declaration operator (=>) allows us to create our own single-line and multi-line functions.
# TradingView"s order of operations

We end the chapter with the following:

  • In the priority of operators in TradingView Pine we discuss the order in which operators are evaluated and how we can change that order with parentheses.
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/

Published October 23, 2015.
# Related TradingView tutorials
  • The iff() function: an alternative to the conditional operator

    In this programming article we learn about the iff() function in TradingView Pine, which allows us to implement if/else logic in our trading scripts.

  • The priority of operators in TradingView Pine

    In this TradingView tutorial we discuss with code examples the operators’ priority that determines which operator is executed first.

  • Comparing values for true or false with TradingView"s comparison operators

    In this TradingView programming article we look at the comparison operators, including several script examples that show how these operators work.

  • Creating functions with TradingView"s function declaration operator

    In this TradingView tutorial we discuss how to create single-line and multi-line functions with the function declaration operator (=>).

  • Combining strings with TradingView"s addition operator

    In this TradingView programming article, we discuss how the addition operator (+) can also combine strings together.

« All TradingView operators articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved