A crypto exchange simulator in Go. Runs a live order matching engine anchored to real Coinbase prices, with competing autonomous market maker and trader agents communicating over NATS. Also persists trades and orderbook snapshots to PostreSQL database (with backpressure handling)
- Go
- NATS (pub/sub + request-reply)
- Coinbase Advanced Trade API (WebSocket)
- PostgreSQL (TimescaleDB)
- Docker
| Metric | Result |
|---|---|
| Order throughput | 32,000+ orders/s |
| Trade throughput | 40,000+ trades/s |
| Average order latency | 1.2ms |
| Rejections | 0 |
| DB write throughput | 57,000+ writes/s |
The matching engine maintains three independent order books (BTC-USD, ETH-USD, XRP-USD), each backed by a max-heap (bids) and min-heap (asks) for price-time priority matching. Supports limit and market orders with partial fills. Agents submit orders via NATS request-reply for synchronous acknowledgment; price feeds and trade events fan out over pub/sub. Market Makers provide liquidity using three different strategies: a fixed-spread scalper (BTC), a momentum-skewed quoter (ETH), and an Avellaneda-Stoikov optimal market maker with dynamic inventory management (XRP). Traders consume liquidity via momentum following (ETH), mean reversion ladders (BTC), VWAP-based execution using live trade data (BTC), and random noise orders across all three coins to prevent book stalling. Persistence writes 57K+ rows/sec using PostgreSQL's binary COPY protocol (pgx.CopyFrom) with 8 worker goroutines. A circular buffer catches batches during transient DB failures to keep the hot path non-blocking. For the full breakdown — matching logic, strategy math, NATS channel design, data model, and tradeoff analysis — see the Design Document.
Prerequisites: Docker + Coinbase Advanced Trade API key
git clone https://github.com/dikpaal/CryptoSim.git
cd CryptoSim
cp .env.example .env # add Coinbase credentials
make build && make upIn a separate terminal:
go run ./cmd/tuiCryptoSim/
├── cmd/ # Entry points (engine, agents, TUI, persistence)
├── internal/ # Core packages (orderbook, matching, agents, pricing)
├── persistence/ # DB write path, circular buffer, batch workers
├── migrations/ # Embedded SQL for TimescaleDB hypertable setup
├── assets/ # Demo gif, screenshots
├── docker-compose.yml
├── Dockerfile
├── Makefile
└── LOAD_TESTING.md # Load testing methodology and results