bgcolor(color, offset?, editable?, title?, display?, force_overlay?)
bgcolor(close > open ? color.new(color.lime, 92) : color.new(color.red, 92))
coloris a series value; returnnaon bars where no tint should apply.offsetshifts the tintnbars left/right (defaults to 0).force_overlay = truepaints 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
bgcolorpaints every selected bar edge-to-edge in the pane and takes constant time.box.newis 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
- Colors
- Bar coloring — recolour the candles themselves
- Sessions