TradingCharts

Backgrounds

bgcolor() tints the background of each bar. It is the right tool for visualising sessions, filter windows, or regime labels — any colour-only, per-bar state.

bgcolor(color, offset?, editable?, title?, display?, force_overlay?)

bgcolor(close > open ? color.new(color.lime, 92) : color.new(color.red, 92))
  • color is a series value; return na on bars where no tint should apply.
  • offset shifts the tint n bars left/right (defaults to 0).
  • force_overlay = true paints the tint on the price pane even when the script is an oscillator.

Common patterns

Highlight a session

isPremarket = session.ispremarket
bgcolor(isPremarket ? color.new(color.orange, 92) : na, title="Premarket")

Highlight a time-range filter

useTimeFilter = input.bool(true, "Use time filter")
startTime     = input.time(timestamp("2020-01-01 00:00"), "Start")
endTime       = input.time(timestamp("2099-01-01 00:00"), "End")

inRange = time >= startTime and time <= endTime
bgcolor(useTimeFilter and not inRange ? color.new(color.red, 85) : na, title="Outside range")

Regime colouring

regime = switch
    close > ta.ema(close, 50) and close > ta.ema(close, 200) => "bull"
    close < ta.ema(close, 50) and close < ta.ema(close, 200) => "bear"
    => "flat"

bgcolor(switch regime
    "bull" => color.new(color.lime, 92)
    "bear" => color.new(color.red,  92)
    => color.new(color.gray, 95))

Z-order

  • bgcolor() renders behind the price series and the plots.
  • Multiple bgcolor() calls alpha-blend in declaration order (last call on top of earlier ones).

When to prefer box.new

  • bgcolor paints every selected bar edge-to-edge in the pane and takes constant time.
  • box.new is better when you need a region with a border, text label, or that extends outside the chart's current bar range (zones from historical pivots).

See also