Home » Plot Functions » Conditional Plotting in Pine Script

Conditional Plotting in Pine Script

Photo of author
Published on

Creating effective visual indicators in trading charts often requires the ability to plot data conditionally. In Pine Script, the scripting language for TradingView, plot() calls can’t be directly used within conditional structures like, but you can control their behavior in various ways. This article will explore two primary methods: value control and color control.

Value Control

Understanding Conditional Plotting with plot()

Pine Script allows you to control the display of plots by plotting na (not available) values or using colors with full transparency to effectively hide them. This is particularly useful when dealing with discontinuous data sets.

Example: Discontinuous Plots

Consider the following script:

indicator("Discontinuous plots", "", true)
bool plotValues = bar_index % 3 == 0
plot(plotValues ? high : na, color = color.fuchsia, linewidth = 6, style = plot.style_linebr)
plot(plotValues ? high : na)
plot(plotValues ? math.max(open, close) : na, color = color.navy, linewidth = 6, style = plot.style_cross)
plot(plotValues ? math.min(open, close) : na, color = color.navy, linewidth = 6, style = plot.style_circles)
plot(plotValues ? low : na, color = plotValues ? color.green : na, linewidth = 6, style = plot.style_stepline)

Key Features:

  • Conditional Plotting: The plotValues boolean controls the plotting, activated every three bars (bar_index % 3 == 0).
  • Plot Styles:
    • plot.style_linebr: Creates a fuchsia line on high values.
    • Default style: A thin blue line that bridges over na values.
    • plot.style_cross and plot.style_circles: Navy blue crosses and circles plotted on candle tops and bottoms.
    • plot.style_stepline: Green step line on lows, with segments wider than those in plot.style_linebr.

Customized Plotting After a Specific Date

To restrict plotting to bars after a specific date:

indicator("", "", true)
startInput = input.time(timestamp("2021-01-01"))
plot(time > startInput ? close : na)
Customized Plotting After a Specific Date

Color Control

Color control in plotting is vital for creating visually distinct and informative charts.

Constant Colors

You can use built-in constant colors or color literals for a consistent look:

indicator("", "", true)
plot(close, color = color.gray)
Constant Colors

Dynamic Color Based on Chart Type

For more dynamic plotting, the color can be determined based on the chart type:

indicator("", "", true)
plotColor = switch syminfo.type
    "stock"     => color.purple
    "futures"   => color.red
    "index"     => color.gray
    "forex"     => color.fuchsia
    "crypto"    => color.lime
    "fund"      => color.orange
    "dr"        => color.aqua
    "cfd"       => color.blue
plot(close, color = plotColor)
Dynamic Color Based on Chart Type

Conclusion and Key Takeaways

  • Value Control: Plotting na values or using fully transparent colors allow you to control plot visibility conditionally.
  • Dynamic Plot Styles: Different plot styles (like linebr, cross, circles, stepline) offer unique visual representations for specific data points.
  • Conditional Plotting: Use conditions based on bar index or time to control when to plot.
  • Color Control: Use constant colors for consistency or dynamic colors based on chart types for a more contextual display.

Leave a Comment