# Tick-Imbalance Bars

`D01-F01-A05` · 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-imbalance-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-imbalance-bars";
```

## Signature

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

Closes a bar when signed tick flow becomes unusually one-sided relative to what recent history led you to expect. Bars are emitted on *information* rather than on elapsed time or traded quantity.

## 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; initialExpectedTickImbalance: number; alphaTicks: number; alphaTickImbalance: number; thresholdFloor: number; thresholdMultiplier: number; closePartial?: boolean }` | yes | The `initial*` values seed the expectations before any bar has closed; the `alpha*` values are the EWMA decay rates that update them afterwards. `thresholdFloor` and `thresholdMultiplier` bound the resulting threshold so it cannot collapse toward zero in quiet periods. |

## Returns

`Bar[]` · length fewer

One bar per imbalance event, with the threshold that triggered it recorded on the bar.

## Errors

- When any alpha falls outside 0…1, or a seed expectation is not positive — 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": "W1",
    "timestamp": "2026-01-05T14:30:00.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 10,
    "currency": "USD"
  },
  {
    "tradeId": "W2",
    "timestamp": "2026-01-05T14:30:01.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 10,
    "currency": "USD"
  },
  {
    "tradeId": "W3",
    "timestamp": "2026-01-05T14:30:02.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 100.1,
    "volume": 10,
    "currency": "USD"
  }
]
```

Showing 3 of 6 elements.

`config`:

```json
{
  "closePartial": true,
  "initialTickSign": 1,
  "initialExpectedTicks": 8,
  "initialExpectedTickImbalance": 0.5,
  "alphaTicks": 0.25,
  "alphaTickImbalance": 0.5,
  "thresholdFloor": 3,
  "thresholdMultiplier": 1
}
```

### Call

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

### Returns

array of 1 object

```json
[
  {
    "barIndex": 0,
    "session": "2026-01-05",
    "startTime": "2026-01-05T14:30:00.000Z",
    "endTime": "2026-01-05T14:30:05.000Z",
    "lastTradeTime": "2026-01-05T14:30:05.000Z",
    "open": 100,
    "high": 100.15,
    "low": 100,
    "close": 100.15,
    "volume": 60,
    "dollarValue": 6004,
    "tickCount": 6,
    "firstTradeId": "W1",
    "lastTradeId": "W6"
  }
]
```

## 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-imbalance-bars/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/bar-construction/tick-imbalance-bars/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Tick-Imbalance-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
