Volume Bars
Install and import
npm install fintech-algorithmsimport { constructBars } from "fintech-algorithms/market-data-engineering/bar-construction/volume-bars";Signature
constructBars(trades, config)Closes a bar once a target share volume has traded. Sampling by volume rather than by clock gives series with far more stable statistical properties than time bars.
Parameters
| Name | Type | Notes |
|---|---|---|
trades | Trade[] | The raw tape in chronological order. Each trade carries tradeId, timestamp, session, symbol, price, volume and currency. |
config | { targetVolume: number; closePartial?: boolean } | targetVolume is the cumulative share volume that closes a bar. A single trade larger than the target closes a bar on its own. closePartial decides whether a final short bar is emitted. |
Returns
Bar[] · length fewer
One bar per completed volume bucket.
Errors
- When targetVolume is not positive — throws
Complexity: time O(n),
space O(bars).
Worked example
executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.
Input
[
{
"tradeId": "T0001",
"timestamp": "2026-01-05T14:30:00.000Z",
"session": "2026-01-05",
"symbol": "SYNTH",
"price": 99.99,
"volume": 36,
"currency": "USD"
},
{
"tradeId": "T0002",
"timestamp": "2026-01-05T14:30:04.000Z",
"session": "2026-01-05",
"symbol": "SYNTH",
"price": 99.99,
"volume": 73,
"currency": "USD"
},
{
"tradeId": "T0003",
"timestamp": "2026-01-05T14:30:07.000Z",
"session": "2026-01-05",
"symbol": "SYNTH",
"price": 100,
"volume": 110,
"currency": "USD"
}
]Showing 3 of 240 elements.
{
"closePartial": true,
"targetVolume": 1300
}Call
constructBars(trades, config)Returns
array of 22 objects
[
{
"barIndex": 0,
"session": "2026-01-05",
"startTime": "2026-01-05T14:30:00.000Z",
"endTime": "2026-01-05T14:30:46.000Z",
"lastTradeTime": "2026-01-05T14:30:46.000Z",
"open": 99.99,
"high": 100.06,
"low": 99.98,
"close": 100.05,
"volume": 1374,
"dollarValue": 137422.17,
"tickCount": 13,
"firstTradeId": "T0001",
"lastTradeId": "T0013"
},
{
"barIndex": 1,
"session": "2026-01-05",
"startTime": "2026-01-05T14:30:54.000Z",
"endTime": "2026-01-05T14:31:43.000Z",
"lastTradeTime": "2026-01-05T14:31:43.000Z",
"open": 100.05,
"high": 100.12,
"low": 100.04,
"close": 100.12,
"volume": 1422,
"dollarValue": 142312.41,
"tickCount": 11,
"firstTradeId": "T0014",
"lastTradeId": "T0024"
},
{
"barIndex": 2,
"session": "2026-01-05",
"startTime": "2026-01-05T14:31:48.000Z",
"endTime": "2026-01-05T14:32:24.000Z",
"lastTradeTime": "2026-01-05T14:32:24.000Z",
"open": 100.11,
"high": 100.15,
"low": 100.1,
"close": 100.15,
"volume": 1358,
"dollarValue": 135978.28,
"tickCount": 9,
"firstTradeId": "T0025",
"lastTradeId": "T0033"
}
]Showing 3 of 22 elements.
Diagrams
Calculation flow
Causal construction flow - Volume Bars
flowchart TD
A["Receive next cleaned eligible trade"] --> B{"New session?"}
B -->|Yes| C["Apply partial-tail policy; reset to zero"]
B -->|No| D["Keep open bar"]
C --> E["Add the whole trade"]
D --> E
E --> F{"Cumulative shares >= target?"}
F -->|No| A
F -->|Yes| G["Emit OHLCV, close reason, and lineage"]
G --> H["Reset to zero; do not carry overshoot"]
H --> A
How it works
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References
- R01 - Advances in Financial Machine Learning, Chapter 1 — Marcos Lopez de Prado
- R02 - NYSE Daily TAQ Client Specification — New York Stock Exchange, an Intercontinental Exchange company
- R03 - Trade Reporting Frequently Asked Questions — Financial Industry Regulatory Authority
- R04 - MIDAS: Market Information Data Analytics System — U.S. Securities and Exchange Commission
- Evidence and data note