Coding scripts in TradingView require operators. But what are operators and which ones are there?
IN THIS ARTICLE:
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:
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 operandsOperators 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):
Now that we"ve the terminology clear, let"s look at this chapter"s articles.
# Standard TradingView operators: assignment and arithmeticWe 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:
Practically every script uses historical data in its calculations. That"s possible with the history referencing operator:
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:
In the chapter"s next part we discuss how to work with Boolean true/false values in TradingView Pine:
After that we explore creating functions so that we can program repeating calculations with less code:
We end the chapter with the following:
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/
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.
In this TradingView tutorial we discuss with code examples the operators’ priority that determines which operator is executed first.
In this TradingView programming article we look at the comparison operators, including several script examples that show how these operators work.
In this TradingView tutorial we discuss how to create single-line and multi-line functions with the function declaration operator (=>).
In this TradingView programming article, we discuss how the addition operator (+) can also combine strings together.