The Assignment Operator: A PrimerAssigning Values to VariablesApplying the Assignment Operator to Function ArgumentsConstraints on Variable ReassignmentUnderstanding the Difference Between Assignment and EqualityKey Tips for Assignment Operator UsageSummaryCitations
Â
Â
Â
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 theta.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
Â
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)
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)
Â
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)
Â
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.
Â