ScriptingConceptsBar plotting

Bar Plotting

Introduction

The plotcandle() built-in function is used to plot candles, while plotbar() is used to plot conventional bars.

Both functions require four arguments corresponding to the OHLC prices (open, high, low, close) of the bars to be plotted. If any of these arguments is na, no bar is plotted.

Plotting Candles with plotcandle()

The signature of plotcandle() is as follows:

plotcandle(open, high, low, close, title, color, wickcolor, editable, show_last, bordercolor, display) → void

This plots simple candles, all in blue, using the habitual OHLC values, in a separate pane:

indicator("Single-color candles");
plotcandle(open, high, low, close);

image

To color them green or red, we can use the following code:

indicator("Example 2");
paletteColor = close >= open ? color.lime : color.red;
plotcandle(open, high, low, close, color = paletteColor);

image

Note:

  • The color parameter accepts “series color” arguments, allowing constant values like color.red, color.lime, "#FF9090", as well as expressions that calculate colors at runtime, such as the paletteColor variable in this example.
  • You can create bars or candles using values other than the actual OHLC values. For instance, you can calculate and plot smoothed candles using the following code, which also colors wicks based on the position of the close relative to the smoothed close (c) of the indicator:
indicator("Smoothed candles", overlay = true)
lenInput = input.int(9)
smooth(source, length) =>
    talib.sma(source, length)
o = smooth(open, lenInput)
h = smooth(high, lenInput)
l = smooth(low, lenInput)
c = smooth(close, lenInput)
ourWickColor = close > c ? color.green : color.red
plotcandle(o, h, l, c, wickcolor = ourWickColor)

image

Plotting Bars with plotbar()

The signature of plotbar() is as follows:

plotbar(open, high, low, close, title, color, editable, show_last, display, force_overlay) → void

Note:

  • The plotbar() function does not have parameters for bordercolor or wickcolor, as conventional bars do not have borders or wicks.

This plots conventional bars using the same coloring logic as in the second example of the previous section:

indicator("Dual-color bars");
paletteColor = close >= open ? color.lime : color.red;
plotbar(open, high, low, close, (color = paletteColor));

image