| [d8ce4e2] | 1 | # Use-case 0005 — Place market SELL order
|
|---|
| 2 |
|
|---|
| 3 | **Initiating actor:** Trader
|
|---|
| 4 |
|
|---|
| 5 | **Other actors:** Market Simulator (indirect — supplies the current price).
|
|---|
| 6 |
|
|---|
| 7 | A Trader sells part or all of a holding at the current market price. Cost basis is preserved so realised P/L can be reconstructed from the ledger.
|
|---|
| 8 |
|
|---|
| 9 | ## Scenario
|
|---|
| 10 |
|
|---|
| 11 | 1. Trader chooses "Place market SELL order".
|
|---|
| 12 | 2. System lists markets (same SQL as UC0004 step 2).
|
|---|
| 13 | 3. Trader enters market symbol and quantity.
|
|---|
| 14 | 4. System resolves the market and looks up the latest price (same SQL as UC0004 step 4).
|
|---|
| 15 | 5. System opens a transaction:
|
|---|
| 16 |
|
|---|
| 17 | ```sql
|
|---|
| 18 | BEGIN;
|
|---|
| 19 |
|
|---|
| 20 | INSERT INTO project.orders
|
|---|
| 21 | (user_id, market_id, side, type, status, quantity, price, executed_at)
|
|---|
| 22 | VALUES
|
|---|
| 23 | ($user_id, $market_id, 'sell', 'market', 'executed', $qty, $price, now())
|
|---|
| 24 | RETURNING id; -- $order_id
|
|---|
| 25 |
|
|---|
| 26 | SELECT quantity, avg_price
|
|---|
| 27 | FROM project.holdings
|
|---|
| 28 | WHERE user_id = $user_id AND crypto_id = $crypto_id
|
|---|
| 29 | FOR UPDATE;
|
|---|
| 30 | -- abort if row missing or quantity < $qty
|
|---|
| 31 | ```
|
|---|
| 32 | 6. If the holding check passes, system reduces the holding, credits cash and debits invested, and appends a ledger and a market trade:
|
|---|
| 33 |
|
|---|
| 34 | ```sql
|
|---|
| 35 | UPDATE project.holdings
|
|---|
| 36 | SET quantity = quantity - $qty,
|
|---|
| 37 | updated_at = now()
|
|---|
| 38 | WHERE user_id = $user_id AND crypto_id = $crypto_id;
|
|---|
| 39 |
|
|---|
| 40 | UPDATE project.users
|
|---|
| 41 | SET available_balance = available_balance + $notional,
|
|---|
| 42 | invested_balance = GREATEST(invested_balance - ($avg_price * $qty), 0),
|
|---|
| 43 | updated_at = now()
|
|---|
| 44 | WHERE id = $user_id;
|
|---|
| 45 |
|
|---|
| 46 | INSERT INTO project.transactions
|
|---|
| 47 | (user_id, type, amount, currency, related_order, description)
|
|---|
| 48 | VALUES
|
|---|
| 49 | ($user_id, 'sell', $notional, 'USD', $order_id, 'Market sell ...');
|
|---|
| 50 |
|
|---|
| 51 | INSERT INTO project.market_trades
|
|---|
| 52 | (market_id, executed_at, price, quantity, side, source)
|
|---|
| 53 | VALUES
|
|---|
| 54 | ($market_id, now(), $price, $qty, 'sell', 'user');
|
|---|
| 55 |
|
|---|
| 56 | COMMIT;
|
|---|
| 57 | ```
|
|---|
| 58 | 7. System confirms: `Order executed: sell 0.5000 ETH @ 3520.000000 (notional 1760.0000 USD)`.
|
|---|
| 59 |
|
|---|
| 60 | ### Alternate flow 5a — insufficient holding
|
|---|
| 61 |
|
|---|
| 62 | If the `SELECT ... FOR UPDATE` returns no row, or the held quantity is smaller than the sell quantity, the entire transaction rolls back and system shows "Insufficient holding: trying to sell X, hold Y."
|
|---|
| 63 |
|
|---|
| 64 | ### Realised P/L (post-scenario)
|
|---|
| 65 |
|
|---|
| 66 | The realised P/L for a sell is `$notional - ($avg_price * $qty)`. It is not persisted explicitly but can be computed from the ledger and the holding at sell time.
|
|---|