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) → 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
.
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)