Loading LLD design...
Design an object-oriented stock trading platform (like Zerodha) with trading accounts, stock listings with OHLCV data, multiple order types (Market, Limit, Stop-Loss, Stop-Limit), a price-time priority order book (matching engine), trade execution with fund settlement, and portfolio management with weighted-average cost P&L.
The platform supports margin management: BUY orders block funds upfront, SELL orders validate sufficient holdings. The order book matches the highest bid against the lowest ask using price-time priority (FIFO within the same price level). Partial fills keep orders open for the remaining quantity. Each trade computes brokerage (₹20 flat or 0.03%), STT, exchange fees, GST, and stamp duty.
Account management
Create trading + demat account; deposit and withdraw funds
Stock listing
List stocks with symbol, name, exchange, OHLCV (Open/High/Low/Close/Volume)
Place order
Place Market, Limit, Stop-Loss, or Stop-Limit orders for BUY or SELL
Margin management
Block balance as margin for BUY orders; validate holdings for SELL orders
Order matching
Price-time priority order book: highest bid meets lowest ask, FIFO within same price
Trade execution
Record trades, update holdings (add/remove), settle funds, update LTP
Portfolio & P&L
Holdings with weighted-average buy price, current value, P&L, P&L%
Cancel/modify order
Cancel open orders (release margin); modify via cancel-and-replace
Brokerage calculation
Brokerage (₹20 or 0.03%), STT, exchange fees, GST, stamp duty per trade
Before diving into code, clarify the use cases and edge cases. Understanding the problem deeply leads to better class design.
Identify the primary actions users will perform. For a parking lot: park vehicle, exit vehicle, check availability. Each becomes a method.
Who interacts with the system? Customers, admins, automated systems? Each actor type may need different interfaces.
What are the limits? Max vehicles, supported vehicle types, payment methods. Constraints drive your data structures.
What happens on overflow? Concurrent access? Payment failures? Thinking about edge cases reveals hidden complexity.