Zopra/Charts
Guide
Getting startedAll chartsWhich chart to use?Domain specific
Base charts7
XY ChartTrendCandlestickHeatmapState TimelineFlame GraphBar Gauge
Derived charts73
Time SeriesHistogramStatus HistoryStatGaugeHoneyCombTracesNode GraphHeikin-AshiRenkoKagiPoint & FigureParetoIshikawaControl ChartBland-AltmanLollipopDumbbellSlopeBumpBack-to-BackWaterfallBulletFunnelMekkoForestBarcodeBeeswarmRidgelineHorizonSpectrogramAttention MapWaffleVennUpSetWarped WaffleWord CloudGanttSankeySankey MatrixTreemap ForceChord DiagramCircosArc DiagramAlluvialSankey FunnelDendrogramIcicleSunburstSpiderNightingale RoseParallel CoordinatesOscilloscopeEfficient FrontierPhase PortraitPoincaré MapBifurcationH-R DiagramRamachandranHarkerRoot LocusRadiation PatternQuiverHodographSmith ChartEye DiagramSequence LogoTernaryTernary ContourPiperStüveTephigramChoropleth

Charts/Base

Candlestick

<Candlestick /> · 12 examples

Open, high, low and close for each period, drawn as one candle, with volume underneath.

Overview

A line chart of a price throws away almost everything that happened in a day. It keeps one number, usually the close, and pretends the rest of the day was quiet. A candlestick keeps four: where the period opened, where it closed, and how far it strayed up and down on the way. That is a lot of story for a few pixels of width.

The form is usually traced to Japanese rice traders in the eighteenth century, with the merchant Munehisa Homma often named as the person who made it famous. The details of that tale are better told by people who have read the sources than by a documentation page, but the design has clearly survived for a reason. Western traders found it in the 1990s and have been drawing it on every screen since, usually in front of a green and red glow.

Do not let the finance flavour fool you. Anything that has a first, a last, a smallest and a largest value per bucket is a candlestick in waiting: the latency of each minute, the temperature of each day, the queue depth of each hour. A candle says where the bucket started, where it ended, and how wild it got in between. The only thing it will not do is predict next Tuesday, which is why the people who read tea leaves for a living still prefer it.

Props

API

<Candlestick> draws price bars: each period's open, high, low and close as a candle or an OHLC bar, with trading volume along the bottom. It is a port of Grafana's Candlestick panel; see Coming from Grafana.

use zopra_charts::*;
  • <Candlestick>
  • Fields
  • How candles are drawn
  • Enums
  • Coming from Grafana

Not sure which option fits? See the guide.


<Candlestick>

<Candlestick data={bars} x={|b: &Bar| b.time}
    open={|b: &Bar| b.open} high={|b: &Bar| b.high} low={|b: &Bar| b.low}
    close={|b: &Bar| b.close} volume={|b: &Bar| b.volume}>
    <TrendSeries name="SMA 20" y={|b: &Bar| b.sma} />
</Candlestick>
PropTypeDefault
dataVec<T>, Signal<Vec<T>>, Frame<N>, Signal<Frame<N>>, or your own Datasetrequired
xFn(&T) -> f64: the start of each period, in milliseconds since the epochfor a Frame: the x_field, or the first field
x_fieldfield name, for a Framethe first field
open, high, low, close, volumeFn(&T) -> f64for a Frame: found by name, see Fields
children<TrendSeries> overlays, such as moving averagesnone
modeCandleModeCandlesVolume
candle_styleCandleStyleCandles
color_strategyColorStrategyOpenClose
up, down, flatcoloursyour theme's good, bad and neutral colours
include_all_fieldsbool: draw a Frame's other fields as linesfalse
x_axisAxisConfiga time axis
y_axisAxisConfig for the price axisAxisConfig::new()
legendLegendlists the overlays
tooltipTooltipTooltip::multi(): open, high, low, close and volume
annotationsAnnotationsnone
orientation, timezones, keyboard, on_selectas on <TimeSeries>
class, key

