Portfolio Management Flow
Orchestration for user-specific portfolios, trade history, and real-time performance tracking.
Overview​
The Portfolio flow manages the lifecycle of a user's financial positions. It tracks Trades (Buys/Sells), computes Cost Basis, and aggregates real-time Market Value and Total Return using live price data.
Position Lifecycle (Sequence)​
Performance Calculation Logic​
The system computes performance metrics on-the-fly directly from the immutable Trades ledger:
- Net Holdings:
SUM(BuyQuantity) - SUM(SellQuantity) - Total Buy Cost:
SUM(BuyQuantity * BuyPrice) - Average Price (Cost Basis):
TotalBuyCost / TotalBuyQuantity - Market Value:
NetHoldings * CurrentMarketPrice - Total Cost:
NetHoldings * AveragePrice - Total Return:
MarketValue - TotalCost - Total Return %:
(TotalReturn / TotalCost) * 100
Note: Portfolio positions are derived exclusively from the immutable
Tradeledger (Tradestable). Watchlist entries (WatchlistItems) are observation-only and do NOT create portfolio positions.
Removal Constraints​
To maintain data integrity, a position can only be removed if:
- It has no active Alert Rules.
- The removal operation deletes the
WatchlistItem(if present) and all associatedTraderecords in a single transaction.
Logic Highlights​
| Feature | Detail |
|---|---|
| Trade-Driven Positions | Positions are queried via _unitOfWork.Trades.GetTradedSymbolsPagedAsync. Un-traded watchlist symbols are excluded. |
| Transaction Safety | Uses ExecuteTransactionAsync to ensure trades are executed safely. |
| Validation | Prevents "Short Selling" by validating net holdings before a Sell trade is recorded. |
| Bulk Import | Supports importing multiple positions via BulkImportPositionsAsync for easy migration. |
| Real-Time Enrichment | Integrates with IStockDataService to provide live P&L (Profit and Loss) metrics. |