Home » Pinescript Syntax » Variable Reassignment in Pine Script

Variable Reassignment in Pine Script

Photo of author
Published on

Introduction to Variable Reassignment

Variable reassignment in Pine Script is a crucial aspect of script development, especially when dealing with dynamic data and conditions in trading indicators and strategies. This tutorial aims to demystify the concept and application of reassignment, ensuring that you can utilize this feature effectively in your scripts.

What is Variable Reassignment?

Variable reassignment is the process of assigning a new value to an already declared variable. In Pine Script, this is done using the := operator. This operation is essential for updating variable values based on new information or conditions in a script’s execution.

The Syntax and Usage

The syntax for variable reassignment is straightforward:

variableName := newValue

Here, variableName is the previously declared variable, and newValue is the value being assigned to it. It’s important to note that reassignment can only occur after a variable has been declared with an initial value.

A Practical Example

Let’s delve into a practical example to illustrate the concept better.

Example: Dynamic Color Assignment to a Moving Average

In this example, we’ll create a script that changes the color of a moving average (MA) based on its direction.

indicator("", "", true)
sensitivityInput = input.int(2, "Sensitivity", minval = 1, tooltip = "Higher values make color changes less sensitive.")
ma = ta.sma(close, 20)
maUp = ta.rising(ma, sensitivityInput)
maDn = ta.falling(ma, sensitivityInput)

var maColor = color.gray // Initial color set to gray
if maUp
    maColor := color.lime // Change to lime if MA is rising
else if maDn
    maColor := color.fuchsia // Change to fuchsia if MA is falling

plot(ma, "MA", maColor, 2)
Variable Reassignment

Line-by-Line Explanation

  1. Initialization: We set up the indicator and declare the initial sensitivity for our MA.
  2. Moving Average Calculation: Here, we calculate the simple moving average (SMA) with a period of 20.
  3. Direction Detection: Two variables, maUp and maDn, detect if the MA is rising or falling.
  4. Color Assignment: Initially, the color of the MA (maColor) is set to gray. Using the if-else conditions, we reassign maColor to lime or fuchsia based on the MA’s direction.

Key Takeaways

  • Variable reassignment in Pine Script uses the := operator.
  • It allows the script to adaptively change the values of variables based on new data or conditions.
  • Reassignment is crucial for making scripts dynamic and responsive to market conditions.


Understanding and effectively using variable reassignment in Pine Script is fundamental for creating dynamic and adaptive trading scripts. By mastering this concept, you can develop more sophisticated and responsive indicators and strategies, enhancing your trading analysis and decision-making.

Remember, variable reassignment is not just a feature but a powerful tool in your scripting arsenal to make your scripts react to market conditions in real-time. Happy coding!

Leave a Comment