# Time Bars

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

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

## Signature

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

Groups a trade tape into fixed-duration bars. This is the sampling scheme every chart you have seen uses, and it is a *choice*: it samples the market at a constant rate regardless of how much is happening in it.

## 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` | `{ intervalSeconds: number; sessionStarts: Record<string, string>; closePartial?: boolean; emptyBarPolicy?: "omit" }` | yes | `intervalSeconds` sets the bar length. `sessionStarts` maps each session id to its opening timestamp, so bucket boundaries are anchored to the session rather than to the first trade. `closePartial` decides whether a final incomplete bar is emitted. `emptyBarPolicy: "omit"` drops intervals with no trades instead of emitting a flat bar. |

## Returns

`Bar[]` · length fewer

One bar per interval that produced trades, carrying open, high, low, close, volume and the interval boundaries.

## Errors

- When intervalSeconds is not a positive number — throws
- When sessionStarts is missing or empty — 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": "S1",
    "symbol": "SYNTH",
    "price": 100,
    "volume": 10,
    "currency": "USD"
  },
  {
    "tradeId": "W2",
    "timestamp": "2026-01-05T14:30:59.999Z",
    "session": "S1",
    "symbol": "SYNTH",
    "price": 101,
    "volume": 5,
    "currency": "USD"
  },
  {
    "tradeId": "W3",
    "timestamp": "2026-01-05T14:31:00.000Z",
    "session": "S1",
    "symbol": "SYNTH",
    "price": 99,
    "volume": 8,
    "currency": "USD"
  }
]
```

Showing 3 of 4 elements.

`config`:

```json
{
  "intervalSeconds": 60,
  "sessionStarts": {
    "S1": "2026-01-05T14:30:00.000Z"
  },
  "emptyBarPolicy": "omit",
  "closePartial": true
}
```

### Call

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

### Returns

array of 3 objects

```json
[
  {
    "barIndex": 0,
    "session": "S1",
    "intervalIndex": 0,
    "startTime": "2026-01-05T14:30:00.000Z",
    "endTime": "2026-01-05T14:31:00.000Z",
    "firstTradeTime": "2026-01-05T14:30:00.000Z",
    "lastTradeTime": "2026-01-05T14:30:59.999Z",
    "open": 100,
    "high": 101,
    "low": 100,
    "close": 101,
    "volume": 15,
    "dollarValue": 1505,
    "tickCount": 2
  },
  {
    "barIndex": 1,
    "session": "S1",
    "intervalIndex": 1,
    "startTime": "2026-01-05T14:31:00.000Z",
    "endTime": "2026-01-05T14:32:00.000Z",
    "firstTradeTime": "2026-01-05T14:31:00.000Z",
    "lastTradeTime": "2026-01-05T14:31:00.000Z",
    "open": 99,
    "high": 99,
    "low": 99,
    "close": 99,
    "volume": 8,
    "dollarValue": 792,
    "tickCount": 1
  },
  {
    "barIndex": 2,
    "session": "S1",
    "intervalIndex": 3,
    "startTime": "2026-01-05T14:33:00.000Z",
    "endTime": "2026-01-05T14:34:00.000Z",
    "firstTradeTime": "2026-01-05T14:33:15.000Z",
    "lastTradeTime": "2026-01-05T14:33:15.000Z",
    "open": 102,
    "high": 102,
    "low": 102,
    "close": 102,
    "volume": 2,
    "dollarValue": 204,
    "tickCount": 1
  }
]
```

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