documentation/cookbook/sql/finance/post-trade-overview.md
Post-trade analysis — also called transaction cost analysis (TCA) — measures execution quality after the fact. Market makers use it to detect adverse selection on their resting orders. Buy-side desks use it to evaluate broker and venue performance. Compliance teams use it to demonstrate best execution.
QuestDB is well suited to this workload because TCA is fundamentally a time-series join problem: pair each trade with the state of the order book at the time of execution, then again at various points in the future. The key SQL features used across these recipes are:
Before diving into the recipes, here are the core TCA metrics in the order you'll encounter them:
The recipes build on each other. Slippage answers "how much did I pay?", markout answers "what happened after?", implementation shortfall answers "why did I pay?", venue scoring answers "where should I trade?", and VPIN answers "who is informed?"
Compare each fill to the prevailing order book at the time of execution.
Track post-fill price movement at multiple time horizons.
Decompose total cost into spread, permanent impact, and temporary impact.
Compare execution quality across venues and counterparties to inform routing.
Detect informed trading using volume-synchronized metrics instead of price-based markout.
All recipes use the demo dataset. The two
tables are joined by symbol and aligned by timestamp:
fx_trades — trade executions with symbol, ecn, side, passive,
price, quantity, counterparty, order_id (nanosecond timestamps)market_data — order book snapshots with symbol, bids[][],
asks[][], best_bid, and best_ask (microsecond timestamps).
The bids and asks arrays hold price and size at each level of the book -
[1][1] is the best price, [1][-1] is the price at the deepest level.
The best_bid and best_ask columns provide the top-of-book prices
directly for convenience and efficiency, since most post-trade analytics
queries need only the best priceThe tables use different timestamp resolutions. QuestDB's time-series joins handle mixed-precision timestamps automatically — no explicit casting is needed.
:::info Related documentation