Skip to content
HS®
All projects

(011) / 012

OrderBookMatchingEngine

An in-memory order book and matching engine behind a small REST API: place orders, read depth, track balances and quote VWAP.

Category
Trading systems · Prototype
Year
2025
Last pushed
Sept 2025
Primary language
TypeScript
BIDSASKSMID
Conceptual diagram — bid and ask depth around the mid price.

(01) Overview

A small matching engine and order book written in TypeScript on Express. It keeps bids and asks in memory for a single ticker, matches incoming orders against the opposite side, moves balances between the two users on every fill, and exposes depth and quote endpoints.

It is a prototype for learning how an exchange's core loop works, not a production trading system.

(02) Problem statement

Every exchange has the same heart: an order book and the rules for matching against it. The mechanics — crossing prices, partial fills, resting orders, depth, mid price, VWAP — are easy to describe and worth implementing once to really understand.

(03) Technical approach

  • Keep both books in memory as arrays sorted by price.
  • On each new order, try to fill it against the opposite book first.
  • Whatever is left over rests on the book.
  • Derive all market data — depth and quotes — from the two books on request.

(04) System architecture

Placing an orderConceptual diagram · select a stage

Stage 01 — POST /order

{ side, price, quantity, userId }

(05) Implementation details

POST /order takes a side, price, quantity and user. The matching function walks the opposite book from the best price and fills while the resting price crosses the incoming one, handling both full and partial fills. Each fill calls a balance function that moves the asset one way and USD the other. Any remaining quantity is pushed onto the book, which is then re-sorted.

GET /depth aggregates resting quantity per price level. GET /quote returns best bid, best ask and mid price, and — when a number of levels is requested — a volume-weighted average price for each side over those levels. GET /balance/:userId returns a user's holdings.

(06) Main features

  1. 01Bid and ask books kept sorted by price for a single ticker.
  2. 02Incoming orders are matched against the opposite book; any remainder rests on the book.
  3. 03Partial fills, with balances moved between counterparties on every fill.
  4. 04Aggregated depth by price level.
  5. 05Quotes with best bid, best ask, mid price and VWAP over a chosen number of levels.

(07) Technology stack

TypeScriptNode.jsExpressJest

(08) Engineering challenges

  • Partial fills. An incoming order can consume several resting orders and still have quantity left; each case has to leave the book and both balances consistent.
  • Deriving quotes. Mid price has to handle a one-sided book, and VWAP has to weight by quantity across a chosen depth.
  • What a real engine would change. Re-sorting an array on every insert is fine at this size but is not how a low-latency engine is built — a production version would use price-level structures with FIFO queues, time priority within a level, multiple tickers and persistence. None of that is implemented here.

(09) Evaluation and results

The repository includes a Jest and Supertest spec for the API. It contains no latency or throughput benchmarks, so despite the repository's name no performance figures are claimed here.

Links