Home » Plot Functions » Plot bar Function in Pine Script

Plot bar Function in Pine Script

Photo of author
Published on

In Pine Script, the plotbar function plays an essential role in plotting ohlc (open-high-low-close) bars on the trading chart. This function enhances visualization by representing data in the form of bars, aiding in understanding and analyzing market behavior effectively. This blog post will dive deeper into the function’s usage, its syntax, and arguments. Lastly, we will see a unique use case that illustrates its application.

Understanding plotbar Function

The syntax for plotbar function is as follows:

plotbar(open, high, low, close, title, color, editable, show_last, display) → void

The function does not return any value (void), but instead plots the ohlc bars on the chart using the parameters provided.

Exploring the Arguments

Let’s unpack the arguments one by one:

  • open, high, low, close: These four required arguments represent the open, high, low, and close values of the bars, respectively. These are data series of int/float type.
  • title: This optional argument provides a title to the plotbar. It’s a string constant.
  • color: This optional argument sets the color of the bars. You can set it to color constants or use complex expressions to change the color dynamically based on conditions.
  • editable: Another optional argument, which, when set to true, makes the plotbar style editable in the Format dialog. The default value is true.
  • show_last: An optional input integer that, when set, defines the number of bars to be plotted from the last bar back to the past.
  • display: This optional argument controls where the plot’s information is displayed, with display.all as the default value.

A Practical Use Case of plotbar Function

To illustrate the usage of the plotbar function, let’s code a simple script that plots ohlc bars with color changing based on the relationship between opening and closing values.

//@version=5
indicator("Dynamic Color PlotBar Example", overlay=true)

myColor = close >= open ? color.green : color.red
plotbar(open, high, low, close, title='OHLC', color=myColor, editable=true, show_last=50)
Plot bar Function

In the above script:

  • We set myColor based on whether close is greater than or equal to open. If it is, the color is green, otherwise, it’s red.
  • We then call the plotbar function using the open, high, low, and close series from our data, with a title ‘OHLC’.
  • We set the color of the bars to myColor.
  • editable is set to true, enabling you to change the plotbar style in the Format dialog.
  • show_last is set to 50, so only the last 50 bars will be displayed on the chart.

This script can be used to visually analyze the opening and closing trends over the last 50 periods in an interactive manner.

Key Takeaways

The plotbar function in Pine Script is an essential tool for visualizing trading data in the form of ohlc bars. The function provides flexibility by allowing the customization of color, editability, and the range of data to display. Understanding how to properly implement and manipulate the plotbar function can significantly enhance the effectiveness of your trading scripts.

Conclusion

Mastering the plotbar function can significantly elevate your trading analysis and strategy development in Pine Script. It offers the power to represent market data in a more visual and intuitive way, thus enabling better decision-making. By exploring its unique capabilities and understanding its arguments, you can leverage this function to unlock new opportunities in the world of trading.

Leave a Comment