Skip to content

Give price bars different colours with TradingView's Pine Script

Give price bars different colours with TradingView’s Pine Script

Section titled “Give price bars different colours with TradingView’s Pine Script”

TL;DR
Combine barcolor() with conditional logic so each bar reflects trend, volatility, or any signal you define.

DifficultyBeginner
Time to implement10-15 min
CategoryVisual Effects
//@version=5
indicator("Coloured bars", overlay=true)
color up = color.new(color.green, 0)
color down = color.new(color.red, 0)
barcolor(close > close[1] ? up : down)
Tip. Return `na` from your colour logic whenever you want to keep TradingView’s default bar colour.

Colour-coding bars makes it easier to spot regimes: trend versus chop, high volatility versus calm, or bullish versus bearish signals. Pine Script lets you assign colours per bar, so you can provide immediate visual feedback without cluttering the chart with extra indicators.

  • Building colour palettes and reusing them
  • Cascaded if, conditional operator, and switch patterns for bar colours
  • Combining multiple conditions without conflicting outputs
  • Applying bar colours alongside other stylings (backgrounds, plots)
CallPurpose
barcolor(color)Sets the colour for the current bar.
color.new(color, transparency)Customises palette and transparency.
if … else / switchChoose a colour based on multiple conditions.
?: (conditional operator)Compact selection for simple cases.
naLeave a bar uncoloured (fallback to default).
  1. Define your colour palette
    Reuse named colours instead of hard-coding each branch.

    bullish = color.new(color.green, 0)
    bearish = color.new(color.red, 0)
    neutral = color.new(color.gray, 50)
  2. Pick the control structure
    Cascaded if, conditional operator, or switch ensure only one colour applies per bar.

    color barColour =
    close > ema ? bullish :
    close < ema ? bearish :
    neutral
  3. Apply the colour and avoid conflicts
    Call barcolor() once with the final colour decision.

    barcolor(barColour)
//@version=5
indicator("Multi-state bar colours", overlay=true)
emaFast = ta.ema(close, 21)
emaSlow = ta.ema(close, 55)
atrVal = ta.atr(14)
color bullishTrend = color.new(color.green, 0)
color bearishTrend = color.new(color.red, 0)
color squeeze = color.new(color.orange, 0)
color neutral = na
color barColour = neutral
if emaFast > emaSlow and close > emaFast + atrVal
barColour := bullishTrend
else if emaFast < emaSlow and close < emaFast - atrVal
barColour := bearishTrend
else if ta.tr(true) < atrVal * 0.5
barColour := squeeze
barcolor(barColour)
plot(emaFast, "EMA 21", color=color.new(color.green, 50))
plot(emaSlow, "EMA 55", color=color.new(color.red, 50))
Why this works.
  • Cascaded `if` ensures only one colour wins per bar.
  • Squeeze detection defaults to orange only when neither bullish nor bearish conditions apply.
  • Using named colours keeps the palette consistent and easy to adjust.
  • Avoid calling barcolor() multiple times—later calls override earlier ones.
  • Keep transparency high when colouring bars on darker chart themes.
  • Combine bar colours with background or label cues for multi-layer storytelling.
  • Reset to na when you want bars to revert to default styling.

My colours flicker or look inconsistent—why?

Section titled “My colours flicker or look inconsistent—why?”

It usually means multiple colour conditions are firing on the same bar. Consolidate your logic into a single decision tree before calling barcolor().

No—barcolor() changes the entire candle. Use plotcandle() for per-part customisation if you need separate wick/body colours.

  • All bar-colour logic should resolve to exactly one colour (or na) per bar.
  • Reusable colour definitions make scripts cleaner and easier to tweak.
  • Think in terms of regimes: trend, volatility, or custom flag states—and map each to a colour.
  • Use barcolor() alongside other visual tools for a cohesive indicator experience.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.