This document provides a comprehensive, step-by-step summary of the lecture detailing the system design, financial mechanics, and software architecture behind building a trading exchange like Zerodha.
Part 1: High-Level Functional Requirements
Before building the architecture, it is essential to understand the functional flow of an exchange from the perspective of a user.
- User Onboarding and KYC: Users must sign up and complete Know Your Customer (KYC) verification (e.g., providing government IDs) to connect a valid Demat account.
- Account Balances: Upon successful registration, the user has a zero balance in both fiat currency (e.g., INR, USD) and assets (e.g., stocks).
- Funding the Account: The user deposits fiat currency from their bank account into the exchange wallet.
- Trading Assets: * Buying: A user can buy a specific quantity of an asset at a given price. The fiat balance is debited, and the asset balance is credited.
- Selling: If the asset appreciates over time, the user can sell it. The asset balance is debited, and the fiat balance is credited with the original capital plus the profit.
- Withdrawal: The user can withdraw their new fiat balance back to their bank account.
Part 2: Core Financial Mechanics and Jargon
To build a trading system, specific financial concepts dictate the logic of the code.
1. Price Determination (Supply and Demand)
The price of any asset on an exchange is strictly determined by supply and demand, not by the company itself.
- If demand is high (more buyers than sellers), buyers outbid each other, driving the price up.
- If supply is high (more sellers than buyers), sellers undercut each other to secure a sale, driving the price down.
- The Current Price: The current price of an asset is defined strictly as the exact price at which the last successful order match (trade) occurred on that specific exchange.
2. The Order Book
The Order Book is a public ledger maintained by the exchange that shows all current, unmatched orders.
- Bids (Buy Side): A list of buyers stating the maximum price they are willing to pay and the quantity they want.
- Asks (Sell Side): A list of sellers stating the minimum price they are willing to accept and the quantity they are selling.