Skip to content

Colouring the background of price bands and price bars in TradingView

Colouring the background of price bands and price bars in TradingView

Section titled “Colouring the background of price bands and price bars in TradingView”

TL;DR
Blend price zones and bar states with bgcolor() and fill() to make trends instantly recognisable.

DifficultyBeginner
Time to implement10-15 min
CategoryVisual Effects
//@version=5
indicator("Background on trend", overlay=true)
bgColor = close > close[1] ? color.new(color.green, 80) : color.new(color.red, 80)
bgcolor(bgColor)
Tip. Keep transparency high (70–90) so price bars stay readable through the background colour.

Colour cues let the eye pick out trends faster than raw numbers. bgcolor() and fill() allow you to shade price zones, highlight breakouts, and keep the price series legible. Done right, background accents show whether price sits inside a balanced range or breaking into a new regime without cluttering the chart.

  • Build dynamic upper and lower bands around a baseline
  • Shade the band interior to reveal compression or expansion
  • Recolour the chart background when price leaves the band
  • Combine bar colours and transparency to keep price readable
CallPurpose
bgcolor(color)Paints the entire plotting pane per bar.
fill(plot1, plot2, color)Shades the area between two plotted series.
color.new(color, transparency)Produces translucent hues (0 opaque → 100 invisible).
plot(series)Draws the baseline and band edges used by fill().
barcolor(color)Optionally matches candle bodies to the current background story.
  1. Define a baseline with envelopes
    Start with a smoothing function, then add and subtract a deviation to form band edges.

    length = 20
    mult = 2.0
    basis = ta.sma(hl2, length)
    dev = mult * ta.stdev(hl2, length)
    upper = basis + dev
    lower = basis - dev
    plot(basis, "Basis", color=color.orange, linewidth=2)
    plotUpper = plot(upper, "Upper band", color=color.new(color.blue, 0))
    plotLower = plot(lower, "Lower band", color=color.new(color.blue, 0))
  2. Shade the interior of the band
    Use the plot references returned above and fill the space with a muted colour.

    fill(plotUpper, plotLower, color.new(color.blue, 88))
  3. Signal breakouts with background and bar colour
    When price crosses outside the band, change the background (and optionally the bars) to draw attention.

    isAbove = close > upper
    isBelow = close < lower
    bgcolor(
    isAbove ? color.new(color.green, 80) :
    isBelow ? color.new(color.red, 80) : na
    )
    barcolor(
    isAbove ? color.new(color.green, 20) :
    isBelow ? color.new(color.red, 20) : na
    )
//@version=5
indicator("Dynamic bands + breakout background", overlay=true)
length = input.int(34, "Basis length")
mult = input.float(2.2, "Deviation multiplier", step=0.1)
bgStrength = input.int(75, "Background transparency", minval=0, maxval=100)
matchBars = input.bool(true, "Recolour bars outside band")
basis = ta.ema(hl2, length)
dev = mult * ta.stdev(hl2, length)
upper = basis + dev
lower = basis - dev
plotUpper = plot(upper, "Upper", color=color.new(color.blue, 0))
plotLower = plot(lower, "Lower", color=color.new(color.blue, 0))
plot(basis, "Basis", color=color.orange, linewidth=2)
fill(plotUpper, plotLower, color.new(color.blue, 88))
isAbove = close > upper
isBelow = close < lower
bgcolor(
isAbove ? color.new(color.green, bgStrength) :
isBelow ? color.new(color.red, bgStrength) : na
)
if matchBars
barcolor(
isAbove ? color.new(color.green, 25) :
isBelow ? color.new(color.red, 25) : na
)
Why this works.
  • The shaded band conveys volatility range without masking candles.
  • Breakout colours only appear when price escapes the band, keeping emphasis rare.
  • Optional bar tinting adds redundancy while retaining the underlying candle structure.
  • Persist plot handles in variables; fill() only accepts references returned by plot() or hline().
  • Combine bgcolor() with barcolor() sparingly—too many colours dilute meaning.
  • If multiple indicators use bgcolor(), order matters. Draw the most important background last.
  • Use overlay=true so backgrounds and fills paint on top of price instead of in a detached pane.

Ensure both band edges are plotted and you pass those plot references to fill(). Calling fill(upper, lower, …) on raw series does nothing.

Transparency is inverted: 0 is solid and 100 is invisible. Aim for 70–90, and consider resetting barcolor(na) when price returns inside the band.

  • fill() and bgcolor() together show where price sits relative to volatility envelopes.
  • color.new() keeps overlays translucent so the underlying price remains readable.
  • Signal only the events that matter (band breakouts, regime shifts) to preserve colour meaning.
  • Layer backgrounds carefully when multiple scripts share the same chart.

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