# Level-3 Order-by-Order Reconstruction

`D01-F05-A03` · 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/level-3-order-by-order-reconstruction/
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 { reconstructL3 } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/level-3-order-by-order-reconstruction";
```

## Signature

```ts
reconstructL3(snapshot_orders)
```

Rebuilds the book order by order rather than by price level. Level 3 preserves queue position, which is the only way to answer where in the queue an order actually sits — and therefore the only basis for a realistic fill model.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `snapshot_orders` | `L3Order[]` | yes | Individual resting orders with their identifiers, prices, quantities and arrival order. |

## Returns

`{ book, orders, queue_state, diagnostics }`

The order-level book with queue positions preserved.

## Errors

- When an update references an order id not in the book — recorded in diagnostics rather than thrown

## Complexity

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

## 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

`snapshot_orders`:

```json
[
  {
    "order_id": "B1",
    "side": "bid",
    "price_ticks": 10001,
    "quantity": 12
  },
  {
    "order_id": "B2",
    "side": "bid",
    "price_ticks": 10000,
    "quantity": 14
  },
  {
    "order_id": "B3",
    "side": "bid",
    "price_ticks": 10000,
    "quantity": 16
  }
]
```

Showing 3 of 12 elements.

`argument 2`:

```json
{
  "snapshot_sequence": 800,
  "events": [
    {
      "sequence": 801,
      "action": "add",
      "order_id": "N1",
      "side": "bid",
      "price_ticks": 10001,
      "quantity": 9
    },
    {
      "sequence": 802,
      "action": "execute",
      "order_id": "A1",
      "quantity": 3
    },
    {
      "sequence": 803,
      "action": "reduce",
      "order_id": "B1",
      "quantity": 2
    }
  ]
}
```

### Call

```ts
reconstructL3(snapshot_orders)
```

### Returns

object with 9 fields: snapshot_sequence, last_sequence, order_count, event_count, action_counts, orders, bids, asks, …

```json
{
  "snapshot_sequence": 800,
  "last_sequence": 820,
  "order_count": 16,
  "event_count": 20,
  "action_counts": {
    "add": 7,
    "reduce": 4,
    "execute": 4,
    "delete": 3,
    "replace": 2
  },
  "orders": [
    {
      "order_id": "B1",
      "side": "bid",
      "price_ticks": 10001,
      "quantity": 10,
      "priority_rank": 1
    },
    {
      "order_id": "B5",
      "side": "bid",
      "price_ticks": 9999,
      "quantity": 17,
      "priority_rank": 5
    },
    {
      "order_id": "B6",
      "side": "bid",
      "price_ticks": 9998,
      "quantity": 22,
      "priority_rank": 6
    }
  ],
  "bids": [
    {
      "price_ticks": 10001,
      "quantity": 22,
      "order_count": 3
    },
    {
      "price_ticks": 10000,
      "quantity": 15,
      "order_count": 1
    },
    {
      "price_ticks": 9999,
      "quantity": 30,
      "order_count": 2
    }
  ],
  "asks": [
    {
      "price_ticks": 10002,
      "quantity": 13,
      "order_count": 2
    },
    {
      "price_ticks": 10003,
      "quantity": 16,
      "order_count": 1
    },
    {
      "price_ticks": 10004,
      "quantity": 31,
      "order_count": 2
    }
  ],
  "state": "current"
}
```

## Other exports

`normalizeEvents`, `reconstructL2`, `aggregatePriceLevels`, `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/level-3-order-by-order-reconstruction/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/order-book-feed-engineering/level-3-order-by-order-reconstruction/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
