This repository now contains a complete high-performance trading stack:
- C++17 adaptive lock-free order book and matching engine
- FastAPI backend that runs the C++ engine in API mode
- React + Vite frontend dashboard (TradeVIT)
- Read-only Auto Insight Mode for predictive market signals
The implementation is designed for low-latency experimentation and research workloads where throughput, stability, and observability matter.
- Adaptive dual data structure:
- Sorted vector for low-load paths
- Lock-free skip list for high-load paths
- Price-time priority matching
- Parallel insertion and lock-free contention handling (CAS with retry/backoff)
- Hysteresis-based switching to avoid oscillation
- Runtime strategy controls for rule-based vs ML-based mode decisions
- Starts and manages the C++ engine process (
--apimode) - Exposes REST APIs for order submission, order book, trades, and simulation control
- Validates order side/price/qty before forwarding to engine
- Exposes aggregated system metrics from engine CSV logs
- TradeVIT branded trading UI
- Live order book and trade history panels
- Manual order entry
- Auto-trading simulator toggle
- Auto Insight ON/OFF toggle
- Hover-based contextual insight tooltips in order book and trade history
- Floating alert panel with bullish/bearish/warning signals
- Detects support/resistance walls, imbalance shifts, volatility bursts, spread widening
- Includes anti-spoofing heuristics and confidence/ETA windows
- Optionally improves/reduces confidence using backend system metrics
- Never mutates or writes to the order book (read-only inference layer)
Frontend (React/Vite)
|
| HTTP polling (order book, trades, metrics, controls)
v
Backend (FastAPI)
|
| stdio JSON commands (ADD / GET_BOOK / GET_TRADES)
v
C++ Engine (order_book_engine --api)
|
+-- AdaptiveOrderBook (buy)
+-- AdaptiveOrderBook (sell)
+-- MatchingEngine thread
The engine monitors:
- arrival rate
- queue depth / book size
- skip-list contention
- recent latency
Default thresholds used by rule mode:
- low load threshold:
1000 orders/sec - high load threshold:
5000 orders/sec - mode switch hysteresis:
100 ms
- Runtime toggle:
--ml-mode ON|OFF - Model file:
--ml-model <path> - Uses lightweight prediction for target mode selection
- Automatic fallback to rule mode when:
- model is unavailable
- prediction fails
- inference budget is exceeded
- prediction pattern is unstable
Mode checks are now performed in all active flows:
- benchmark flows
- interactive flow
- API flow (periodic checks after insert batches)
This keeps adaptive switching active during dashboard/API usage, not only benchmarks.
Enable/disable and configure via:
--metrics-log ON|OFF--metrics-log-prefix <prefix>
Generated files:
<prefix>_buy.csv<prefix>_sell.csv
Logged columns include:
arrival_rate,book_size,queue_depth,contention,recent_latency_us- rule-selected mode, final selected mode
- strategy name (
RULE/ML) - ML confidence and inference time
- fallback-used flag
GET /api/system-metrics
- Reads latest rows from buy/sell CSV files
- Returns averaged runtime indicators and strategy status
- Returns
{"status":"unavailable"}when metrics files are not yet available
Environment variable:
ENGINE_METRICS_PREFIX(default:ml_switch_metrics)
Base URL: http://127.0.0.1:8000
GET /health- Engine process status
POST /api/orders- Body:
{ "side": "buy|sell", "price": number, "qty": int } - Strict validation for side/price/qty
- Body:
GET /api/orderbook?depth=20- Returns top bid/ask levels
GET /api/trades?limit=50- Returns recent executed trades
POST /api/simulation/startPOST /api/simulation/stopGET /api/simulation/statusGET /api/system-metrics
The benchmark flow now includes:
- Single-threaded baseline
- Multi-threaded scalability
- Low load scenario
- High load scenario
- Burst traffic scenario
- Heavy skew scenario
- High contention scenario
- Rule vs ML comparison
Collected outputs include throughput, latency percentiles (P50/P95/P99), mode switches, and ML fallback/failure counters.
- C++17 compiler (
g++/clang) make- Python 3.10+
- Node.js 18+
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r backend/requirements.txt
cd frontend
npm install
cd ..
makeOption A (script):
./run_app.shOption B (manual, two terminals):
Terminal 1:
source .venv/bin/activate
python backend/main.pyTerminal 2:
cd frontend
npm run devFrontend default URL:
http://localhost:5173
Backend default URL:
http://127.0.0.1:8000
./order_book_engine --help
./order_book_engine --demo
./order_book_engine --interactive
./order_book_engine --quick 10000
./order_book_engine --benchmark 50000With ML mode enabled:
./order_book_engine --benchmark 50000 --ml-mode ON --ml-model ml_model_weights.csv
./order_book_engine --quick 10000 --ml-mode ON --metrics-log ON --metrics-log-prefix ml_runAPI mode:
./order_book_engine --api --ml-mode ON --ml-model ml_model_weights.csvSystem Operationalbadge reflects backend connectivity- Auto-trading switch controls backend market-maker simulation
- Auto Insight panel shows active predictive alerts and confidence
- Hovering order book levels reveals support/resistance interpretation
- Hovering trade rows reveals momentum/impact interpretation
.
├── main.cpp
├── order_book_engine.hpp
├── matching_engine.hpp
├── order_generator.hpp
├── benchmark.hpp
├── Makefile
├── CMakeLists.txt
├── run_app.sh
├── build_and_test.sh
├── test_api.py
├── backend/
│ ├── main.py
│ ├── bot.py
│ └── requirements.txt
└── frontend/
├── package.json
├── src/
│ ├── App.tsx
│ ├── index.css
│ ├── components/
│ │ ├── OrderBook.tsx
│ │ ├── OrderForm.tsx
│ │ ├── TradeHistory.tsx
│ │ └── InsightPanel.tsx
│ └── insights/
│ └── autoInsightEngine.ts
└── ...
Build and smoke test:
./build_and_test.shBackend health:
curl -sS http://127.0.0.1:8000/healthAPI sanity test:
python test_api.pyFrontend production build:
cd frontend
npm run buildPlanned enhancements that are still open:
- dedicated unit test suite for core engine internals
- persistent order book snapshots/replay tooling
- multi-symbol support and symbol-partitioned books
- async metrics sink for stricter non-blocking telemetry writes
MIT License