Skip to content

Scenario comparison

The rules behind both operator clients' Compare view (openspec operator-clients, Scenario Comparison): two scenarios side by side, with the difference. The clients read each scenario as their P&L, Risk, Order Book and Live Events views do, and hand what they read to these functions, so the TUI and the web app compute the same comparison:

  • Python: virtufin.domain_devkit.comparison
  • TypeScript: @virtufin/domain-devkit (compareBooks, spread, foldOrders, matchOrders)

The conformance vectors are comparison/*.yaml (shipped in both packages: Python importlib.resources on virtufin.domain_devkit.comparison, TypeScript COMPARISON_VECTORS). Both packages' tests run every vector, and check that swapping the pair negates every difference and mirrors every match.

Amounts

Every amount goes in and comes out as an exact decimal string, never a float: 10.3 minus 10.1 is 0.2. Results are fixed-point at their natural scale (a difference keeps the larger number of fraction digits, a product their sum), with no exponent and never -0. A missing value is null / None.

compare_books / compareBooks

Takes two P&L books as their P&L workers save them (sc.<S>.pnl.book), each symbol optionally carrying notional from the scenario's risk.exposure.update, and returns:

Key Contents
rows One per symbol of either book, sorted: first, second, difference (second minus first) of qty, avg_cost, mark, unrealized_pnl, realized_pnl, notional and fees (per asset)
total realized_pnl, unrealized_pnl, notional and fees summed over the symbols
missing first or second for a book that is not there (no P&L worker), else null
  • A symbol only one book holds counts as zero in the other (quantity, P&L, notional, fees); its average cost and mark are missing there.
  • Without notional, a symbol's notional is the absolute quantity times mark: gross, as the P&L worker writes it (Risk's gross exposure is their sum).
  • With a book missing, there are no differences.

spread

Takes two prices (an order-book mid, a last trade) and returns difference (second minus first) and bp, the difference in basis points of the first to 2 places, half away from zero: 91000.00 to 90950.00 is -50.00, -5.49 bp. A missing price gives no spread; a first price of zero gives no basis points.

fold_orders / foldOrders

Takes one scenario's order events (sc.<S>.trading.order.<event>) in the order received, each {event, order_id, at} with at the payload's own time (submitted_at, ..., filled_at) and the order's fields on submitted. Returns:

Key Contents
orders One per order whose submitted arrived, newest first by decided_at (its submitted_at), with its outcome and its events by time
fills filled events per symbol of their order
earlier_fills Fills of orders whose submitted the client did not see (submitted before it started listening)

An order's outcome is filled (any fill, including a canceled or expired order whose execution filled part of it), else rejected, canceled or expired, else open.

match_orders / matchOrders

Takes both scenarios' orders and the match window (DEFAULT_MATCH_WINDOW_SECONDS, 5) and returns one row per matched pair and per unmatched order, newest first: {first, second, divergence}.

  • Two orders can match when their symbol and side are equal and their decision times are at most the window apart.
  • Pairs are taken nearest first; on a tie, the pair whose earlier order is earlier. Each order matches at most once, so swapping the scenarios gives the same pairs.
  • divergence is unmatched for an order without a match, outcomes for a pair whose outcomes differ, else null.

Decision times are the payloads' own times, never when an event arrived: a replay running ten times faster than real time still lines up on its own market clock, and a replay of a past day matches nothing today.