Building a Financial Exchange from first principles
18 hours ago
- #financial-exchange
- #high-frequency-trading
- #system-design
- Speed and simplicity rarely come together, but farzi.exchange aims to achieve both in a fake stock exchange built from first principles.
- The core of a financial exchange is to match orders via an Order Book and a Matching Algorithm, with rules ensuring determinism and fairness.
- farzi.exchange is a price-time exchange, matching orders based on price and time, with tie-breaks considering disclosed quantity or order size.
- Key order types include Limit Orders, Market Orders, and Iceberg Orders, each serving different trading purposes and scenarios.
- The exchange architecture includes a Matching Engine with parallel goroutines per instrument, an in-memory Order Book using Binary Trees for speed.
- An Emitter fans out events to consumers, an Aggregator creates candles from trades, and a Persistence Layer stores data asynchronously in PostgreSQL.
- A MarketData Layer serves real-time data via websockets and historical data via APIs, using CQRS separation for scalability.
- Market Makers with personas (Farzi Maker, Seth, Scalper, Gambler) provide liquidity and price movement in the absence of real traders.
- The Farzi 10 Index is computed from underlying instrument values, updated on each trade and sampled at 100ms intervals.
- The exchange processed over 28 million trades and 122 million orders in a day, hosted on AWS with Vercel for the client, showing significant scalability.