High-performance, thread-safe C++ trading system with real-time WebSocket API, custom memory pool allocator, and order book matching engine.
- Order Book: Fast, time-priority matching for buy/sell orders
- Custom Pool Allocator: O(1) memory management for orders
- Thread Safety: Fine-grained locking with C++17
std::shared_mutex - WebSocket API: Real-time trading, order management, and market data
- Trade History: Persistent log of all executed trades
- Configurable: Easy to extend for new order types or matching logic
- C++17 compiler (g++, clang++)
- uWebSockets (v20+)
- nlohmann/json
- zlib (for compression)
-
Clone the repository:
git clone <your-repo-url>
-
Install dependencies:
- Option 1: Build from source
cd uWebSockets
make
Then move the folder to `libs/uWebSockets` in your project. - Option 2: Homebrew (macOS)
Add
brew install uwebsockets
/opt/homebrew/includeto your include path.
- Option 1: Homebrew (macOS) Example build command:
g++ -std=c++17 \
-Ilibs/uWebSockets/src \
-Ilibs/uWebSockets/uSockets/src \
-I/opt/homebrew/include \
websocket.cpp order-book.cpp libs/uWebSockets/uSockets/*.o -o trading_server -lz- Linux: Install with your package manager, e.g.:
sudo apt-get install zlib1g-dev
Note:
Do not commit third-party libraries (libs/uWebSockets, libs/nlohmann) to your repository.
Add them to .gitignore and document installation steps here.
- Build the server:
make
To clean build artifacts:
make cleanStart the trading server:
./trading_serverConnect via WebSocket (port 9001) and use JSON messages to:
- Authenticate
- Submit, modify, or cancel orders
- Query order status, order book, and trade history
The frontend/ app connects to the WebSocket server and renders:
- Realized/Unrealized PnL per connected client
- Order book snapshot
- Recent trades
Run the frontend:
cd frontend
npm install
npm run devEnvironment variables (optional):
VITE_WS_URL(defaultws://localhost:9001)VITE_AUTH_TOKEN(defaultyour_secret_token)VITE_ALGO_NAME(optional name displayed in PnL charts)
Notes:
- The server may send JSON as binary WebSocket frames; the frontend handles Blob/ArrayBuffer parsing.
- If you supply
VITE_ALGO_NAME, it will be sent during auth and echoed in PnL responses.
Authentication:
{"type": "auth", "token": "your_secret_token"}Submit Order:
{"type": "submit", "price": 100.5, "qty": 10, "is_buy": true}Cancel Order:
{"type": "cancel", "id": 123}Get Order Book Snapshot:
{"type": "getOrderBookSnapshot"}order-book.cpp— Order book and matching enginepool_allocator.h— Custom memory pool allocatorwebsocket.cpp— WebSocket server and APIlibs/uWebSockets/— uWebSockets source and build.vscode/— VS Code configurationMakefile— Build configuration
Pull requests are welcome! Please open issues for bugs or feature requests.
MIT License. See LICENSE for details.