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
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 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) 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:
overlay. With the assignment operator we set the
title argument to
"Example script" while the equal sign is used to set
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 scripting languages like PowerLanguage: 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) / 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 an error however:
We can often work around this limitation by creating several variables like so:
study(title="Assignment operator - example 1", overlay=true) averageClose = (close + close) / 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:
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 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)
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:
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:
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 errors:
And the script looks like this when added to the chart:
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
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.
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/