Skip to content

bgcolor() - Pine Script Function

bgcolor() fills the chart background (price pane or indicator pane) with a color on a bar-by-bar basis. Use it to highlight sessions, regimes, or signal zones without drawing explicit shapes. Passing na leaves the background unchanged for that bar.

bgcolor(color, offset, editable, show_last, title, display, force_overlay) → void
NameTypeDescription
colorseries colorColor applied to the background. Use na to skip. Required.
offsetseries intShift the coloring forward/backward by offset bars. Defaults to 0.
editableconst boolAllow the user to change the color via the Style settings. Defaults to true.
show_lastseries intApply the background only to the most recent n bars. Defaults to na (all bars).
titleconst stringName shown in the Style panel.
displayconst stringDisplay mode (see display.* constants).
force_overlayconst boolForce the color into the price pane even when the script is not overlayed. Defaults to false.
  • Colors stack: the last bgcolor() call on a bar sits on top of earlier ones from the same script.
  • Use color.new(color, transp) to control transparency—lower values are more opaque.
  • On multi-pane indicators, force_overlay=true lets you color the main price chart without switching overlay=true.
//@version=5
indicator("Session highlighter", overlay=true)
sessStart = input.session("0930-1600", "RTH session (exchange time)")
sessionActive = not na(time(timeframe.period, sessStart))
bgcolor(sessionActive ? color.new(color.teal, 85) : color.new(color.gray, 95))
plot(close, "Close")