Time-frame Aggregation

ApexStock.aggregateOHLC() rolls a fine-grained OHLC series up into a coarser time frame, for example 1-minute bars into 1-hour or daily bars. It is a pure static helper: it takes a series and an interval and returns a new array, which you then pass to new ApexStock(...) or to update({ series }) to re-render at the chosen interval.

Usage

// Roll a 1-minute series up to hourly bars
const hourly = ApexStock.aggregateOHLC(oneMinuteSeries, '1h')

const apexStock = new ApexStock(document.querySelector('#chart'), {
  chart: { height: 500 },
  series: [{ name: 'ACME', data: hourly }],
})
apexStock.render()

Because it is a static method, call it on the ApexStock class, not on an instance.

Supported intervals

The accepted interval strings are exposed as ApexStock.INTERVALS:

const intervals = ApexStock.INTERVALS
// e.g. "1m", "5m", "15m", "1h", "4h", "1d", "1w", "1M"

Use this list to build a time-frame selector rather than hard-coding strings.

Switching time frames at runtime

Aggregate from your finest-resolution buffer, then push the result with update():

function setTimeframe(interval) {
  const rolled = ApexStock.aggregateOHLC(oneMinuteSeries, interval)
  apexStock.update({ series: [{ name: 'ACME', data: rolled }] })
}

setTimeframe('4h')

Keep the raw 1-minute series as your source of truth and re-aggregate on demand, so switching between time frames is lossless.

Building bars from a raw tick feed

Two patterns cover turning a raw trade/tick feed into candles you can stream:

1. Append completed bars

Roll ticks into bars (with aggregateOHLC or your own logic), then append each finished bar:

// Roll a 1-minute series up to 5-minute bars, then stream the closed ones.
const fiveMin = ApexStock.aggregateOHLC(oneMinSeries, '5m')
apexStock.appendData(fiveMin[fiveMin.length - 1])

2. Keep a forming candle live

Fold each incoming trade into the in-progress bar and use appendData(bar, { updateLast: true }). See the full recipe in Real-time Streaming.

Both keep indicators exact, since a forming bar recomputes from the last committed state each tick.