Mock Stock Exchange Platform
Real-time matching engine handling 500+ trades per session
Sep 2024 – Oct 2024
Solo Developer
500+
Trades/Session
100+
Users Simulated
100%
Test Coverage
ms-level
Order Updates
The Problem
Understanding how real exchanges work requires hands-on implementation. Reading about price-time priority is one thing; handling partial fills and maintaining order book integrity is another. I built this to deeply understand exchange mechanics.
The Insight
Financial systems demand precision. A single edge case in the matching engine—say, a partial fill that doesn't update remaining quantity correctly—can cascade into incorrect portfolio states and audit trail gaps. The devil is in the details.
The Solution
Core Architecture
Modular class design with clear separation of concerns:
- Price: Immutable value object for currency handling
- Order: Buy/sell orders with quantity, price, and timestamp
- Quote: Real-time bid/ask spread tracking
- OrderBook: Price-time priority matching engine
- Portfolio: User holdings and balance validation
Matching Engine
class="text-purple-400">public List<Trade> match(Order incomingOrder) {
List<Trade> trades = new ArrayList<>();
PriorityQueue<Order> oppositeBook = incomingOrder.isBuy()
? sellOrders : buyOrders;
while (!oppositeBook.isEmpty() && canMatch(incomingOrder, oppositeBook.peek())) {
Order matchedOrder = oppositeBook.peek();
int fillQty = Math.min(
incomingOrder.getRemainingQty(),
matchedOrder.getRemainingQty()
);
class="text-text-muted">// Execute trade
trades.add(new Trade(incomingOrder, matchedOrder, fillQty));
class="text-text-muted">// Handle partial fills
incomingOrder.fill(fillQty);
matchedOrder.fill(fillQty);
if (matchedOrder.isFilled()) {
oppositeBook.poll();
}
if (incomingOrder.isFilled()) break;
}
return trades;
}Results
The system handles 100+ concurrent users and 1,000+ trade events per session. The matching engine executes 500+ trades with millisecond-level order book updates. 50+ unit and integration tests provide 100% coverage.
Reflections
The hardest part was handling partial fills correctly—when a large order matches against multiple smaller orders at different prices. Edge cases in financial systems compound quickly. This project taught me why exchanges invest so heavily in testing infrastructure.