Home » Color Functions » Understanding Conditional Coloring in Pine Script

Understanding Conditional Coloring in Pine Script

Photo of author
Published on

Conditional coloring in Pine Script is a powerful feature that allows you to visually represent different states or conditions in your trading scripts. By using this technique, you can make your indicators more intuitive and informative. Let’s dive into the concept of conditional coloring, particularly focusing on its application in coloring moving averages and pivot points.

Coloring Moving Averages Based on Direction

We’ll start by looking at how to color a moving average based on whether it is rising (bullish) or not (bearish).

indicator("Conditional colors", "", true)
int   lengthAlter = input.int(20, "Length", minval = 2)
color maBullishColor = input.color(color.green, "Bull")
color maBearishColor = input.color(color.maroon, "Bear")
float maValue = ta.sma(close, lengthAlter)
// Define states.
bool maIsRising  = ta.rising(maValue, 1)
// Build color.
color maColor = maIsRising ? maBullishColor : maBearishColor
plot(maValue, "MA", maColor, 2)
Coloring Moving Averages Based on Direction


  • Selection of Colors: The script allows users to choose colors for bullish and bearish conditions.
  • Boolean Variable maIsRising: This variable is true when the moving average is higher on the current bar than the previous one.
  • Color Variable maColor: It is assigned either the bullish or bearish color based on the maIsRising boolean. The ternary operator (? :) facilitates this conditional assignment.

Using Conditional Colors to Control Plotting

Now, let’s explore how to use conditional colors to avoid plotting under certain conditions, such as during pivot transitions.

indicator("Conditional colors", "", true)
int legsAlter = input.int(5, "Pivot Legs", minval = 1)
color pHighColor = input.color(color.olive, "High pivots")
color pLowColor = input.color(color.orange, "Low pivots")
// Initialize pivot level variables.
var float pivotHigh = na
var float pivotLow = na
// Detect new pivot and save its value.
pivotHigh := nz(ta.pivothigh(legsAlter, legsAlter), pivotHigh)
pivotLow := nz(ta.pivotlow(legsAlter, legsAlter), pivotLow)
// Avoid plotting color on pivot change.
plot(pivotHigh, "High", ta.change(pivotHigh) ? na : pHighColor, 2, plot.style_line)
plot(pivotLow, "Low",  ta.change(pivotLow) ? na : pLowColor, 2, plot.style_line)
Conditional Colors to Control Plotting


  • Pivot Detection: The script uses ta.pivothigh() and ta.pivotlow() functions to detect high and low pivots, respectively.
  • Handling na Values: The nz() function is used to maintain the last pivot value when a new pivot is not found.
  • Conditional Plotting: The script plots lines for pivots, but it uses a ternary operator to return na as the color when a pivot change is detected, preventing a line from being plotted for that bar.

Key Features and Takeaways

  • Function Usability: Conditional coloring in Pine Script enhances the usability of scripts by visually representing different market states or conditions.
  • Syntax and Application: The ternary operator (? :) is crucial for implementing conditional statements efficiently in Pine Script.
  • Practical Application: This technique is particularly useful in indicators like moving averages or pivot points, providing a clear visual distinction between different market conditions.

Leave a Comment