# Multi-Venue Best-Quote and Book Consolidation

`D01-F05-A07` · 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/multi-venue-best-quote-and-book-consolidation/
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 { consolidateVenues } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/multi-venue-best-quote-and-book-consolidation";
```

## Signature

```ts
consolidateVenues(quotes)
```

Merges books from several venues into one consolidated view and identifies the best bid and offer across them. Venue clocks differ, so a naive merge can produce a consolidated book that was never simultaneously true.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `quotes` | `VenueQuote[]` | yes | Per-venue quotes with their own timestamps, which is what makes staleness assessable per venue rather than globally. |

## Returns

`{ best_bid, best_ask, consolidated, contributors, stale_venues, … }`

The consolidated top of book with which venue contributed each side, and any venue excluded as stale.

## Errors

- When no venue supplies a usable quote — reported as a status rather than thrown

## Complexity

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

`quotes`:

```json
[
  {
    "venue": "X1",
    "receive_time_ns": 950000,
    "eligible": true,
    "status": "open",
    "bids": [
      {
        "price_ticks": 10000,
        "quantity": 10
      },
      {
        "price_ticks": 9999,
        "quantity": 16
      },
      {
        "price_ticks": 9998,
        "quantity": 22
      }
    ],
    "asks": [
      {
        "price_ticks": 10003,
        "quantity": 9
      },
      {
        "price_ticks": 10004,
        "quantity": 15
      },
      {
        "price_ticks": 10005,
        "quantity": 21
      }
    ]
  },
  {
    "venue": "X2",
    "receive_time_ns": 980000,
    "eligible": true,
    "status": "open",
    "bids": [
      {
        "price_ticks": 10001,
        "quantity": 6
      },
      {
        "price_ticks": 10000,
        "quantity": 12
      },
      {
        "price_ticks": 9999,
        "quantity": 18
      }
    ],
    "asks": [
      {
        "price_ticks": 10002,
        "quantity": 7
      },
      {
        "price_ticks": 10003,
        "quantity": 11
      },
      {
        "price_ticks": 10004,
        "quantity": 17
      }
    ]
  },
  {
    "venue": "X3",
    "receive_time_ns": 850000,
    "eligible": true,
    "status": "open",
    "bids": [
      {
        "price_ticks": 10001,
        "quantity": 20
      },
      {
        "price_ticks": 10000,
        "quantity": 20
      }
    ],
    "asks": [
      {
        "price_ticks": 10003,
        "quantity": 20
      },
      {
        "price_ticks": 10004,
        "quantity": 20
      }
    ]
  }
]
```

Showing 3 of 5 elements.

`argument 2`:

```json
{
  "as_of_receive_time_ns": 1000000,
  "max_staleness_ns": 100000
}
```

### Call

```ts
consolidateVenues(quotes)
```

### Returns

object with 13 fields: as_of_receive_time_ns, max_staleness_ns, eligible_venue_count, excluded, best_bid_ticks, best_bid_quantity, best_bid_venues, best_ask_ticks, …

```json
{
  "as_of_receive_time_ns": 1000000,
  "max_staleness_ns": 100000,
  "eligible_venue_count": 3,
  "excluded": [
    {
      "venue": "X3",
      "reason": "stale"
    },
    {
      "venue": "X4",
      "reason": "not-open"
    }
  ],
  "best_bid_ticks": 10001,
  "best_bid_quantity": 10,
  "best_bid_venues": ["X2", "X5"],
  "best_ask_ticks": 10002,
  "best_ask_quantity": 12,
  "best_ask_venues": ["X2", "X5"],
  "consolidated_book": {
    "bids": [
      {
        "price_ticks": 10001,
        "quantity": 10,
        "venues": ["X2", "X5"]
      },
      {
        "price_ticks": 10000,
        "quantity": 31,
        "venues": ["X1", "X2", "X5"]
      },
      {
        "price_ticks": 9999,
        "quantity": 48,
        "venues": ["X1", "X2", "X5"]
      }
    ],
    "asks": [
      {
        "price_ticks": 10002,
        "quantity": 12,
        "venues": ["X2", "X5"]
      },
      {
        "price_ticks": 10003,
        "quantity": 30,
        "venues": ["X1", "X2", "X5"]
      },
      {
        "price_ticks": 10004,
        "quantity": 47,
        "venues": ["X1", "X2", "X5"]
      }
    ]
  },
  "consolidated_levels": [
    {
      "side": "bid",
      "price_ticks": 10001,
      "quantity": 10,
      "venues": ["X2", "X5"]
    },
    {
      "side": "bid",
      "price_ticks": 10000,
      "quantity": 31,
      "venues": ["X1", "X2", "X5"]
    },
    {
      "side": "bid",
      "price_ticks": 9999,
      "quantity": 48,
      "venues": ["X1", "X2", "X5"]
    }
  ],
  "state": "normal"
}
```

## Other exports

`normalizeEvents`, `reconstructL2`, `aggregatePriceLevels`, `reconstructL3`, `recoverSequenceStream`, `reconcileSnapshotIncrementals`, `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/multi-venue-best-quote-and-book-consolidation/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/order-book-feed-engineering/multi-venue-best-quote-and-book-consolidation/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
