barcolor() - Pine Script Function
barcolor()
Section titled “barcolor()”Overview
Section titled “Overview”barcolor() overrides the color of the current chart’s price bars. You can pass a literal color (e.g., color.green) or any series color expression, which makes it ideal for highlighting bullish/bearish regimes. Passing na leaves the bar unchanged.
Syntax
Section titled “Syntax”barcolor(color, offset, editable, show_last, title, display) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
color | series color | Color to apply to the bar. Use na to leave the bar unchanged. Required. |
offset | series int | Shift the application forward (> 0) or backward (< 0) bars. Defaults to 0. |
editable | const bool | Whether the color can be adjusted from the style settings. Defaults to true. |
show_last | series int | Apply coloring only to the most recent n bars. Defaults to na (all bars). |
title | const string | Label shown in the style settings. |
display | const string | Where the coloring applies (see display.* constants). |
Remarks
Section titled “Remarks”barcolor()affects only the script that calls it; other indicators on the chart can still override colors.- Use
color.new(color, transp)to adjust transparency instead of RGB literals. - Combine with
bgcolor()orplotfor additional context, but remember each call counts toward the plot limit.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=5indicator("barcolor example", overlay=true)
isUp = close >= opencolorUp = color.new(color.green, 0)colorDown = color.new(color.red, 0)
barcolor(isUp ? colorUp : colorDown)
plotshape(isUp, title="Up", location=location.belowbar, style=shape.triangleup, color=colorUp)plotshape(not isUp, title="Down", location=location.abovebar, style=shape.triangledown, color=colorDown)