Tick-Run Bars
Install and import
npm install fintech-algorithmsimport { constructBars } from "fintech-algorithms/market-data-engineering/bar-construction/tick-run-bars";Signature
constructBars(trades, config)Closes a bar when a run of same-signed trades exceeds what the recent buy probability makes plausible. Where imbalance bars react to net one-sidedness, run bars react to *persistence*.
Parameters
| Name | Type | Notes |
|---|---|---|
trades | Trade[] | The raw tape in chronological order. Each trade carries tradeId, timestamp, session, symbol, price, volume and currency. |
config | { initialTickSign: number; initialExpectedTicks: number; initialBuyProbability: number; alphaTicks: number; alphaBuyProbability: number; thresholdFloorTicks: number; thresholdMultiplier: number; closePartial?: boolean } | initialBuyProbability seeds the estimate of how often trades arrive buyer-initiated, updated by alphaBuyProbability. The threshold is bounded below by thresholdFloorTicks. |
Returns
Bar[] · length fewer
One bar per run event.
Errors
- When any alpha or probability falls outside 0…1 — 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,
"initialTickSign": 1,
"initialExpectedTicks": 20,
"initialBuyProbability": 0.6,
"alphaTicks": 0.2,
"alphaBuyProbability": 0.2,
"thresholdFloorTicks": 4,
"thresholdMultiplier": 1
}Call
constructBars(trades, config)Returns
array of 14 objects
[
{
"barIndex": 0,
"session": "2026-01-05",
"startTime": "2026-01-05T14:30:00.000Z",
"endTime": "2026-01-05T14:31:22.000Z",
"lastTradeTime": "2026-01-05T14:31:22.000Z",
"open": 99.99,
"high": 100.09,
"low": 99.98,
"close": 100.09,
"volume": 2406,
"dollarValue": 240695.03,
"tickCount": 21,
"firstTradeId": "T0001",
"lastTradeId": "T0021"
},
{
"barIndex": 1,
"session": "2026-01-05",
"startTime": "2026-01-05T14:31:30.000Z",
"endTime": "2026-01-05T14:33:10.000Z",
"lastTradeTime": "2026-01-05T14:33:10.000Z",
"open": 100.07,
"high": 100.21,
"low": 100.07,
"close": 100.21,
"volume": 3074,
"dollarValue": 307851.32,
"tickCount": 24,
"firstTradeId": "T0022",
"lastTradeId": "T0045"
},
{
"barIndex": 2,
"session": "2026-01-05",
"startTime": "2026-01-05T14:33:18.000Z",
"endTime": "2026-01-05T14:34:22.000Z",
"lastTradeTime": "2026-01-05T14:34:22.000Z",
"open": 100.23,
"high": 100.41,
"low": 100.22,
"close": 100.41,
"volume": 2306,
"dollarValue": 231336.47,
"tickCount": 16,
"firstTradeId": "T0046",
"lastTradeId": "T0061"
}
]Showing 3 of 14 elements.
Diagrams
Calculation flow
Causal construction flow — Tick-Run Bars
flowchart TD
A["Receive cleaned chronological trade"] --> B{"New session?"}
B -->|Yes| C["Emit or drop partial; reset price, sign, expectations, and bar state"]
B -->|No| D["Keep session state"]
C --> E["Assign tick sign; flat carries prior sign"]
D --> E
E --> F["Update OHLCV, N+, and N−"]
F --> G{"max(N+, N−) >= frozen h?"}
G -->|No| A
G -->|Yes| H["Emit complete bar with lineage and diagnostics"]
H --> I["Update E[T] and p+ from the completed bar"]
I --> J["Freeze the next threshold"]
J --> 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
- *Advances in Financial Machine Learning*, Section 2.3.2.2 — Marcos López de Prado
- Inferring Trade Direction from Intraday Data — Charles M. C. Lee and Mark J. Ready
- NYSE Daily TAQ Client Specification — New York Stock Exchange / Intercontinental Exchange
- Trade Reporting Frequently Asked Questions — Financial Industry Regulatory Authority (FINRA)
- Evidence classification
- Data, licensing, and historical-example decision