Design a Stock Portfolio Profit Tracker
Model holdings, trades and average-cost accounting, compute realized and unrealized profit exactly in whole paise, and keep the cost method replaceable.
Design and implement the core of a stock portfolio tracker for one investor. The investor buys and sells shares, and the tracker shows what they hold, what each holding cost, and how much profit or loss they have made.
For this exercise, assume: - A trade has a symbol, a side (BUY or SELL), a whole number of shares greater than 0, a price per share, and a time. Trades are applied in time order. Money is kept in whole paise (1 rupee = 100 paise), so there are no floating-point amounts. - Each holding uses the average-cost method.
A holding keeps its share count and its total cost. A buy adds the shares and adds shares x price to the total cost. A sell of q shares from a holding of Q shares removes cost equal to total cost x q / Q, rounded half up to a whole paisa, and the rest of the total cost stays with the remaining shares.
- A sell's realized profit is its proceeds (shares x price) minus the cost it removed. Realized profit adds up over all sells. Unrealized profit of a holding is shares x latest price minus its remaining total cost.
- The investor cannot sell more shares than they hold; such a trade, or a trade with a non-positive quantity or price, is rejected and changes nothing. A holding whose shares reach 0 is removed. - Latest prices are given to the tracker by the caller.
Fees, taxes, dividends, splits, short selling, order matching and multiple currencies are out of scope.
After the design, implement applying a trade and computing the portfolio's realized and unrealized profit in code.