# Level-2 Snapshot-and-Delta Reconstruction

`D01-F05-A02` · 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-2-snapshot-and-delta-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 { reconstructL2 } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/level-2-snapshot-and-delta-reconstruction";
```

## Signature

```ts
reconstructL2(snapshot)
```

Rebuilds an aggregated price-level book from a snapshot plus the deltas that follow it. Level 2 shows quantity per price, not individual orders — the distinction matters because a delta that is applied twice corrupts the book invisibly.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `snapshot` | `L2Snapshot` | yes | The starting snapshot with its sequence number, followed by the incremental updates to apply in order. |

## Returns

`{ book, applied, gaps, state }`

The reconstructed book with the updates applied and any sequence gaps encountered — a book rebuilt across a gap is wrong and must be said so.

## Errors

- When a delta's sequence precedes the snapshot — reported as a gap rather than thrown

## Complexity

Time `O(updates × 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

`snapshot`:

```json
{
  "bids": [
    {
      "price_ticks": 10000,
      "quantity": 20
    },
    {
      "price_ticks": 9999,
      "quantity": 25
    },
    {
      "price_ticks": 9998,
      "quantity": 30
    }
  ],
  "asks": [
    {
      "price_ticks": 10002,
      "quantity": 18
    },
    {
      "price_ticks": 10003,
      "quantity": 22
    },
    {
      "price_ticks": 10004,
      "quantity": 26
    }
  ]
}
```

`argument 2`:

```json
{
  "snapshot_sequence": 500,
  "deltas": [
    {
      "sequence": 501,
      "side": "bid",
      "price_ticks": 10000,
      "quantity": 12
    },
    {
      "sequence": 502,
      "side": "ask",
      "price_ticks": 10002,
      "quantity": 19
    },
    {
      "sequence": 503,
      "side": "bid",
      "price_ticks": 9999,
      "quantity": 26
    }
  ]
}
```

### Call

```ts
reconstructL2(snapshot)
```

### Returns

object with 12 fields: quantity_semantics, snapshot_sequence, last_sequence, applied_sequences, applied_count, discarded_sequences, discarded_count, bids, …

```json
{
  "quantity_semantics": "absolute-replacement",
  "snapshot_sequence": 500,
  "last_sequence": 524,
  "applied_sequences": [501, 502, 503, 504, 505, 506],
  "applied_count": 24,
  "discarded_sequences": [],
  "discarded_count": 0,
  "bids": [
    {
      "price_ticks": 10000,
      "quantity": 17
    },
    {
      "price_ticks": 9998,
      "quantity": 14
    },
    {
      "price_ticks": 9997,
      "quantity": 28
    }
  ],
  "asks": [
    {
      "price_ticks": 10002,
      "quantity": 24
    },
    {
      "price_ticks": 10003,
      "quantity": 38
    },
    {
      "price_ticks": 10004,
      "quantity": 21
    }
  ],
  "best_bid_ticks": 10000,
  "best_ask_ticks": 10002,
  "state": "current"
}
```

## Other exports

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