ScriptingPutting results on the chartBar colouring and background

Bar colouring and background

Four functions colour the chart itself rather than adding a line to it. Two of them recolour what is already there. Two of them draw candles or bars from series you supply.

All four are chart-output functions, so all four must be called at the top level of the script. A condition goes in the colour argument, not around the call.

Recolouring the bars

barcolor(series color color, const string title, input int offset) → void

barcolor colours the price bars. It draws no value of its own: it changes the colour of bars that are already on the chart.

ParameterTypeRequiredNotes
colorseries colornoSeries-form, so it may differ on every bar.
titleconst stringnoConst-form, so it cannot be built from series data.
offsetinput intnoInput-form, so it cannot change from bar to bar.

Shading the background

bgcolor(series color color, const string title, input int offset, const bool force_overlay) → void

bgcolor colours the background. Like barcolor it draws no value of its own.

ParameterTypeRequiredNotes
colorseries colornoSeries-form, so it may differ on every bar.
titleconst stringnoConst-form, so it cannot be built from series data.
offsetinput intnoInput-form, so it cannot change from bar to bar.
force_overlayconst boolnoSets this drawing’s overlay, separately from the script-wide setting.

bgcolor takes a force_overlay argument and barcolor does not.

indicator("Bar and background colour")
plot(close, title = "Close")
barcolor(color.orange, title = "Bars")
bgcolor(color.new(color.blue, 0.1), title = "Background")

To shade only some bars, vary the colour rather than the call. The colour parameter is series-form, so it can differ from bar to bar; the call cannot be moved inside a branch.

Drawing your own candles

plotcandle(series float open, series float high, series float low, series float close, const string title, series color color, series color wickcolor, series color bordercolor, const bool force_overlay) → void

plotcandle needs four series: an open, a high, a low and a close. All four are required, and they are the first four arguments in that order.

ParameterTypeRequiredNotes
openseries floatyes
highseries floatyes
lowseries floatyes
closeseries floatyes
titleconst stringnoConst-form, so it cannot be built from series data.
colorseries colornoThe body.
wickcolorseries colornoThe wick.
bordercolorseries colornoThe border.
force_overlayconst boolnoSets this drawing’s overlay, separately from the script-wide setting.

The body, the wick and the border take separate colours, and each of the three is series-form, so each can change from bar to bar.

indicator("Recoloured candles")
plotcandle(open, high, low, close, title = "Candles", color = color.new(color.blue, 0.8), wickcolor = color.gray, bordercolor = color.blue)

Drawing your own bars

plotbar(series float open, series float high, series float low, series float close, const string title, series color color, const bool force_overlay) → void

plotbar takes the same four required series and one colour. There is no separate wick or border colour.

ParameterTypeRequiredNotes
openseries floatyes
highseries floatyes
lowseries floatyes
closeseries floatyes
titleconst stringnoConst-form, so it cannot be built from series data.
colorseries colornoThe whole bar.
force_overlayconst boolnoSets this drawing’s overlay, separately from the script-wide setting.

Neither plotcandle nor plotbar has an offset parameter at all. Where an offset does exist on an output function it is input-form, so it never changes from bar to bar.

Reach for plotcandle unless you want a bar rather than a candle.

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