Skip to content

Bar color

Kind: module

Namespace: barColor

Change the bar color on the chart. Always draws on the main price chart (overlay is implicit). For background fill, use bgColor; for line or histogram series, use spline; for numeric outputs without drawing, use output.

A bar has exactly one color, so barColor is a single-slot output — calling it multiple times in the same onTick() does not create several layers. Instead:

  • Color: the last call in the current tick wins.
  • Options (title, offset, numberOfLast): captured on the very first call and ignored on any later call — they stay static for the lifetime of the indicator.
  • Across ticks, the color history still reflects one color per bar: a new point is appended when a new bar starts and overwritten on intra-bar updates.

This differs from bgColor, spline, and shape, which are positional — each call site registers its own layer.

barColor()

Kind: func

Outputs a bar color for the current bar for one layer.

Syntax

barColor(color) → void
barColor(color, {title, offset, numberOfLast}) → void

Arguments

NameTypeDescription
colorcolor?Color for the current bar. Pass null if this layer should not paint on this bar.

Options

Static (ignored after the first call):

NameTypeDefaultDescription
title?stringnullDisplay name in the legend
offset?int0Horizontal offset in bars (positive = right)
numberOfLast?int0How many of the last bars this layer applies to (host-defined semantics)

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 the init section
  • 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()) {
        barColor(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);

    barColor(regime ? bull : bear, {
        title: "Regime",
        offset: 0,
        numberOfLast: 100
    });
}