Ignore:
Timestamp:
09/24/26 17:43:19 (5 days ago)
Author:
Stefan <trsunovstefan@…>
Branches:
main
Children:
0cee8ec
Parents:
a531b45
Message:

Wiki docs, phase 6 and phase 7 added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/P1-ConceptualModel/ERModel.md

    ra531b45 ref1c1c7  
    1 # Entity-Relationship Model v.03
     1# Entity-Relationship Model v.04
    22
    33## Diagram
    44
    5 ![ERModel_v03](ERModel_v03.png)
     5![ERModel_v04](ERModel_v04.png)
    66
    77Notation: Chen. Rectangles are entity sets, diamonds are relationships, ellipses
    … …  
    5151| `available_balance` | numeric(18,4) | required, default 0, ≥ 0 |
    5252| `invested_balance` | numeric(18,4) | required, default 0, ≥ 0 |
     53| `reserved_balance` | numeric(18,4) | required, default 0, ≥ 0 — cash set aside for the user's open buy orders (added in v04, after P7) |
    5354| `created_at` | timestamptz | required, defaults to now |
    5455| `updated_at` | timestamptz | optional (null until first change) |
    … …  
    9697makes the ledger auditable.
    9798
    98 Placing an order is what triggers a **reservation** of whatever it commits —
    99 the crypto being sold (`Holds.reserved_quantity`, below) on a sell, cash
    100 already handled the same way on a buy via `available_balance` /
    101 `invested_balance`. `status` therefore has real meaning as a lifecycle, not
    102 just a label: `open` means reserved but not yet settled, `executed` means
    103 settled, `cancelled` would release the reservation without settling (not yet
    104 exercised by any use case, since only market orders — which settle
    105 immediately — are implemented). See
     99Placing an order is what triggers a **reservation** of whatever it commits:
     100the crypto being sold (`Holds.reserved_quantity`, below) on a sell, and the
     101cash (`Users.reserved_balance`) on a buy. Since v04 (after P7) an order can
     102wait in the order book and be filled in parts, so `status` is a real
     103lifecycle driven by `filled_quantity`: `open` (nothing filled yet),
     104`partially_filled`, `executed` (completely filled), or `cancelled`, which
     105releases what is still reserved. See
    106106[UseCase0005](../P3-UseCaseModel/UseCase0005.md) for the reserve-then-settle
    107 sequence.
     107sequence and
     108[AdvancedDatabaseDevelopment](../P7-AdvancedDatabaseDevelopment/AdvancedDatabaseDevelopment.md)
     109for the rules that keep it consistent.
    108110
    109111**Keys:** candidate `{id}` only — there is no natural key, since the same user
    … …  
    115117| `id` | UUID | PK, required |
    116118| `side` | text | required, `buy` or `sell` |
    117 | `type` | text | required, `market` or `limit` — the prototype executes only `market`; `limit` exists so the model does not have to change when limit orders are implemented |
    118 | `status` | text | required, `open`, `executed` or `cancelled` |
     119| `type` | text | required, `market` or `limit` (both executed since P7) |
     120| `status` | text | required, `open`, `partially_filled`, `executed` or `cancelled` |
    119121| `quantity` | numeric(20,4) | required, > 0 |
    120 | `price` | numeric(18,6) | optional — null until the order settles, then the fill price |
     122| `filled_quantity` | numeric(20,4) | required, default 0, between 0 and `quantity` — how much has been traded; remaining = `quantity − filled_quantity` (added in v04, after P7) |
     123| `price` | numeric(18,6) | the limit price; for a market order, the market price when it was placed |
    121124| `placed_at` | timestamptz | required, defaults to now |
    122125| `executed_at` | timestamptz | optional, set when the order settles |
    … …  
    160163| `source` | text(50) | required, default `simulation` — distinguishes a simulated trade from a user's own fill (`user`) |
    161164
     165Since v04 (after P7) a trade also records which orders it filled, through the
     166relationships `FillsBuy` and `FillsSell` below.
     167
     168#### OrderEvents
     169*Added in v04, after P7.* The audit trail of an order: one event for its
     170placement, one for every (partial) fill, and one for a cancellation. The
     171`Orders` row only holds the current state; this entity keeps the history of
     172how the order got there. Events are recorded automatically by the database.
     173
     174**Keys:** candidate `{id}` only; primary key **`id`** (auto-incrementing
     175integer, events are only read in order).
     176
     177| Attribute | Type | Constraints |
     178|---|---|---|
     179| `id` | integer | PK, required, auto-generated |
     180| `event_type` | text | required, `placed`, `partially_filled`, `filled` or `cancelled` |
     181| `quantity` | numeric(20,4) | required — the ordered quantity for `placed`, the filled amount for a fill, the unfilled rest for `cancelled` |
     182| `price` | numeric(18,6) | optional — the order price, or the trade price for a fill |
     183| `status_after` | text | required, the order's status after the event |
     184| `created_at` | timestamptz | required, defaults to now |
     185
    162186#### MarketCandles
    163187OHLCV aggregates per market and timeframe — the data a price chart is drawn
    … …  
    222246#### Fills — Markets (1) : MarketTrades (N), total on MarketTrades
    223247Every executed trade happened on exactly one market. No attributes.
     248
     249#### FillsBuy — Orders (1) : MarketTrades (N), partial on both sides
     250*Added in v04, after P7.* The buy order a trade filled. An order can be
     251filled by many trades (partial fills); a trade fills at most one buy order,
     252and none when the simulated market was the buyer. No attributes.
     253
     254#### FillsSell — Orders (1) : MarketTrades (N), partial on both sides
     255*Added in v04, after P7.* The sell order a trade filled, symmetric to
     256`FillsBuy`. A trade between two users' orders participates in both. No
     257attributes.
     258
     259#### Logs — Orders (1) : OrderEvents (N), total on OrderEvents
     260*Added in v04, after P7.* Every event belongs to exactly one order. No
     261attributes.
    224262
    225263#### Aggregates — Markets (1) : MarketCandles (N), total on MarketCandles
    … …  
    290328  [UseCase0005](../P3-UseCaseModel/UseCase0005.md) for how the new attribute
    291329  is enforced.
     330- **v04 — after P7.** Phase 7 (order, balance and trade consistency) needed
     331  data the model did not have, so the model was extended to stay in line with
     332  the database:
     333  - `Users.reserved_balance`: cash reserved by open buy orders;
     334  - `Orders.filled_quantity` and the status value `partially_filled`: orders
     335    can now be filled in parts;
     336  - the relationships `FillsBuy` and `FillsSell` between `Orders` and
     337    `MarketTrades`: which orders a trade filled;
     338  - the entity set `OrderEvents` with the relationship `Logs`: the
     339    automatically recorded history of every order.
     340
     341  Nothing existing was removed or changed. See
     342  [AdvancedDatabaseDevelopment](../P7-AdvancedDatabaseDevelopment/AdvancedDatabaseDevelopment.md).
     343  The diagram files are `ERModel_v04.xml` / `ERModel_v04.png`; earlier versions
     344  are kept.
    292345
    293346Reasoning for the AI-assisted part of this phase, and the full interaction log,
    294347are on [ERModelAIUsage](ERModelAIUsage.md).
    295348
    296 > **Student action required.** Open `ERModel_v03.xml` in TerraER, read the
    297 > whole diagram — not just the new `reserved_quantity` ellipse — and change
    298 > anything you disagree with, including the compaction. The phase rules
    299 > require the model to be yours; this is a generated revision to review and
    300 > take over, not an answer to submit unread.
Note: See TracChangeset for help on using the changeset viewer.