Home » Plot Functions » Understanding Plots Function in Pine Script

Understanding Plots Function in Pine Script

Photo of author
Published on

Pine Script, the scripting language for TradingView charts, offers a versatile tool in the form of a plot() function. This function is indispensable for visualizing various data types and trends in financial charting. Let’s delve into the nuances of the plot() function and explore how it enhances the visualization of trading strategies and indicators.

Introduction to the plot() Function

Basic Usage

The plot() function is primarily used to display information calculated using Pine scripts. Its versatility allows it to plot various styles, including:

  • Lines
  • Histograms
  • Areas
  • Columns (like volume columns)
  • Fills
  • Circles or Crosses

Syntax Overview

The basic syntax of the plot() function in Pine Script is as follows:

//@version=5
indicator("plot()", "", true)
plot(series, title, color, linewidth, style)
  • series: The data series to be plotted.
  • title: A string denoting the title of the plot.
  • color: The color of the plot.
  • linewidth: The width of the plot line.
  • style: The style of the plot (line, cross, circles, etc.).

Example

//@version=5
indicator("`plot()`", "", true)
plot(high, "Blue `high` line")
plot(math.avg(close, open), "Crosses in body center", close > open ? color.lime : color.purple, 6, plot.style_cross)
plot(math.min(open, close), "Navy step line on body low point", color.navy, 3, plot.style_stepline)
plot(low, "Gray dot on `low`", color.gray, 3, plot.style_circles)

color VIOLET = #AA00FF
color GOLD   = #CCCC00
ma = ta.alma(hl2, 40, 0.85, 6)
var almaColor = color.silver
almaColor := ma > ma[2] ? GOLD : ma < ma[2]  ? VIOLET : almaColor
plot(ma, "Two-color ALMA", almaColor, 2)
Example

Detailed Explanation

1. Plotting the Highs

plot(high, "Blue `high` line")
  • This line plots a 1-pixel blue line across the bar highs.

2. Plotting Crosses at Mid-Points

plot(math.avg(close, open), "Crosses in body center", close > open ? color.lime : color.purple, 6, plot.style_cross)
  • It plots crosses at the midpoint of the bars.
  • The crosses are colored lime for an upward bar and purple for a downward bar.
  • The 6 here is for the relative size, not the pixel value.

3. Navy Step Line

plot(math.min(open, close), "Navy step line on body low point", color.navy, 3, plot.style_stepline)
  • This creates a 3-pixel wide navy step line following the low point of the bodies.

4. Plotting Gray Circles

plot(low, "Gray dot on `low`", color.gray, 3, plot.style_circles)
  • This command plots a gray circle at the bars’ low point.

5. Two-Color ALMA (Arnaud Legoux Moving Average)

color VIOLET = #AA00FF
color GOLD   = #CCCC00
ma = ta.alma(hl2, 40, 0.85, 6)
var almaColor = color.silver
almaColor := ma > ma[2] ? GOLD : ma < ma[2] ? VIOLET : almaColor
plot(ma, "Two-color ALMA", almaColor, 2)
  • Defines bull/bear colors and calculates an ALMA.
  • Initially, the color is set to silver.
  • The color changes to gold or violet based on the ALMA’s value relative to its value two bars ago, reducing noise in color transitions.

Key Features and Takeaways

  • Function Versatility: plot() can display a wide range of data styles, making it an essential tool for financial charting.
  • Color and Style Customization: The function allows customization in terms of color, size, and style, enhancing visual clarity.
  • Conditional Formatting: As seen in the examples, conditional logic can be applied to change plot characteristics based on the data.
  • Focus on Readability: The use of descriptive titles and varied colors improves the readability of the plotted data.
  • Advanced Calculations: The incorporation of complex calculations like ALMA demonstrates the function’s ability to handle sophisticated trading indicators.

In conclusion, the plot() function in Pine Script is a powerful tool for financial data visualization, offering flexibility and customization to cater to a wide array of charting requirements. Its ability to plot different styles and respond to data conditions makes it an invaluable asset in developing comprehensive trading strategies and indicators.

Leave a Comment