source: docs/P3-UseCaseModel/UseCase0006.md

main
Last change on this file was 9577c79, checked in by Stefan <trsunovstefan@…>, 13 days ago

add reserved_quantity and modify the phases, add v_03.png and v_03.xml for P1

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[d8ce4e2]1# Use-case 0006 — View portfolio and transaction history
2
3**Initiating actor:** Trader
4
5**Other actors:** —
6
7A Trader inspects their current holdings, unrealised P/L, cash balance and recent ledger.
8
9## Scenario
10
11### Portfolio
12
131. Trader chooses "View portfolio".
142. System queries the `v_portfolio` view:
15
16 ```sql
17 SELECT symbol,
18 quantity,
[9577c79]19 COALESCE(reserved_quantity, 0),
20 COALESCE(available_quantity, quantity),
[d8ce4e2]21 COALESCE(avg_price, 0),
22 COALESCE(current_price, 0),
23 COALESCE(market_value, 0),
24 COALESCE(unrealized_pnl, 0)
25 FROM project.v_portfolio
26 WHERE user_id = $1
27 AND quantity > 0
28 ORDER BY symbol;
29 ```
[9577c79]30
31 `reserved_quantity` is the amount committed to the Trader's own open sell
32 orders (see [UseCase0005](UseCase0005.md)); `available_quantity` is what is
33 actually free to sell right now.
[d8ce4e2]343. System displays the rows and a computed summary:
35
36 ```sql
37 SELECT available_balance, invested_balance
38 FROM project.users
39 WHERE id = $1;
40 ```
41
42### Transaction history
43
441. Trader chooses "View transaction history".
452. System queries the last 20 ledger entries:
46
47 ```sql
48 SELECT created_at, type, amount, currency, COALESCE(description, '')
49 FROM project.transactions
50 WHERE user_id = $1
51 ORDER BY created_at DESC
52 LIMIT 20;
53 ```
54
55### Reference — how `v_portfolio` is defined
56
57```sql
58CREATE OR REPLACE VIEW project.v_portfolio AS
59SELECT h.user_id,
60 c.symbol,
61 h.quantity,
[9577c79]62 h.reserved_quantity,
63 (h.quantity - h.reserved_quantity) AS available_quantity,
[d8ce4e2]64 h.avg_price,
65 lp.price AS current_price,
66 (h.quantity * lp.price) AS market_value,
67 (h.quantity * (lp.price - h.avg_price)) AS unrealized_pnl
68 FROM project.holdings h
69 JOIN project.crypto c ON c.id = h.crypto_id
70 LEFT JOIN project.markets m
71 ON m.crypto_id = c.id AND m.quote_currency = 'USD'
72 LEFT JOIN project.v_latest_prices lp
73 ON lp.market_id = m.id;
74```
Note: See TracBrowser for help on using the repository browser.