Pine Script Bar Colors: Conditions, Offsets and Repainting
barcolor() changes the chart's price-bar colors according to a Pine expression. A color means that the expression selected that color; it does not independently identify institutional buying, a confirmed trend or a profitable entry.

Read the condition before reading the color
The retained example compares the current close with a 14-bar simple moving average. No trading performance is asserted for this code.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//======================================================================//
// ____ _ _ __ //
// | _ \(_)_ __ ___(_)/ _|_ _ //
// | |_) | | '_ \ / _ \ | |_| | | | //
// | __/| | | | | __/ | _| |_| | //
// |_| |_|_| |_|\___|_|_| \__, | //
// |___/ //
//======================================================================//
//@version=6
indicator('SMA Bar Colors', overlay = true)
sma14 = ta.sma(close, 14)
barcolor(close > sma14 ? color.green : color.red)
plot(sma14)
Its green branch requires close > sma14. Red includes equality as well as a close below the SMA. During the initial bars when the SMA is unavailable, the comparison is not true, so the red branch is also selected. That is a useful reason to distinguish unavailable data from a bearish state in a production design.
The line and the colored bars use the same SMA, but neither places orders. An indicator does not create a strategy trade list merely because its colors resemble buy and sell signals.
What the function's settings control
TradingView documents the arguments in its bar-coloring guide.
| Argument | Purpose | What it does not establish |
|---|---|---|
color | Select a color for each bar; na leaves the bar unchanged | Whether the condition predicts returns |
offset | Shift where a calculated color is drawn | When the underlying information became known |
editable | Control whether the coloring can be edited in the Style settings | A user input for every part of the calculation |
show_last | Restrict coloring to a number of recent bars | Restrict the history needed by the calculation |
title | Identify the coloring output | A strategy rule |
display | Control display visibility | A data-confirmation rule |
The parameter name is show_last, with an underscore. Bar coloring acts on the main chart's bars even if the indicator itself runs in a separate pane.
Why offset does not prevent repainting
In the example, both the current close and the SMA can change while a realtime bar is open. A green bar can therefore become red before that bar closes. Ordinary intrabar updates should not be described as a guarantee that the finalized bar will later change.
A negative offset draws a result on an earlier bar. It does not make that result available earlier. Using it to display a newly detected event in the past can make a historical chart misleading.
For chart-timeframe logic, decide whether to wait for the bar to be confirmed or to use consistently previous-bar inputs. Replacing only one input with close[1] while leaving the rest current can create a mixed-time rule. Higher-timeframe requests need their own confirmation treatment; confirming the chart bar alone does not close the daily bar. See TradingView's repainting explanation.
Design colors around a question
A useful color scheme separates states the reader can explain. Possible experiments include:
- Volume: distinguish volume above a chosen baseline. Volume alone does not reveal the buyer's identity or prove accumulation.
- RSI: distinguish above-threshold, below-threshold and intermediate readings. These are indicator states, not mandatory reversal points.
- ATR: show changes in measured range. A larger ATR says nothing by itself about direction.
- Multiple timeframes: display a higher-timeframe condition while keeping the requested bar's confirmation status explicit.
Choose a priority when conditions overlap. For example, if a high-volume condition takes precedence over an SMA condition, a single color cannot communicate both independently. Add a legend or a separate value display when that distinction matters.
Check the display and timing
- Select a symbol, session and timeframe; record them with the settings.
- Compare selected bars with the raw inputs in the Data Window. Include equal values, unavailable values and the earliest calculated bars.
- Observe an open bar, its close and the chart after reload. Note whether the behavior matches the intended timing rule.
- If using another timeframe, repeat around that timeframe's close.
- Inspect both light and dark themes and provide meaning beyond red-versus-green alone.
These are proposed verification steps, not a report of completed chart tests. For related display choices, see chart colors and conditional plotting.

