# Tick-Run Bars

`D01-F01-A07` · Market Data Engineering → Bar Construction · archetype `tape-aggregate` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/bar-construction/tick-run-bars/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { constructBars } from "fintech-algorithms/market-data-engineering/bar-construction/tick-run-bars";
```

## Signature

```ts
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 | Required | Notes |
| --- | --- | --- | --- |
| `trades` | `Trade[]` | yes | 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 }` | yes | `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

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`trades`:

```json
[
  {
    "tradeId": "W01",
    "timestamp": "2026-01-05T14:30:00.000Z",
    "session": "S1",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 10,
    "currency": "USD"
  },
  {
    "tradeId": "W02",
    "timestamp": "2026-01-05T14:30:01.000Z",
    "session": "S1",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 20,
    "currency": "USD"
  },
  {
    "tradeId": "W03",
    "timestamp": "2026-01-05T14:30:02.000Z",
    "session": "S1",
    "symbol": "SYNTH",
    "price": 99.9,
    "volume": 15,
    "currency": "USD"
  }
]
```

Showing 3 of 8 elements.

`config`:

```json
{
  "closePartial": true,
  "initialTickSign": 1,
  "initialExpectedTicks": 4,
  "initialBuyProbability": 0.625,
  "alphaTicks": 0.5,
  "alphaBuyProbability": 0.5,
  "thresholdFloorTicks": 2,
  "thresholdMultiplier": 1
}
```

### Call

```ts
constructBars(trades, config)
```

### Returns

array of 2 objects

```json
[
  {
    "firstTradeId": "W01",
    "lastTradeId": "W04",
    "tickCount": 4,
    "buyTicks": 3,
    "sellTicks": 1,
    "dominantSide": "buy",
    "thresholdTicks": 2.5,
    "overshootTicks": 0.5,
    "closeReason": "threshold"
  },
  {
    "firstTradeId": "W05",
    "lastTradeId": "W08",
    "tickCount": 4,
    "buyTicks": 1,
    "sellTicks": 3,
    "dominantSide": "sell",
    "thresholdTicks": 2.75,
    "overshootTicks": 0.25,
    "closeReason": "threshold"
  }
]
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/market-data-engineering/bar-construction/tick-run-bars/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/bar-construction/tick-run-bars/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Tick-Run-Bars-Bar-Construction-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
