Backgrounds
The bgcolor()
function is used to change the background color of the script. When the script runs in overlay = true
mode, it colors the chart’s background.
The function’s signature is as follows:
bgcolor(color, offset, editable, show_last, title, force_overlay) → void
Arguments
-
color
(series color): Defines the color of the filled background. This can be a constant like'red'
or'#ff001a'
, or a complex expression such as'close >= open ? color.green : color.red'
. This is a required argument. -
offset
(series int): Determines the number of bars to shift the color series to the left or right. The default value is0
. -
editable
(const bool): Specifies whether the bgcolor style can be edited in the Format dialog. The default value istrue
. -
show_last
(input int): Sets the number of bars (counted from the last bar going back in time) to fill on the chart. -
title
(const string): Provides the title for the bgcolor. This is an optional argument. -
display
(input plot_simple_display): Controls where the bgcolor appears on the chart. Acceptable values aredisplay.none
anddisplay.all
. The default isdisplay.all
. -
force_overlay
(const bool): If set totrue
, forces the plotted results to appear on the main chart pane, even if the script is placed in a separate pane. This is an optional argument, and the default value isfalse
.
Example:
indicator("bgcolor example", overlay=true)
bgcolor(close < open ? color.new(color.red,70) : color.new(color.green, 70))
In our next example, we generate a gradient for the background of a CCI line:
indicator("CCI Background")
bullColor = input.color(color.lime, "🠅", inline = "1")
bearColor = input.color(color.yellow, "🠇", inline = "1")
// Calculate CCI.
myCCI = talib.cci(hlc3, 20)
// Get relative position of CCI in last 100 bars, on a 0-100% scale.
myCCIPosition = talib.percentrank(myCCI, 100)
// Generate a bull gradient when position is 50-100%, bear gradient when position is 0-50%.
backgroundColor = myCCIPosition >= 50 ?color.new(bullColor, 0.25):color.new(bearColor, 0.25)
// Wider white line background.
plot(myCCI, "CCI", color.white, 3)
// Think black line.
plot(myCCI, "CCI", color.black, 1)
// Zero level.
hline(0)
// Gradient background.
bgcolor(backgroundColor)