Three.js 3D Stock Chart (Candlesticks, Volume & Moving Averages)

A live financial chart in Three.js, with instanced candlesticks, volume bars and 20/50-day moving averages for simulated tickers that stream new trading days, plus a comparison view of every ticker's return as stacked 3D ribbons.

Key topics: candlestick chart, stock market visualization, three.js finance, moving average, ohlc chart, financial data 3d

What this code does

- Candlesticks: the body spans open→close (green up, red down), and a thin wick spans the day's low→high, each an InstancedMesh with one draw call.
- Volume: bars behind the candles show daily volume, which rises with large price moves.
- Moving Averages: running-sum 20- and 50-day means smooth noise and show trend crossovers.
- Simulated Prices: geometric Brownian motion with occasional volatility regimes; tickers are fictional.
- Compare View: each ticker becomes a ribbon of % return since the first visible day.
- Controls: ticker, view, moving averages, live feed and days per second; hover for a day's OHLCV.

Continue Learning

JavaScript Implementation

ES6+
// Candlestick chart with instancing: one box per day for the body, one thin box for the wick
const bodies = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1), new THREE.MeshStandardMaterial(), days)
bars.forEach((b, d) => {
  const y0 = yOf(Math.min(b.open, b.close)), y1 = yOf(Math.max(b.open, b.close))
  bodies.setMatrixAt(d, new THREE.Matrix4().makeScale(0.1, Math.max(y1 - y0, 0.03), 0.1).setPosition(xOf(d), (y0 + y1) / 2, 0))
  bodies.setColorAt(d, new THREE.Color(b.close >= b.open ? 0x22c55e : 0xef4444))
})
// Simulated prices: geometric Brownian motion, p *= exp((μ − σ²/2)Δt + σ√Δt·N(0,1))