5 min read

Normalisation for trading data: orders, executions and reference data

The normal forms applied to an order and execution schema, where time changes what counts as a dependency, why positions are a projection and not a table to update, and the places where denormalising is the correct design.


Normalisation is usually taught with customers and products, where its only visible benefit is tidiness. In a trading database the benefit is specific: every fact stored twice is a reconciliation break waiting to happen, and breaks in order, execution and position data are found by operations, by the client or by the regulator.

This note walks the normal forms through an order and execution schema, then covers the two things the textbook treatment leaves out: time, and the places where denormalising is correct.

First to third normal form on an order table

Start with the table that tends to grow out of a first prototype:

order_id | symbol | side | qty | trader | desk    | fill1_qty | fill1_px | fill2_qty | fill2_px
1001     | VOD.L  | B    | 300 | asmith | EQ-CASH | 100       | 72.10    | 200       | 72.08

1NF: no repeating groups. The numbered fill columns cap the number of fills, make "total filled" a different query for every order, and cannot hold what a fill really carries (its own id, time, venue, liquidity flag). An order has many executions; that is a child table.

2NF: no dependency on part of a key. Once executions are keyed by (order_id, exec_seq), any column that depends on order_id alone (symbol, side, trader) does not belong on the execution row. Copying it there is how an amended order ends up with fills that disagree with it.

3NF: no dependency through another non-key column. desk depends on trader, not on the order. Stored on the order, a desk rename touches millions of rows, and a missed row is an order attributed to a desk that no longer exists.

The result:

CREATE TABLE instrument (
  instrument_id INTEGER PRIMARY KEY,
  isin          TEXT NOT NULL UNIQUE
);

CREATE TABLE orders (
  order_id      INTEGER PRIMARY KEY,
  instrument_id INTEGER NOT NULL REFERENCES instrument,
  side          TEXT    NOT NULL CHECK (side IN ('B', 'S')),
  quantity      INTEGER NOT NULL CHECK (quantity > 0),
  limit_price   NUMERIC,
  created_at    TIMESTAMP NOT NULL
);

CREATE TABLE execution (
  exec_id     TEXT PRIMARY KEY, -- assigned by the venue
  order_id    INTEGER NOT NULL REFERENCES orders,
  quantity    INTEGER NOT NULL CHECK (quantity > 0),
  price       NUMERIC NOT NULL,
  executed_at TIMESTAMP NOT NULL
);

Two details matter more than the forms. The execution's primary key is the venue's execution id, which makes inserts idempotent: a replayed drop copy cannot create a second fill. And prices are NUMERIC, never binary floating point, because 0.1 + 0.2 is not 0.3 and a ledger has to add up.

Time changes what a dependency is

The textbook says: trader → desk, so store the desk once on the trader. But traders move desks, and the order placed in March belongs to the March desk for P&L attribution, for limits and for surveillance. The dependency that actually holds is (trader, time) → desk.

Reference data has the same structure everywhere. Tickers are reused, tick sizes and lot sizes change, instruments move between segments, corporate actions rewrite identifiers. A single instrument row with the current values cannot answer the question "what was the tick size when this order was validated?", and every historical report silently changes when the row is updated.

The normalised answer is to version the facts that vary, and join on time:

CREATE TABLE instrument_version (
  instrument_id INTEGER NOT NULL REFERENCES instrument,
  valid_from    TIMESTAMP NOT NULL,
  valid_to      TIMESTAMP NOT NULL, -- exclusive; far future when current
  symbol        TEXT    NOT NULL,
  tick_size     NUMERIC NOT NULL,
  lot_size      INTEGER NOT NULL,
  PRIMARY KEY (instrument_id, valid_from),
  CHECK (valid_from < valid_to)
);

SELECT e.exec_id, v.symbol, v.tick_size, e.quantity, e.price
FROM execution e
JOIN orders o USING (order_id)
JOIN instrument_version v
  ON v.instrument_id = o.instrument_id
 AND e.executed_at >= v.valid_from
 AND e.executed_at <  v.valid_to
ORDER BY e.executed_at;

With an instrument whose symbol changes from OLD to NEW and whose tick size halves on 1 June, executions on either side of the date pick up the version that applied to them:

E1 | OLD | 0.01  | 100 | 10.00
E2 | OLD | 0.01  | 200 | 9.99
E3 | NEW | 0.005 |  50 | 10.105

The half-open interval is deliberate: with valid_to exclusive, an event exactly on the boundary matches one version, never two or none. A database that supports exclusion constraints can also enforce that an instrument's versions do not overlap, which is worth having.

It follows that copying a value onto a transaction is not always denormalisation. The execution price, the account an order was booked to, and the limit that was applied are facts about the event at the time it happened. They depend on the event's key and belong on its row. The test is whether the value should change when the reference data is corrected. If the answer is no, it is the event's own attribute.

Two time axes

Valid time is not enough for anything that is audited. A late correction ("that trade was booked to the wrong account, as of last Tuesday") needs to change what is true about last Tuesday without destroying the record of what the system believed on Wednesday, because reports and client statements were produced from that belief. That requires a second axis: the time at which the database recorded each row. Corrections are inserted with a new recording time and nothing is updated in place. Bitemporal tables cost storage and query complexity; they are what makes "reproduce the report as it was sent" a query instead of an investigation.

Derived data: positions are a projection

Leaves quantity, average price, order status and positions are all functions of the executions:

SELECT o.order_id,
       o.quantity,
       COALESCE(SUM(e.quantity), 0)              AS filled,
       o.quantity - COALESCE(SUM(e.quantity), 0) AS leaves
FROM orders o
LEFT JOIN execution e USING (order_id)
GROUP BY o.order_id, o.quantity;

Storing them as columns updated alongside each fill creates a second source of truth and, sooner or later, a row where filled disagrees with the sum of the fills. Since the executions are immutable, the safer design is to treat them as the ledger and everything else as a projection of it.

That does not mean computing positions with SUM at query time on a busy system. It means that when a position table exists for speed, it is maintained by one writer from the execution stream, can be rebuilt from zero, and is checked against a full recomputation on a schedule. A derived table that cannot be rebuilt is a second ledger.

The higher normal forms earn their keep less often. BCNF violations appear with overlapping candidate keys, for instance several identifier schemes for one instrument. Fourth normal form is what stops independent multi-valued facts, such as the venues an instrument trades on and the indices it belongs to, being stored as a cross product in one table.

Where denormalising is the right design

Tick and bar storage. Market data is append-only and never updated, queried by time range and aggregated over billions of rows. Columnar time-series stores keep it in wide tables sorted by time, and combine tables with as-of joins. Update anomalies, the thing normalisation prevents, cannot occur in data that is never updated.

Published snapshots. End-of-day positions, client statements and regulatory reports are records of what was reported. They are stored as produced and not re-derived when reference data changes.

The hot path. An order gateway does not join tables to validate an order. It holds a flat in-memory structure per instrument, loaded from the normalised store, and treats the database as the system of record, not as part of the request.

What these have in common is that the denormalised copy is either immutable or rebuildable from a normalised source that remains authoritative.

Summary

Normalise the transactional core: instruments, orders, executions, accounts. Put time into reference data so that dependencies say what was true when. Keep executions immutable and treat positions as a projection. Denormalise where data is append-only, frozen for the record, or a rebuildable cache.