fintech-algorithms

Level-3 Order-by-Order Reconstruction

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

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

NameTypeNotes
snapshot_ordersL3Order[]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

executed 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
[
  {
    "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
{
  "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

reconstructL3(snapshot_orders)

Returns

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

{
  "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

This module also 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.

Diagrams

Level-3 Order-by-Order Reconstruction — d11 handoff
Level-3 Order-by-Order Reconstruction — failure map
Level-3 Order-by-Order Reconstruction — sequence boundary
Level-3 Order-by-Order Reconstruction — state transition
Level-3 Order-by-Order Reconstruction — system map

How it works

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References