Skip to content

Candlestick · OHLC ​

Financial charts of open / high / low / close prices. renderCandlestickChart draws candles: a body from open to close and a wick from low to high. renderOhlcChart draws OHLC bars: a high–low stem with an open tick on the left and a close tick on the right. Both share the time axis, volume pane, aggregation and navigation.

Live example

Basic usage ​

ts
import { Charturbo } from 'charturbo';

const canvas = document.querySelector('canvas')!;
const sdk = await Charturbo.create(canvas, { timeZone: 'UTC' });

sdk.renderCandlestickChart(
  { t, open, high, low, close, volume }, // t: Date[] or epoch ms
  { title: 'ACME', hollowUp: true, yFit: 'visible', axes: { y: { format: '$,.2f' } } }
);
sdk.setXRange(t[t.length - 120], t[t.length - 1]); // start on the last 120 candles
sdk.enableNodeInteractions(canvas, { autoRender: true });
sdk.render();
ts
renderCandlestickChart(data: CandlestickChartData, options?: CandlestickChartOptions): { candleCount: number }
renderOhlcChart(data: OhlcChartData, options?: OhlcChartOptions): { barCount: number }

Data point id = candle index.

Data format ​

CandlestickChartData (OhlcChartData is the same type) has one entry per candle in every array:

FieldTypeDescription
tFloat64Array | number[] | Date[]Optional x of each candle: epoch ms or Date for a time axis, or plain numbers with axes.x.type: 'numeric'. Finite, ascending. Omit to place candles by index.
openFloat32Array | number[]Open prices.
highFloat32Array | number[]High prices.
lowFloat32Array | number[]Low prices.
closeFloat32Array | number[]Close prices.
volumeFloat32Array | number[]Optional volume, drawn as bars in a bottom pane with its own scale.

A candle with a non-finite open, high, low or close is a gap.

Options ​

CandlestickChartOptions:

OptionTypeDefaultDescription
titlestring—Chart title.
descriptionstring—Accessible name of the canvas.
upColorColorInputtheme palette[0] (blue)Rising candle color (close ≥ open).
downColorColorInputtheme palette[1] (orange)Falling candle color.
hollowUpbooleantrueRising candles as outlines, a shape cue besides color. Narrow candles are filled.
wickWidthnumber1Wick / outline width in CSS px.
bodyWidthnumber0.7Body width as a fraction of the candle spacing.
cornerRadiusnumber0Rounded body corners in CSS px, 0..1.
showVolumebooleantrueVolume pane when volume is given.
volumeOpacitynumber0.45Volume bar opacity.
volumeHeightnumber0.2Volume pane height as a fraction of the plot.
yFit'visible' | 'all''visible''visible': the price axis fits the visible candles on zoom / pan. 'all': every candle.
axes{ x?: LineXAxisOptions; y?: ChartAxisOptions }—x.type defaults to 'time' when t is given. See Data & Axes.
localestringSDK localeLocale of default labels and tooltip values.
timeZoneCharturboTimeZoneSDK zoneTime zone of the time axis and its labels.
hoverChartHoverStyle—Crosshair style (vertical at the candle, horizontal at the pointer).
showBackdropGridbooleantrueHorizontal gridlines and baseline.
gridChartGridStyle—Grid style.
paddingChartPaddingOptionsautoPlot margins in CSS px.
animationChartAnimationInputSDK settingEnter / update animation.
widthnumber168World width of the plot.
heightnumber86World height of the plot.

OhlcChartOptions takes the same options except hollowUp, wickWidth, bodyWidth and cornerRadius, and adds:

OptionTypeDefaultDescription
lineWidthnumber1Stem and tick stroke width in CSS px (0.25–8).
tickWidthnumber0.7Combined left / right tick span as a fraction of the bar spacing (0.05–1).
ts
const { barCount } = sdk.renderOhlcChart({ t, open, high, low, close, volume }, {
  lineWidth: 1.5,
  tickWidth: 0.8,
  showVolume: true,
  yFit: 'visible',
  timeZone: 'UTC',
  axes: { y: { format: '$,.2f' } },
});

Colors ​

The default colors are blue for rising and orange for falling candles, not red and green alone, so they stay distinct under color-vision deficiency. Rising candles are hollow by default as a second cue. The defaults follow the theme when it changes.

Aggregation ​

When candles get narrower than 2 CSS px, the engine aggregates them into 3 px buckets: open = first, high = max, low = min, close = last, volume = sum. Extremes are preserved, and 1,000,000+ candles stay interactive. The tooltip then shows the bucket values and count.

Interactions ​

  • Wheel, drag and pinch zoom and pan the x axis; double-click resets. setXRange(Date, Date) zooms from code.
  • The crosshair is vertical at the candle and horizontal at the pointer.
  • Tooltip: ctx.chartType is 'candlestick' or 'ohlc'; ctx.ohlc = { open, high, low, close, volume, x, xEnd, count }. The tooltip field labels (Open, High, Low, Close, Volume) follow the chart locale; see Internationalization.
  • Brush selection kind is 'candlestick' or 'ohlc'; ids are candle indices.
  • Annotations use x = t and y = price. See Annotations.
  • Keyboard navigation, the data table, CSV and vector SVG export are supported.

See Interactions and Tooltips & Hover.

Updating data ​

Call renderCandlestickChart or renderOhlcChart again with the new arrays. The OHLC mode is kept across theme changes.

Frameworks ​

tsx
<CharturboChart type="candlestick" data={{ t, open, high, low, close }} />
<CharturboChart type="ohlc" data={{ t, open, high, low, close, volume }} options={{ tickWidth: 0.8 }} />

See also ​

MIT License