# Dollar Bars

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

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

## Signature

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

Closes a bar once a target traded *value* is reached. Unlike volume bars this stays comparable as the price level changes — 1,000 shares of a $10 stock and of a $500 stock are not the same event.

## 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` | `{ targetDollar: number; currency: string; priceDecimals?: number; quantityDecimals?: number; closePartial?: boolean }` | yes | `targetDollar` is the notional that closes a bar and `currency` the unit it is denominated in. The two decimal settings fix the rounding used when accumulating price × quantity, so the same tape gives the same bars on any machine. |

## Returns

`Bar[]` · length fewer

One bar per completed notional bucket.

## Errors

- When targetDollar is not positive — throws
- When a trade's currency does not match the configured currency — 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": 40,
    "currency": "USD"
  },
  {
    "tradeId": "W2",
    "timestamp": "2026-01-05T14:30:01.000Z",
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "price": 90,
    "volume": 70,
    "currency": "USD"
  }
]
```

`config`:

```json
{
  "targetDollar": 10000,
  "currency": "USD",
  "priceDecimals": 2,
  "quantityDecimals": 0,
  "closePartial": true
}
```

### Call

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

### Returns

array of 1 object

```json
[
  {
    "barIndex": 0,
    "session": "2026-01-05",
    "symbol": "SYNTH",
    "currency": "USD",
    "startTime": "2026-01-05T14:30:00.000Z",
    "endTime": "2026-01-05T14:30:01.000Z",
    "lastTradeTime": "2026-01-05T14:30:01.000Z",
    "open": 100,
    "high": 100,
    "low": 90,
    "close": 90,
    "volume": 110,
    "dollarValue": 10300,
    "targetDollar": 10000
  }
]
```

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