A high-performance order matching engine written in Go.
- Limit and market order support
- Time-in-force: GTC (default), IOC (Immediate-or-Cancel), FOK (Fill-or-Kill)
- Stop orders: Stop-Market and Stop-Limit with automatic trigger on last trade price
- Price-time priority matching (FIFO)
- Exact decimal arithmetic via shopspring/decimal
- Efficient price-level order book with O(log p) insertion (p = distinct price levels)
- Duplicate order ID detection
- Internal order ID generation (atomic counter, monotonically increasing)
- Self-trade prevention (4 modes: CancelResting, CancelIncoming, CancelBoth, Decrement)
- Thread-safe design with snapshot-based order book reads
- Bounded trade log with configurable size and trade callback
- Symbol validation and normalization
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Order Input ββββββΆβ Match Engine ββββββΆβ Trades β
βββββββββββββββ ββββββββ¬ββββββββ βββββββββββββββ
β
ββββββββ΄ββββββββ
β Order Book β
ββββββββββββββββ€
β Bids (Buy) β
β Asks (Sell) β
ββββββββββββββββ
- Go 1.21+
go build ./...go test ./... -vgo run cmd/example/main.go.
βββ pkg/
β βββ engine/ # Core matching engine
β βββ orderbook/ # Order book implementation
β βββ model/ # Order, Trade, and other models
βββ cmd/
β βββ example/ # Example usage
βββ go.mod
βββ README.md
The engine uses a price-time priority algorithm:
- Buy orders are sorted by price descending, then by time ascending
- Sell orders are sorted by price ascending, then by time ascending
- A match occurs when the best bid price >= best ask price
- Partial fills are supported β remaining quantity stays in the book
MIT License