Best Pine Script V5 & V6 Tutorial

Master Pine Script V5 and V6 with our comprehensive TradingView guide. Start your trading journey today!

Assignment Operators | Pine Script Tutorial

Discover the fundamentals of the assignment operator in Pine Script. This tutorial provides an in-depth explanation of how to use the assignment operator to store values in variables, enabling you to write effective trading strategies and indicators. Perfect for beginners and seasoned traders alike, learn the best practices and enhance your Pine Script programming skills today!

Last updated on November 17, 2024
 

 
 
The assignment operator is the most frequently used operator in TradingView scripting. Understanding how it functions and how to use it effectively is essential for creating scripts.

The Assignment Operator: A Primer

An operator is a code element that performs actions on values, known as operands, and returns results. The assignment operator (=) specifically assigns the value on its right to the variable on its left. This operator is also used to assign values to function arguments.
 

Assigning Values to Variables

Values are assigned to variables using the assignment operator as follows:
closingPrice = 150 prevVolume = volume[1] smaValue = ta.sma(close, 20)
In this example:
  • closingPrice is assigned a literal value of 150.
  • prevVolume retrieves the previous bar’s volume using the history referencing operator ([ ]).
  • smaValue stores the result of the ta.sma() function, which calculates the simple moving average.
 

Applying the Assignment Operator to Function Arguments

The assignment operator is also utilized within annotated functions that accept keyword arguments. For instance:
indicator(title="[Pine Script Tutorial] Example script", overlay=true)
Here, the indicator() function sets the title argument to "Pine Script Tutorial] Example script" and the overlay argument to true.
 

Constraints on Variable Reassignment

A notable limitation in TradingView's Pine Script is that once a variable is assigned a value using the assignment operator, it cannot be reassigned another value. This behavior contrasts with typical programming languages, where variables can change values.
 
For example, the following code will generate an error:
//@version=5 indicator(title="[Pine Script Tutorial] Assignment operator - example 1", overlay=true) averageClose = (close + close[1]) / 2 averageClose = ta.sma(close, 10) // This will trigger an error
notion image
 
To work around this limitation, create new variables instead:
//@version=5 indicator(title="[Pine Script Tutorial] Assignment operator - example 1", overlay=true) averageClose = (close + close[1]) / 2 smaClose = ta.sma(close, 10) // No error here plot(smaClose, linewidth=3)
notion image

Understanding the Difference Between Assignment and Equality

It’s crucial not to confuse the assignment operator (=) with the equality comparison operator (==). The assignment operator assigns a value, while the equality operator checks if two values are identical.
For instance, consider this erroneous code:
//@version=5 indicator(title="[Pine Script Tutorial] Assignment operator - example 2", overlay=true) currentBarColor = high = ta.highest(high, 20) ? color.green : color.red // Corrected version barcolor(currentBarColor)
notion image
 
This should instead use the equality operator:
//@version=5 indicator(title="[Pine Script Tutorial] Assignment operator - example 2", overlay=true) currentBarColor = high == ta.highest(high, 20) ? color.green : color.red // Corrected version barcolor(currentBarColor)
notion image
 

Key Tips for Assignment Operator Usage

To write clear and effective Pine Script code, follow these straightforward guidelines when using the assignment operator:
  • Name variables clearly: Pick names that show what the variable does.
  • Set variables early: Assign values at the start of your script.
  • Don't overuse assignments: Plan carefully since you can't reassign variables.
  • Add helpful comments: Explain complex assignments with brief notes.
  • Watch variable scope: Make sure you assign values in the right part of your script.
Stick to these tips to create Pine Script code that's easier to read, fix, and improve.
 

Summary

The assignment operator (=) is a fundamental component of TradingView's Pine Script language. It allows users to assign values to variables and function arguments, enabling the creation of dynamic and responsive trading scripts. However, it's important to note the limitations on variable reassignment and to avoid confusing it with the equality operator (==). By mastering the use of the assignment operator, traders can create more efficient and effective scripts for their technical analysis and trading strategies.
 

Citations