Skip to main content

Candlestick Chart

A candlestick chart is essentially a sequence of box-plot-like bars placed side by side. It is commonly used to visualize the price movement of financial instruments such as forex, stocks, and bonds.

  • Good for: open-high-low-close price movement over time, stock and forex trading sessions, daily or weekly trading ranges.
  • Not great for: a single value per period (use a line or bar chart), non-time-series data, or categories without high and low bounds.
reporting-custom-chart/candlestick

Syntax

Use the following AML definition to add the Candlestick Chart to your custom chart library.

Legacy syntax
CustomChart {
fields {
field date {
type: "dimension"
label: "Date field"
}
field low {
type: "dimension"
label: "Low price"
}
field high {
type: "dimension"
label: "High price"
}
field open {
type: "dimension"
label: "Open price"
}
field close {
type: "dimension"
label: "Close price"
}
}

options {
option tooltip {
type: 'toggle'
label: 'Show tooltip'
default_value: true
}
option green_candle {
type: 'color-picker'
label: 'Green candlestick'
default_value: 'green'
}
option red_candle {
type: 'color-picker'
label: 'Red candlestick'
default_value: 'red'
}
}

template: @vgl
{
"data": {
"values": @{values}
},
"layer": [
{
"mark": "rule",
"encoding": {
"y": {"field": @{fields.low.name}},
"y2": {"field": @{fields.high.name}}
}
},
{
"mark": {
"type": "bar",
"tooltip": @{options.tooltip.value}
},
"encoding": {
"y": {"field": @{fields.open.name}},
"y2": {"field": @{fields.close.name}}
}
}
],
"encoding": {
"x": {
"axis": {
"format": "%m/%d",
"labelAngle": -45
},
"type": "temporal",
"field": @{fields.date.name},
},
"y": {
"axis": {
"title": "Price"
},
"type": "quantitative",
"scale": {
"zero": false
}
},
"color": {
"condition": {
"test": "datum.@{fields.open.name} < datum.@{fields.close.name}",
"value": @{options.green_candle.value}
},
"value": @{options.red_candle.value}
}
}
};;
}

Required fields

A Candlestick Chart expects exactly five fields. Each row of input is one period (one candle) with its open, high, low, and close values.

FieldLabelTypeRole
dateDatedimensionTime period for each candle; sets the x position. Sorted ascending (apply_order: 1).
lowLowmeasureLowest price; bottom of the wick. Sorted ascending (apply_order: 2).
highHighmeasureHighest price; top of the wick. Sorted ascending (apply_order: 3).
openOpenmeasureOpening price; one end of the candle body. Sorted ascending (apply_order: 4).
closeClosemeasureClosing price; the other end of the candle body. Sorted ascending (apply_order: 5).

Data requirements: Pre-aggregate to one row per date; the template does not combine duplicate periods. Each row needs all four price values, and the template colors a candle with the green option when open is less than close (otherwise the red option).

Sample data:

datelowhighopenclose
2024-01-02184.30188.44187.15185.64
2024-01-03183.89185.88184.22184.25
2024-01-04181.59183.09182.15181.91
2024-01-05180.17182.76181.99181.18
2024-01-08181.50185.60182.09185.56

Options

Set these options to adjust the chart without editing the Vega template. The CustomChartDef block above declares each option's type and allowed values.

OptionDefaultEffect
tooltiptrueShows a tooltip on hover over each candle body.
green_candlegreenFill color for up periods, where close is higher than open.
red_candleredFill color for down periods, where close is at or below open.

Known limitations

  • Every row needs all four price values. Each candle reads open, high, low, and close. Rows missing any of these render incompletely.

  • One row per period. The template does not aggregate, so duplicate dates draw overlapping candles. Pre-aggregate to a single open-high-low-close row per period.

  • The y-axis does not start at zero. The scale is set to fit the price range (zero: false), which is right for price data but means bar lengths are not proportional to absolute value.


Open Markdown
Let us know what you think about this document :)