# Price-Level Quantity Aggregation

`D01-F05-A05` · Market Data Engineering → Order-Book Feed Engineering · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/order-book-feed-engineering/price-level-quantity-aggregation/
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 { aggregatePriceLevels } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/price-level-quantity-aggregation";
```

## Signature

```ts
aggregatePriceLevels(orders)
```

Collapses individual orders into quantity per price level — the Level 3 to Level 2 projection, and the form most analytics actually consume.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `orders` | `Order[]` | yes | Individual resting orders with price, quantity and side. |

## Returns

`{ bids, asks, level_count, … }`

Aggregated levels per side, sorted outward from the touch.

## Errors

- When an order has a non-positive quantity — excluded and reported

## Complexity

Time `O(orders log levels)`, space `O(levels)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`orders`:

```json
[
  {
    "order_id": "B00",
    "side": "bid",
    "price_ticks": 10000,
    "quantity": 4
  },
  {
    "order_id": "A01",
    "side": "ask",
    "price_ticks": 10002,
    "quantity": 9
  },
  {
    "order_id": "B02",
    "side": "bid",
    "price_ticks": 9999,
    "quantity": 14
  }
]
```

Showing 3 of 32 elements.

`argument 2`:

```json
{
  "depth_limit": 5
}
```

### Call

```ts
aggregatePriceLevels(orders)
```

### Returns

object with 13 fields: order_count, full_bid_level_count, full_ask_level_count, depth_limit, bids, asks, visible_bid_quantity, visible_ask_quantity, …

```json
{
  "order_count": 32,
  "full_bid_level_count": 8,
  "full_ask_level_count": 8,
  "depth_limit": 5,
  "bids": [
    {
      "price_ticks": 10000,
      "quantity": 12,
      "order_count": 2
    },
    {
      "price_ticks": 9999,
      "quantity": 32,
      "order_count": 2
    },
    {
      "price_ticks": 9998,
      "quantity": 14,
      "order_count": 2
    }
  ],
  "asks": [
    {
      "price_ticks": 10002,
      "quantity": 22,
      "order_count": 2
    },
    {
      "price_ticks": 10003,
      "quantity": 23,
      "order_count": 2
    },
    {
      "price_ticks": 10004,
      "quantity": 24,
      "order_count": 2
    }
  ],
  "visible_bid_quantity": 108,
  "visible_ask_quantity": 120,
  "full_bid_quantity": 200,
  "full_ask_quantity": 204,
  "hidden_by_depth_limit_bid_quantity": 92,
  "hidden_by_depth_limit_ask_quantity": 84,
  "state": "aggregated"
}
```

## Other exports

`normalizeEvents`, `reconstructL2`, `reconstructL3`, `recoverSequenceStream`, `reconcileSnapshotIncrementals`, `consolidateVenues`, `calculate`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **verified** (via D).

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/order-book-feed-engineering/price-level-quantity-aggregation/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/order-book-feed-engineering/price-level-quantity-aggregation/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
