# Tick Bars

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

Full page: https://docs.thefintechbuilder.com/market-data-engineering/bar-construction/tick-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-bars";
```

## Signature

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

Closes a bar every N trades rather than every N seconds. Bars then arrive at the rate the market is transacting, so quiet periods produce fewer of them.

## 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` | `{ targetTicks: number; closePartial?: boolean }` | yes | `targetTicks` is the trade count that closes a bar. `closePartial` decides whether a final short bar is emitted. |

## Returns

`Bar[]` · length fewer

One bar per completed group of `targetTicks` trades.

## Errors

- When targetTicks is not a positive integer — 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": "E01",
    "timestamp": "2026-01-05T14:30:00.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 10,
    "currency": "USD"
  },
  {
    "tradeId": "E02",
    "timestamp": "2026-01-05T14:30:01.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 101,
    "volume": 20,
    "currency": "USD"
  },
  {
    "tradeId": "E03",
    "timestamp": "2026-01-05T14:30:02.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 99,
    "volume": 15,
    "currency": "USD"
  }
]
```

Showing 3 of 7 elements.

`config`:

```json
{
  "targetTicks": 3,
  "closePartial": true
}
```

### Call

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

### Returns

array of 3 objects

```json
[
  {
    "barIndex": 0,
    "session": "2026-01-05",
    "startTime": "2026-01-05T14:30:00.000Z",
    "endTime": "2026-01-05T14:30:02.000Z",
    "lastTradeTime": "2026-01-05T14:30:02.000Z",
    "open": 100,
    "high": 101,
    "low": 99,
    "close": 99,
    "volume": 45,
    "dollarValue": 4505,
    "tickCount": 3,
    "firstTradeId": "E01",
    "lastTradeId": "E03"
  },
  {
    "barIndex": 1,
    "session": "2026-01-05",
    "startTime": "2026-01-05T14:30:03.000Z",
    "endTime": "2026-01-05T14:30:05.000Z",
    "lastTradeTime": "2026-01-05T14:30:05.000Z",
    "open": 100,
    "high": 102,
    "low": 100,
    "close": 101,
    "volume": 65,
    "dollarValue": 6550,
    "tickCount": 3,
    "firstTradeId": "E04",
    "lastTradeId": "E06"
  },
  {
    "barIndex": 2,
    "session": "2026-01-05",
    "startTime": "2026-01-05T14:30:06.000Z",
    "endTime": "2026-01-05T14:30:06.000Z",
    "lastTradeTime": "2026-01-05T14:30:06.000Z",
    "open": 103,
    "high": 103,
    "low": 103,
    "close": 103,
    "volume": 5,
    "dollarValue": 515,
    "tickCount": 1,
    "firstTradeId": "E07",
    "lastTradeId": "E07"
  }
]
```

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