Home » Color Functions » barcolor Function in Pinescript

barcolor Function in Pinescript

Photo of author
Published on

In this blog post, we’re going to dive deep into the world of Pine Script, focusing specifically on the barcolor function. This function is instrumental in coloring chart bars according to specified criteria, adding a visual dimension to your analysis and making it easier to identify patterns or trends in the data.

Understanding the barcolor Function

The barcolor function in Pine Script is a powerful tool that enables the coloring of bars on your chart based on various parameters. It allows you to define color, shift the color series, make the barcolor style editable, define the number of bars to fill, title the barcolor, and control where the barcolor is displayed. Syntax

The syntax of the barcolor function is as follows:

barcolor(color, offset, editable, show_last, title, display) → void

Let’s break down each parameter:

  • color (series color): This required argument defines the color of bars. You can use constants such as ‘red’ or ‘#ff001a’, or complex expressions like ‘close >= open ? color.green : color.red’.
  • offset (series int): This argument shifts the color series to the left or right by the specified number of bars. The default value is 0.
  • editable (const bool): If set to true, the barcolor style will be editable in the Format dialog. The default is true.
  • show_last (input int): This parameter, if set, defines the number of bars (from the last bar back to the past) to fill on the chart.
  • title (const string): This optional argument is used to title the barcolor.
  • display (input plot_simple_display): This controls where the barcolor is displayed. Possible values are: display.none, display.all. The default is display.all.

Unique Use Case Example

Let’s take a look at an interesting use case, wherein we want to color the bars based on whether they are above or below a Simple Moving Average (SMA) of 14 periods.

indicator("SMA Barcolor Example", overlay=true)
sma14 = ta.sma(close, 14)
barcolor(close > sma14 ? color.green : color.red)
barcolor Function

In this script:

  • We start by specifying the version of Pine Script with //@version=5.
  • Then, we define an indicator script with the indicator function and set the overlay parameter to true to display our script on the price chart.
  • We calculate the 14-period Simple Moving Average (SMA) of the close prices and assign it to sma14.
  • Lastly, we use barcolor function where if the closing price is greater than the sma14, the bar will be colored green, otherwise red.

Key Takeaways

The barcolor function in Pine Script provides a potent tool for visualizing trading conditions, enabling users to color bars based on various criteria, aiding in swift data interpretation. By understanding how to manipulate this function, you can make your custom scripts more intuitive and valuable.


The barcolor function is one of the many flexible features offered by Pine Script, allowing traders to customize the look of their charts for effective visualization of trading patterns. By properly harnessing the power of this function, one can create visually compelling, easy-to-read scripts that can greatly enhance their trading decision-making process.

Leave a Comment