Fields

With a Frame and no field accessors, fields are matched by name, ignoring case:

PartField names
openopen, o
highhigh, h, max
lowlow, l, min
closeclose, c
volumevolume, v

Missing parts are filled in as Grafana does:

  • No open: each candle opens at the previous close.
  • No close: each candle closes at the next open.
  • No high or low: the wick ends at the body.

include_all_fields draws every other field of the frame as a line on the price axis. In Candles mode an unused volume field becomes one of those lines; in Volume mode, high and low do.

How candles are drawn

  • Width. Bodies are 60% of the narrowest gap between periods. Wicks and OHLC ticks are 1 px wide when bodies are 12 px or narrower, otherwise 2 px.
  • Colour. With ColorStrategy::OpenClose, a candle is up when it closes above its open. With CloseClose, it is up when it closes above the previous close; rising candles are then drawn hollow, outlined in their colour. Equal values count as up, except the first candle under CloseClose, which is flat.
  • Volume. In CandlesVolume mode, volume bars sit in the bottom seventh of the chart on their own hidden scale (0 to 7 × the largest volume), coloured like their candle at half opacity. In Volume mode, they use the whole chart and the price axis.
  • Overlays. <TrendSeries> children are drawn over the candles on the price axis and listed in the legend; the price parts themselves are not.

Enums

EnumVariants
CandleModeCandles, CandlesVolume (default), Volume
CandleStyleCandles (default), OhlcBars
ColorStrategyOpenClose (default), CloseClose

Coming from Grafana

Grafana optionHere
Modemode
Candle stylecandle_style
Color strategycolor_strategy
Up color, Down color, Flat colorup, down, flat
Open, High, Low, Close, Volume fieldsopen, high, low, close, volume, or field names
Include all fieldsinclude_all_fields, or <TrendSeries> children
Legend, Tooltip, Annotationslegend, tooltip, annotations

Not available here: the volume field's own fill opacity (volume is drawn at 50%) and tick labels on the volume axis.

Run them

12 examples

Press Run to load an example into the live render at the top of the page. Open one to read its code.

How to read it

  1. 01Read the body first: the filled part runs from open to close, and its colour says whether it went up or down under the chosen strategy.
  2. 02Then the wicks. A long upper wick means the price went high and was pushed back. A long lower one means the opposite. Both are the range the line chart hid.
  3. 03Look at volume under the candle. A big move on small volume and a big move on large volume are different events.
  4. 04Hover a candle for open, high, low, close and volume.

When to use it, and when not to

Reach for it when

  • Each x holds a range, not a single value: a trading day, a minute of latency, a day's temperature.
  • You care about the spread within a period as well as where it ended.
  • You want to overlay a moving average, a band or a target on the same axes.
  • Volume or activity should be read alongside price, in one panel.

Skip it when

  • You have one value per point. Candlestick with only a close draws bodies that are just the change since the last candle, which is a fine way to disguise a line chart as a lot of rectangles. Use Time Series.
  • Your audience does not read candles. Explaining which end of the body is the open will take longer than reading the data.
  • Periods are very uneven in size, such as one tick and then a whole week, so the candles are not comparable.
  • You want to show that patterns predict the future. They do not, whatever the green glow suggests.

The math behind it

One bucket, four numbers

For each period ttt you supply an open oto_tot​, a high hth_tht​, a low ltl_tlt​ and a close ctc_tct​ (and optionally a volume vtv_tvt​). The candle is a body between oto_tot​ and ctc_tct​ and a thin wick from ltl_tlt​ to hth_tht​. Both are placed on a shared linear y scale, the same one used by Trend, so the axis is rounded outwards to nice numbers in the usual way.

Parts you do not supply are not an error. If there is no open, the candle opens at the previous close, which is what happens in a market that never closes. If you give only a close, the whole chart still draws: each candle spans previous close to close.

ot←ct−1when no open is giveno_t \leftarrow c_{t-1} \quad \text{when no open is given}ot​←ct−1​when no open is given

