bgcolor() - Pine Script Function
bgcolor()
Section titled “bgcolor()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”bgcolor(color, offset, editable, show_last, title, display, force_overlay) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
color | series color | Color applied to the background. Use na to skip. Required. |
offset | series int | Shift the coloring forward/backward by offset bars. Defaults to 0. |
editable | const bool | Allow the user to change the color via the Style settings. Defaults to true. |
show_last | series int | Apply the background only to the most recent n bars. Defaults to na (all bars). |
title | const string | Name shown in the Style panel. |
display | const string | Display mode (see display.* constants). |
force_overlay | const bool | Force the color into the price pane even when the script is not overlayed. Defaults to false. |
Remarks
Section titled “Remarks”- 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=truelets you color the main price chart without switchingoverlay=true.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=5indicator("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")