# Trade-and-Quote Event Normalization

`D01-F05-A01` · 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/trade-and-quote-event-normalization/
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 { normalizeEvents } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/trade-and-quote-event-normalization";
```

## Signature

```ts
normalizeEvents(events)
```

Normalises raw venue trade and quote messages into one canonical event shape. Every venue names, orders and timestamps its fields differently; this is the boundary where that stops being everyone else's problem.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `events` | `RawEvent[]` | yes | Raw venue messages with their native field names, sequence numbers and timestamps. |

## Returns

`{ events, rejected, diagnostics }`

Canonical events plus the ones rejected and why — a normaliser that silently drops malformed messages hides a feed problem rather than surfacing it.

## Errors

- When an event carries no recognisable type — recorded in rejected rather than thrown

## Complexity

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

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

`events`:

```json
[
  {
    "venue": "XNAS",
    "instrument": "SYN1",
    "sequence": 100,
    "event_time_ns": 1000000,
    "receive_time_ns": 1000210,
    "price_scale": 10000,
    "kind": "quote",
    "bid_price_atoms": 1000000,
    "bid_quantity": 70,
    "ask_price_atoms": 1000200,
    "ask_quantity": 65
  },
  {
    "venue": "XNAS",
    "instrument": "SYN1",
    "sequence": 101,
    "event_time_ns": 1000300,
    "receive_time_ns": 1000545,
    "price_scale": 10000,
    "kind": "trade",
    "trade_id": "T01",
    "price_atoms": 1000200,
    "quantity": 11,
    "aggressor_side": "buy"
  },
  {
    "venue": "XNAS",
    "instrument": "SYN1",
    "sequence": 102,
    "event_time_ns": 1000600,
    "receive_time_ns": 1000880,
    "price_scale": 10000,
    "kind": "quote",
    "bid_price_atoms": 1000200,
    "bid_quantity": 74,
    "ask_price_atoms": 1000400,
    "ask_quantity": 67
  }
]
```

Showing 3 of 24 elements.

### Call

```ts
normalizeEvents(events)
```

### Returns

object with 8 fields: schema_version, event_count, trade_count, quote_count, venue_count, max_latency_ns, events, state

```json
{
  "schema_version": 1,
  "event_count": 24,
  "trade_count": 8,
  "quote_count": 16,
  "venue_count": 1,
  "max_latency_ns": 350,
  "events": [
    {
      "schema_version": 1,
      "venue": "XNAS",
      "instrument": "SYN1",
      "sequence": 100,
      "event_time_ns": 1000000,
      "receive_time_ns": 1000210,
      "latency_ns": 210,
      "price_scale": 10000,
      "kind": "quote",
      "bid_price_atoms": 1000000,
      "bid_price": 100,
      "bid_quantity": 70,
      "ask_price_atoms": 1000200,
      "ask_price": 100.02
    },
    {
      "schema_version": 1,
      "venue": "XNAS",
      "instrument": "SYN1",
      "sequence": 101,
      "event_time_ns": 1000300,
      "receive_time_ns": 1000545,
      "latency_ns": 245,
      "price_scale": 10000,
      "kind": "trade",
      "trade_id": "T01",
      "price_atoms": 1000200,
      "price": 100.02,
      "quantity": 11,
      "aggressor_side": "buy"
    },
    {
      "schema_version": 1,
      "venue": "XNAS",
      "instrument": "SYN1",
      "sequence": 102,
      "event_time_ns": 1000600,
      "receive_time_ns": 1000880,
      "latency_ns": 280,
      "price_scale": 10000,
      "kind": "quote",
      "bid_price_atoms": 1000200,
      "bid_price": 100.02,
      "bid_quantity": 74,
      "ask_price_atoms": 1000400,
      "ask_price": 100.04
    }
  ],
  "state": "normalized"
}
```

## Other exports

`reconstructL2`, `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/trade-and-quote-event-normalization/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/order-book-feed-engineering/trade-and-quote-event-normalization/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