Colour: which way did it go

Colour is a function of direction. With the default ColorStrategy::OpenClose a candle is up when its close is at or above its open, and down otherwise. With ColorStrategy::CloseClose the comparison is against the previous close, and the candle is up when ct≥ct−1c_t \ge c_{t-1}ct​≥ct−1​. The two can disagree: a stock that opens far below yesterday's close and ends the day slightly higher is an up candle by one rule and a down candle by the other.

Equal values count as up, with one exception: the very first candle under CloseClose has no previous close and is drawn flat. Under CloseClose, rising candles are drawn hollow, outlined in their colour, which is the convention many trading tools use.

uptopen-close=[ ct≥ot ],uptclose-close=[ ct≥ct−1 ]\text{up}_t^{\text{open-close}} = [\,c_t \ge o_t\,], \qquad \text{up}_t^{\text{close-close}} = [\,c_t \ge c_{t-1}\,]uptopen-close​=[ct​≥ot​],uptclose-close​=[ct​≥ct−1​]

Volume and bar width

In the default CandlesVolume mode the plot is shared: candles use the price scale and volume bars use their own scale, drawn at half opacity so they stay behind the candles. The Candles and Volume modes give the whole height to one of them.

Moving averages and bands are ordinary <TrendSeries> children with the full Trend styling, drawn over the candles.

Real life usage

The five-minute outage that wasn't in the average

Ines was an SRE at a company that sold appointment reminders by text message. On the dashboard the checkout latency was a calm blue line sitting at about 180 milliseconds, and it had been that calm blue line for months, including during the incident the customer success team kept bringing up in meetings.

The incident was real. Customers had written in. Some had been charged twice. And the latency chart, which was an average per minute, showed a polite little bump to 260 ms and then went back to being calm, like a man who has been told to relax.

An average is a very efficient way to hide a minute in which 3 requests took 40 seconds and 9,000 took 180 milliseconds. Ines knew that. She needed a chart that would show the worst request in every minute along with the first and last, so that anyone looking could see the length of the tail without opening a query console.

She rebuilt the panel with <Candlestick> from zopra-charts: for each minute, the first latency as open, the last as close, the minimum as low and the maximum as high, with request count as volume. A moving average went on top as a <TrendSeries>, and she picked blue and orange for up and down so nobody would need a colour chart.

During the incident window the candles stopped looking like candles and started looking like flagpoles. Wicks ran up to 41 seconds, at the same minutes the double-charge tickets arrived, and the volume bars dipped just before, which is what a retry storm looks like from the outside.

Armed with that picture she got the retry policy changed in a single afternoon. The next month there were two wicks above 5 seconds instead of forty-three, and the customer success team found something else to bring up in meetings.

The calm blue line is still on the dashboard, in the corner, labelled average. It has been asked to reflect on that.

Pitfalls

  • Colour rules differ between tools. Open-close and close-close colouring give different candles for the same data. Pick one and say which.
  • Gaps in time. Weekends and holidays leave holes unless your x values are sequence numbers. Decide whether a gap is information.
  • Red and green only. Pick up and down colours that read for colour-blind viewers, such as blue and orange.
  • Treating a single close as OHLC. With close only, each candle opens at the previous close, which draws bodies but adds no information.

From around the web

Watch and read

YouTubeHow To Read Candlestick Charts FAST (Beginner's Guide)WysetradeBeginner guide to reading open, high, low and close from candles.

Keep going

Related charts

Heikin-AshiCandles smoothed with an averaging rule, for when the raw ones are jumpy.RenkoWhen time is noise and you only want price moves above a fixed size.Time SeriesWhen you have one value per timestamp and no range.TrendFor moving averages and bands, which are drawn with the same series options.
PreviousTrendNextHeatmap

On this page

  • Live
  • Overview
  • API
  • Examples
  • How to read it
  • When to use it
  • The math
  • Story
  • Pitfalls
  • Watch and read
  • Related