Problem · Database

Track the Book and Mark Final PnL

MediumMillennium logoMillenniumNEW GRADINTERNOA

Problem statement

A market-making desk fills client requests and tracks its cash and inventory. Every row in fills is one completed client trade.

Book updates

  • For a client BUY, the desk sells the quantity: desk inventory decreases and cash increases by quantity * fill_price.
  • For a client SELL, the desk buys the quantity: desk inventory increases and cash decreases by quantity * fill_price.

The closing_prices table represents the final price row, with exactly one closing mark for every traded asset. After all fills, compute pnl = cash + sum(inventory[asset] * mark_price[asset]).

Return exactly one row with the column pnl.

Table schema

MySQLPostgreSQLPandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

fills

ColumnTypeNullableDescription
fill_idPKIntegerNo
assetTextNo
client_sideTextNo
quantityIntegerNo
fill_priceDecimalNo

closing_prices

ColumnTypeNullableDescription
assetTextNo
mark_priceDecimalNo

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
pnlDecimalNo

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • 0 <= fills.row_count <= 2000.
  • Every fill_id is unique.
  • Every client_side is exactly BUY or SELL.
  • Every quantity is an integer in [1, 1000000].
  • Every fill and closing price is an exact decimal in (0, 1000000000000] with at most 6 fractional digits.
  • closing_prices contains exactly one row for each asset that occurs in fills; it may also contain untraded assets.
  • Each fill notional is at most 10^18; the absolute cash and marked-inventory totals are each at most 2 * 10^21, and |pnl| <= 4 * 10^21. These bounds keep every exact intermediate and result within the shared DECIMAL(38, 12) / 38-digit decimal runtime contract.
  • Use exact decimal arithmetic and return 0 when there are no fills.

More Millennium problems