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

The assignment operator (=) in TradingView

The assignment operator is used more often than any other operator. How does this operator work and how can we use it?

IN THIS ARTICLE:

  • The assignment operator in TradingView
  • Assigning values to variables with the assignment operator
  • Using the assignment operator with function arguments
  • Assigning a variable a different value: a TradingView limitation
  • Tip: don"t confuse assignment (=) with equality (==)
  • Summary
# The assignment operator in TradingView

Operators do an important part of a script"s ‘work’. An operator is a code element that performs an action on one or multiple values followed by returning a result (Stephens, 2014). We call the values that an operator ‘operates on’ operands (Sharp, 2013).

The most-used operator in TradingView is the assignment operator (=). This operator requires two operands and assigns whatever is on the right of = to the variable that"s on its left. We also use this operator when assigning a function argument a value (Pine Script Language Tutorial, n.d.). Let"s take a closer look at each of these two uses.

# Assigning values to variables with the assignment operator

We assign values to variables with the assignment operator like this:

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

Here first assign a literal value of 100 to the closingPrice variable. With the history referencing operator ([ ]) we then retrieve the previous bar"s volume (volume[1]) and put this value into prevVolume with the assignment operator. Lastly, we store the value returned by the ema() function (which calculates the exponential moving average; TradingView, n.d.) in the emaValue variable series.

# Using the assignment operator with function arguments

The assignment operator is also used inside the argument list of annotated functions (see Pine Script Language Tutorial, n.d.). An annotated function is a built-in function that accepts keyword arguments to set specific arguments to a certain value (Pine Script Language Tutorial, n.d.). That sounds very technical, so let"s look at an example to clarify things:

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

Here we used the annotated function that"s included in every script: study(). Two of its keyword arguments are set: title and overlay. With the assignment operator we set the title argument to "Example script" while the equal sign is used to set overlay to true.

# Assigning a variable a different value: a TradingView limitation

The assignment operator in TradingView Pine has an odd limitation that"s unlike the behaviour of other programming and scripting languages: once a variable is assigned a value by the assignment operator, it cannot be given another value (Pine Script Language Tutorial, n.d.). That"s ironic since a variable is named ‘variable’ because its value varies (Stellman & Greene, 2010).

For example, this isn"t allowed:

study(title="Assignment operator - example 1", overlay=true) averageClose = (close + close[1]) / 2 averageClose = sma(close, 10) plot(series=averageClose, linewidth=3)

Here we first assign the averageClose variable the sum of the current and previous close divided by 2. Then we use the assignment operator to assign a value to averageClose again. That triggers a TradingView error however:

Variable already assigned error in TradingViewVariable already assigned error in TradingView

We can often work around this limitation by creating several variables like so:

study(title="Assignment operator - example 1", overlay=true) averageClose = (close + close[1]) / 2 smaClose = sma(close, 10) plot(series=smaClose, linewidth=3)

Instead of assigning a value to the averageClose variable twice, we"ve replaced the second use of averageClose with a new variable (smaClose). The script now saves successfully and can be plotted on the chart:

TradingView chart exampleTradingView chart example# Tip: don"t confuse assignment (=) with equality (==)

The assignment operator (=) uses one equal sign while the equality comparison operator (==) uses two. The difference between these is that the assignment operator assigns the value on its right to the variable on its left. The equality operator, on the other hand, tests whether the value on its right is identical to the value on its left (Pine Script Language Tutorial, n.d.). An error that even experienced programmers make from time to time is confusing these two (Liberty & MacDonald, 2009).

An example indicator that uses the assignment operator (=) when it should use the equality operator (==) is the following:

study(title="Assignment operator - example 2", overlay=true) colour = (high = highest(high, 20)) ? green : red barcolor(color=colour)

Here the colour variable is assigned a value that"s returned by the conditional ternary operator (?:). That operator evaluates a condition and, when the condition is true, returns its second operand (green here). Otherwise, its third operand (red) is returned (TradingView, n.d.).

The condition evaluated by that operator is high = highest(high, 20), which normally is true when the current bar"s high (high) is the highest high of the last 20 bars (highest(high, 20)). But the problem here is that we"ve used the assignment operator (=) when it should have been the equality operator (==). After all, we"re testing for equality and not assigning. And so TradingView generates the ‘no viable alternative’ error:

TradingView: no viable alternative errorTradingView: no viable alternative error

Likewise, we can mistakenly use the == (equality operator) when we mean = (assignment operator), like this:

study(title="Assignment operator - example 2", overlay=true) colour == (high == highest(high, 20)) ? green : red barcolor(color=colour)

Here the previous error is fixed: the condition (high == highest(high, 20)) now uses the equality operator. But instead of using the = operator to assign the value returned by the conditional operator (?:) to the colour variable, we"ve used the equality comparison operator (==). This also triggers the ‘no viable alternative’ error when the script is saved:

TradingView: no viable alternative errorTradingView: no viable alternative error

The correct code example is, of course, using the assignment operator (=) to give the colour variable its value and using the equality operator (==) to check if the high is the highest high of the last 20 bars:

study(title="Assignment operator - example 2", overlay=true) colour = (high == highest(high, 20)) ? green : red barcolor(color=colour)

This version of the script gives no TradingView errors:

Compile error in TradingView fixedCompile error in TradingView fixed

And the script looks like this when added to the chart:

TradingView example of coloured price barsTradingView example of coloured price bars

Other often-used operators besides the assignment operator are: arithmetic operators for doing math, comparison operators that compare values against each other, and the logical operators which combine several true and false expressions.

# Summary

The assignment operator (=) gives variables a value and sets keyword arguments to a value. Its left operand is set to whatever is on the right side of the equal sign. With the assignment operator we cannot assign values to existing variables.

References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. 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.

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O"Reilly Media.

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

TradingView (n.d.). Script Language Reference Manual. Retrieved on September 5, 2015, from https://www.tradingview.com/study-script-reference/

Published October 31, 2015.
# Related TradingView tutorials
  • Combining strings with TradingView"s addition operator

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

  • Working with logical operators in TradingView Pine

    This TradingView programming tutorial discusses how the logical operators work, and how they can combine several true/false values into one Boolean.

  • Getting historical data with the history referencing operator

    In this programming article we discuss TradingView"s history referencing operator, and how we can use it to get data from previous price bars.

  • 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.

  • 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 (=>).

« All TradingView operators articles


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