Market Share Ticker with Imbalance
Problem statement
An exchange publishes market trades, and one participant separately records the subset of trades in which it participated. A symbology table maps exchange product identifiers to ticker symbols.
For every product in which the participant traded, compute its market share as the participant's total traded quantity divided by the exchange's total traded quantity for that product. Select the product with the largest exact market-share ratio.
For the selected product, compute the participant's closing imbalance: total quantity on buy rows (B) minus total quantity on sell rows (S).
Return exactly one row with columns product and imbalance, in that order. FastPrep represents the source routine's two-key dictionary as this one-row result table.
Table schema
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.
market_trades
One row per market transaction.
| Column | Type | Nullable | Description |
|---|---|---|---|
| product_id | Integer | No | Exchange product identifier. |
| trade_idPK | Integer | No | Unique exchange transaction identifier. |
| quantity | Integer | No | Total exchange shares in the transaction. |
participant_trades
The participant's subset of exchange transactions.
| Column | Type | Nullable | Description |
|---|---|---|---|
| product | Text | No | Ticker symbol. |
| trade_idPK | Integer | No | Referenced exchange transaction identifier. |
| side | Text | No | B for buy or S for sell. |
| quantity | Integer | No | Shares executed by the participant. |
Foreign key: trade_id → market_trades(trade_id)
symbology
Mapping from ticker symbols to exchange product identifiers.
| Column | Type | Nullable | Description |
|---|---|---|---|
| product | Text | No | Ticker symbol. |
| product_idPK | Integer | No | Exchange product identifier. |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| product | Text | No | — |
| imbalance | Integer | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
market_trades.trade_id,symbology.product, andsymbology.product_idare unique.- Every participant row references one market trade, and its
productmatches that trade's mapped symbology product. Products with no participant row are not candidates. - Every participant product has a positive total exchange quantity. Market and participant quantities are positive integers, and all required sums fit a signed 64-bit integer.
sideis exactlyBorS, and at least one participant row is present.- Compare market-share ratios without rounding. If products tie, return the lexicographically smaller uppercase ASCII
product.