Design a Stock Portfolio Management Service
Design a service where customers record the stock trades they have made and then see what they hold, what it is worth now, how it moved today and how much they have gained or lost, with every trade counted exactly once.
Problem statement
For this exercise, assume an investing app wants a portfolio service for its customers. Customers record the stock trades they made with their own brokers, and the service shows what they hold and how it is doing. The service does not place orders.
Assume one market, one currency, and customers who are already signed in and can see only their own portfolio. Design the service.
A trade has a symbol (the short code of a stock), a side (buy or sell), a whole number of shares, a price per share and the time of the trade. A customer may record a trade that happened earlier, for example when typing in old trades. The service works out the rest: - Holdings.
For each symbol, the shares held and their average cost per share. A buy sets the average cost to the total paid for the shares held divided by their number. A sale does not change the average cost, and its realized profit or loss is the sale price minus the average cost, times the shares sold. - Value and profit.
For each symbol, the latest price, the value of the shares held, and the unrealized profit or loss, which is the value minus what the shares cost. For the whole portfolio, the total value, the total unrealized profit or loss and the total realized profit or loss. - Day change.
The gain or loss that comes from price moves since the previous close. It is the value now, minus the value at the previous close, minus the net money the customer put in through trades after that close. Buying or selling on its own must never look like a gain or a loss.
Prices come from a market data feed that the service does not own. For each symbol, the feed sends the latest price, which is delayed by a few minutes, the time that price was observed, and the previous close with its time. The feed may have no price for a symbol, and the view must say so instead of hiding it.
Also assume a recorded trade is never edited or deleted. A wrong trade is voided, and a voided trade counts nowhere. Looking at a customer's trades in the order of their trade times, the shares held of a symbol must never fall below zero, so a trade or a void that would break this is refused.
Taxes, fees, dividends, splits, other currencies, cost methods other than average cost, and sharing a portfolio are out of scope.
What to build
- A customer records a buy or a sell with a symbol, a number of shares, a price and a trade time; recording the same trade twice, for example after a retry, counts it once; and a trade that would leave fewer than zero shares held at any moment of trade time is refused.
- A customer voids a wrong trade so that it no longer counts in any holding or profit, and a void that would leave fewer than zero shares held at any moment of trade time is refused.
- A customer opens their portfolio and sees, for each symbol held, the shares, the average cost, the latest price with the time it was observed, the value and the unrealized profit or loss, and for the whole portfolio the total value and the total unrealized and realized profit or loss, with any symbol that has no price clearly marked.
- A customer sees the day change for each symbol and for the whole portfolio, calculated as the value now minus the value at the previous close minus the net money put in through trades after that close.
At scale
- For this exercise, assume about 1 million customers who each hold about 20 symbols on average, 3,000 symbols in total (so about 20 million positions), and that the most popular symbol is held by about 300,000 customers.
- For this exercise, assume up to 500 trades recorded a second and up to 3,000 portfolio views a second in the busiest minute, and that 95 percent of views must be answered within 300 milliseconds.
- For this exercise, assume the feed sends a new price for a symbol at most once every 10 seconds (about 300 price updates a second across all symbols), and that a view may use a price up to one minute older than the feed's newest price as long as it shows when that price was observed.
- For this exercise, assume a customer's next view must show every trade they just recorded or voided, and a recorded trade must never be lost.