Skip to main content
Call barcolor at the top level and put the condition in its color argument. The color is a series value, so it may differ on every bar. barcolor draws nothing of its own; it recolors the chart’s bars.

Color only some bars

Pass na as the color for the bars that should keep their own. bgcolor shades the pane background and takes the condition the same way. color.new(color, alpha) sets the opacity from 0 (invisible) to 1 (solid), so use a low value for a light shade:

Color by a value

color.from_gradient blends two colors by where a value lies between two bounds:
bgcolor also takes force_overlay=true, which shades the price pane even when the script’s plots sit in a pane of their own. barcolor has no such argument.

The mistake to avoid

Putting the call inside an if to make it conditional. These calls must be at the top level, and the condition belongs in the argument:
The compiler says: barcolor() must be called in the global scope. The editor’s check reports this. A related slip is giving color.new a percentage such as 50: it compiles with the warning opacity 50 is outside 0–1; 1 is solid. See Bar coloring and background and Color and transparency.