Appearance
Background color
Kind: module
Namespace: bgColor
Paints a per-bar background color on the chart. For line or histogram series, use spline; for numeric outputs without drawing, use output.
Each bgColor layer maintains one color per bar. A new color is added when a new bar starts and replaced when the current bar updates.
bgColor()
Kind: func
Outputs a background color for the current bar for one layer.
Syntax
bgColor(color) → void
bgColor(color, {title, offset, numberOfLast, overlay}) → voidArguments
| Name | Type | Description |
|---|---|---|
| color | color? | Color for the current bar. Pass null if this layer should not paint on this bar. |
Options
Static (ignored after the first call):
| Name | Type | Default | Description |
|---|---|---|---|
| title? | string | null | Display name in the legend |
| offset? | int | 0 | Horizontal offset in bars (positive = right) |
| numberOfLast? | int | 0 | How many of the last bars this layer applies to (host-defined semantics) |
| overlay? | boolean | true | If true, draws on the main price chart instead of a separate pane (same idea as spline) |
The color is always the first argument (not an option), so it can change on every tick.
Returns
void
Caveats
- Must be called inside
onTick(), not in theinitsection - Must be at the top level of
onTick(), not inside conditionals or loops. This ensures the call count and order remain constant across ticks
Pitfall
javascript
function onTick() {
if (bar.isConfirmed()) {
bgColor(color.RED); // ❌ inside conditional
}
}Example
javascript
const bull = input.color("Bull", color.GREEN);
const bear = input.color("Bear", color.RED);
function onTick() {
const regime = close > ta.sma(close, 20);
bgColor(regime ? bull : bear, {
title: "Regime",
offset: 0,
numberOfLast: 100,
overlay: true
});
}