This project is a high-performance, polyglot system designed to serve as a mock electronic trading engine, simulating the core components of a real financial exchange's Central Limit Order Book (CLOB). It uses a layered architecture to maximize speed in the core logic while maintaining flexibility in the API layer.
| Component | Technology | Role | |
|---|---|---|---|
| Server Interface (C++) | httplib |
Provides the minimal, high-speed HTTP interface for the Go API to communicate with the engine process. | |
| API Gateway (Go) | Go (w/ go-chi/chi, net/http) |
Routing, thread-safe OrderID generation, and proxying requests to the C++ engine. |
The core engine models key market concepts and logic:
- Limit Order Management: Handles creation, placement, and persistence of buy and sell orders.
- CLOB Structure: Maintains separate internal data structures (Bids/Asks) for the book state.
- Priority Matching: Orders are executed based on Price–Time Priority (best price first, then earliest submission time).
- Order Types: Models GTC (Good-Till-Cancel) and FAK (Fill-And-Kill) orders.
- Real-World State: Tracks important metrics like the total quantity at specific Price Levels and handles Partially Filled Orders.
To run the system, you must compile and start the C++ engine first, followed by the Go API.
-
Navigate to the C++ Directory:
cd /S/Users/tanis/Desktop/Projects/OrderBook/backend/engine -
Compile the Server: This command compiles the entire merged engine and server logic.
g++ -std=c++23 -O2 Server.cpp -lws2_32 -o server.exe
-
Run the Server: Keep this console window open and running.
./server.exe
(Output will confirm listening on port 6060).
-
Open a NEW Console Window.
-
Navigate to the Go API Directory:
cd /S/Users/tanis/Desktop/Projects/OrderBook/backend/cmd/api -
Run the Go Server:
go run main.go
(Output will confirm listening on port 8000).
Direct all requests to the Go API on Port 8000. The Go API will handle ID generation and proxy the asset name as the book parameter.
This order creates the first resting Buy Limit order for TSLA, setting up the Bid side of the book.
- URL:
http://localhost:8000/order/trade - Method:
POST - Body (Raw JSON):
{ "tradetype": "GTC", "side": "BUY", "price": 100, "quantity": 100, "name": "TSLA" } - Expected Status:
200 OK
This order crosses the spread, immediately matching the resting Buy order from Step 1.
- URL:
http://localhost:8000/order/trade - Method:
POST - Body (Raw JSON):
{ "tradetype": "GTC", "side": "SELL", "price": 99, "quantity": 50, "name": "TSLA" } - Expected Status:
200 OK. The JSON response confirms a Trade occurred at $100. The original Buy order remains in the book with a remaining quantity of 50.
This removes the partially filled order from the book using the OrderID generated from the first trade call (which is likely 1).
- URL:
http://localhost:8000/order/cancel - Method:
POST - Body (Raw JSON):
{ "orderid": 1, "book": "TSLA" } - Expected Status:
200 OK(Message confirms successful removal).
This returns all information across all Orderbook's, stock information, and ask(s)/bid(s) at each level.
- URL:
http://localhost:8000/order/status - Method:
GET - Body (Raw JSON):
{ "AAPL": { "bids": [ { "type": "Bid", "price": 100, "quantity": 200 } ], "asks": [], "size": 2 }, "GOOG": { "bids": [ { "type": "Bid", "price": 100, "quantity": 200 } ], "asks": [], "size": 2 }, "TSLA": { "bids": [ { "type": "Bid", "price": 100, "quantity": 200 } ], "asks": [], "size": 2 }
} ```
- Expected Status:
200 OK(Message confirms successful retrieval).
The fundamental concepts and core algorithm structure for the Orderbook matching engine were derived from the open-source work of CodingJesus, with some additional features. Everything else including backend api, server functions, parsing logic, and frontend were built entirely from scratch.