Skip to main content
On this page

Pine Script Bar Colors: Conditions, Offsets and Repainting

· 5 min read
Pineify Team
Pine Script and AI trading workflow research team

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.

Enhancing TradingView Charts with Pine Script Bar Colors

Read the condition before reading the color​

The Best Pine Script Generator

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.

ArgumentPurposeWhat it does not establish
colorSelect a color for each bar; na leaves the bar unchangedWhether the condition predicts returns
offsetShift where a calculated color is drawnWhen the underlying information became known
editableControl whether the coloring can be edited in the Style settingsA user input for every part of the calculation
show_lastRestrict coloring to a number of recent barsRestrict the history needed by the calculation
titleIdentify the coloring outputA strategy rule
displayControl display visibilityA 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​

  1. Select a symbol, session and timeframe; record them with the settings.
  2. Compare selected bars with the raw inputs in the Data Window. Include equal values, unavailable values and the earliest calculated bars.
  3. Observe an open bar, its close and the chart after reload. Note whether the behavior matches the intended timing rule.
  4. If using another timeframe, repeat around that timeframe's close.
  5. 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.

Does offset stop bar colors from repainting?
No. It shifts the drawing position. Confirmation must come from the data and evaluation rule.
Does red always mean below the SMA in this example?
No. Red also includes equality and the initial unavailable-SMA comparison because the condition selects green only when it is true.
Can a bar retain its normal chart color?
Yes. A color result of na leaves that bar unchanged.
Are volume colors evidence of institutional activity?
No. They can show a volume condition without identifying who traded or why.