Overview
A full-stack order matching engine that implements a price-time priority algorithm
for matching buy and sell orders across multiple trading symbols. The backend is built
in C++20 with a REST API served via cpp-httplib, while the frontend uses
Next.js and TypeScript to provide a real-time trading dashboard.
The engine supports LIMIT and MARKET orders, partial fills, order cancellation, and a full trade history. The frontend auto-refreshes every two seconds to reflect live order book and trade data without requiring WebSockets.
Key Features
- Price-time priority matching algorithm (FIFO within same price level)
- LIMIT and MARKET order support with partial fills
- Multi-symbol order book (
std::mapwith custom comparators) - REST API: submit, cancel, query orders and trades
- O(1) order cancellation via
std::unordered_maplookup - Next.js frontend with live order book, trade history, and statistics
- Fixed-point arithmetic (prices stored as integer cents) for precision
Tech Stack
Architecture
The C++ backend runs a multi-threaded HTTP server on port 8080. A Next.js proxy
rewrites /api/* requests to the backend, avoiding CORS issues while keeping
the frontend on port 3000. All prices are stored as 64-bit integer cents to eliminate
floating-point precision problems.
Challenges & Learnings
The hardest part was designing a data structure that guarantees correct price-time
priority: a std::map naturally sorts by price, but matching multiple
deques within a price level required careful iterator management during partial fills.
Bridging a compiled C++ server with a Node.js frontend also required setting up a
transparent reverse-proxy in Next.js config to keep development seamless.