trading-algo
2025–2026 · Solo project · C++17 · Verified end-to-end against OKX demo trading
A low-latency C++17 trading bot for OKX. It streams the live BTC-USDT order book over a TLS WebSocket, rebuilds the book locally from a snapshot + incremental updates (with sequence-gap detection and CRC32 integrity checks), runs a strategy over top-of-book signals, and places / amends / cancels orders on the same instrument — with a background logger, pre-trade risk gate, and a kill switch. Built and verified end-to-end against OKX demo trading (paper funds, zero financial risk).
The core trading logic is a pure, fully unit-tested pipeline (352 checks, 0 failures, ASan-clean) that runs identically live and in an offline replay/backtest driver — so correctness is provable without a network.
Learning outcomes
- Real-time order-book reconstruction from snapshot + incremental deltas, with sequence-gap recovery and CRC32 integrity verification (HFT-grade correctness).
- Hot-path hygiene: cache-friendly containers, lock-free hand-off, zero-alloc checksum, async logging kept off the critical path.
- Live exchange plumbing: Boost.Beast + Asio over TLS, HMAC-SHA256 request signing, WebSocket order ops, reconnect/heartbeat, graceful shutdown.
Correctness & safety
- Never trades on an invalid book. Any sequence gap or checksum mismatch invalidates the book and pulls all resting quotes until a fresh snapshot re-validates it.
- Fixed-point integer prices — order-book ordering is never subject to floating-point error.
- Pre-trade risk and balance gates on every order: max size, max net position, max open orders, a PnL-floor kill switch, and a refusal to send any order the account can’t actually fund.
- Startup reconciliation — on launch the bot cancels any order still resting on the exchange, so a restart begins from a known-flat book instead of quoting on top of orphans.
Result worth naming
The published latency figures were once wrong in an instructive way: a per-stage breakdown showed JSON parsing dominates the hot path at 72–76% of per-message cost — the opposite of what the project had previously concluded by comparing two corpora. The parse sat outside the measured span the whole time. Measuring the stage directly, rather than inferring it, is what corrected the record.