ScriptingHow toHow to colour bars by a condition

How to colour bars by a condition

Call barcolor at the top level and put the condition in the colour argument. The colour parameter is series-form, so it may differ on every bar.

barcolor(series color color, const string title, input int offset) → void
indicator("Up and down bars", "updown", true)
barcolor(close > open ? color.green : color.red, title = "Direction")

barcolor draws nothing of its own. It recolours bars that are already on the chart.

If only some bars should carry your colour

Give the rest the same colour at an opacity of 0. Opacity runs from 0 to 1, where 1 is solid and 0 is invisible.

indicator("Wide bars only", "wide", true)
barcolor(high - low > close * 0.02 ? color.orange : color.new(color.orange, 0.0), title = "Wide range")

If you want the pane shaded rather than the bars

bgcolor colours the background and takes the condition the same way.

bgcolor(series color color, const string title, input int offset, const bool force_overlay) → void
indicator("Shaded down bars", "shade", true)
bgcolor(close < open ? color.new(color.red, 0.1) : color.new(color.red, 0.0), title = "Down bars")

Set the opacity with color.new for a background. A low value shades; 1 is solid.

The failure to watch for

Wrapping the call in a branch is rejected with an error saying the call must be made in the global scope. There is no conditional form of the call: the condition always goes in the argument.

See Bar colouring and background.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade