Skip to content

Documentation / @finchart/core / index / barAggregator

Function: barAggregator() ​

barAggregator(options): object

Defined in: packages/core/src/data/aggregate.ts:49

Folds a stream of trades into bars.

fold is the primitive and reduce is a wrapper over it. An imperative consumer holds the bar in progress and pushes: feed.push(agg.fold(current, trade)) — no array is copied per trade. A consumer holding state instead writes setBars(prev => agg.reduce(prev, trade)).

What it does not do is half the contract:

  • One trade makes at most one bar. A reconnection that skipped a stretch is handle.append's business — if one call made two bars, the updateLast that follows would drop the one in the middle.
  • A trade already past comes back as the same object. "I did not eat this" is said with identity, so a consumer writes if (next === prev) return; rather than paying a copy per rejected tick.
  • The close is the last to arrive, not the last to have happened. Given one trade and the bar so far, a pure reducer never learns when the trade that set the close occurred. Ordering inside a bar belongs to the feed.
  • Aggregate first, conflate after. Conflating raw trades merges them last-wins and throws away that frame's high and low.
  • It is a door, because folding erases what it is handed. A price that is not a number loses every comparison it is in, so it neither raises the high nor lowers the low and only lands on the close — where the next trade overwrites it. Its volume is already in the sum by then, so what reaches the data door is a bar that adds up wrong and looks perfectly well formed. Nothing downstream can refuse what it never sees, so a trade's own numbers are checked here.

It seeds from an empty array rather than refusing one — a consumer with no history would otherwise never start. The bar that opens that way has the session's first trade for an open, not the session's opening price, and its x is not a cursor any server has heard of.

Parameters ​

options ​

barStart ​

BarStart

Returns ​

fold() ​

fold(bar, trade): OHLC

Folds one trade into the bar in progress. No array involved.

Parameters ​

bar ​

OHLC | null

trade ​

Trade

Returns ​

OHLC

reduce() ​

reduce(bars, trade): OHLC[]

That fold over an array — the front keeps its object identity.

Parameters ​

bars ​

OHLC[]

trade ​

Trade

Returns ​

OHLC[]