Metrics¶
Every strategy in a run has its own Portfolio (what
it owns and what it made) and ExecutionMetrics
(how well it traded). strategy_report()
combines them with the run's price path into one flat row per strategy.
from microbook import Side
from microbook.sim import MarketConfig, MarketMaker, TWAPExecutor, build_market, format_table, strategy_report
mm = MarketMaker("mm", half_spread_ticks=1, size=5)
twap = TWAPExecutor(Side.BUY, total_qty=500, start=0, end=10_000, tick_interval=100, name="twap")
market = build_market(MarketConfig(seed=4), [mm, twap], horizon=10_000)
sim = market.run(10_000)
report = strategy_report(sim, ["mm", "twap"], horizons=(1, 100))
print(format_table(report, ["final", "volume", "maker_share", "spread_capture", "markout_1", "markout_100"]))
# final volume maker_share spread_capture markout_1 markout_100
# ---- --------- ------ ----------- -------------- --------- -----------
# mm 2.7400 1336 1.0000 0.0055 0.0041 0.0032
# twap -228.6900 500 0.0000 - -0.0048 -0.0110
Prices and per-share metrics are in price units; with the default 0.01 tick,
0.0055 is 0.55 ticks. A - means the metric is undefined: the TWAP never rests
an order, so it has no spread capture.
PnL and inventory¶
Portfolio uses average-cost accounting. Buying adds
to the position at a blended average cost; selling against the position
realises PnL on the closed quantity. Total PnL marks the open position at the
midprice, net of fees:
Fees are per share, with separate maker and taker rates, and negative for a
rebate (for example Portfolio(maker_fee=-0.002, taker_fee=0.003)). The test
suite checks total_pnl(mark) == cash − initial_cash + position × mark for
random fill sequences.
The simulator samples each strategy's total PnL and position at every
SNAPSHOT. From those series:
| Column | Definition |
|---|---|
final |
total PnL at the last snapshot |
max_drawdown |
largest fall from a running peak of PnL |
pnl_vol |
standard deviation of PnL changes between snapshots |
sharpe |
mean ÷ standard deviation of those changes × √(number of changes): a signal-to-noise ratio for the whole run, not annualised |
mean_abs_inventory, max_abs_inventory, final_inventory |
the position, in shares |
In the table above, the TWAP lost 228.69 because the price fell while it was buying 500 shares: that is market exposure, not bad execution. The execution metrics below separate the two.
Execution quality¶
| Column | Definition |
|---|---|
volume |
shares filled, buys plus sells |
maker_share |
share of that volume filled as the resting order |
fill_ratio |
filled ÷ submitted quantity |
participation |
the strategy's share of all volume traded in the market |
fees |
fees paid, net of rebates |
ExecutionMetrics has more, such as VWAPs by
side and slippage, in summary().
Slippage against arrival¶
When a strategy sends an order, the simulator records the midprice at the decision time as its arrival price. Slippage is the average fill price minus the arrival price for buys, and the arrival price minus the average fill price for sells, weighted by quantity, so a positive number is a cost. Because arrival is taken at decision time, slippage includes everything the price did during the strategy's latency, like implementation shortfall.
m = sim.exec_metrics["twap"]
print(f"bought {m.buy_qty} at {m.buy_vwap():.4f}, slippage vs arrival {m.buy_slippage():.4f}")
# bought 500 at 99.9024, slippage vs arrival 0.0051
The TWAP paid about half a tick per share over the mid at each decision: half the spread, the price of crossing it with market orders.
Spread capture¶
For maker fills only: how far the fill price was from the midprice just before the trade, per share, in the strategy's favour. A market maker quoting one tick either side of a one-tick-wide mid earns up to half a tick here.
Markouts and adverse selection¶
A markout asks: some time after the trade, was it a good one? For a fill of
qty shares at price p and time t, the markout at horizon h is
per share, weighted by quantity across fills, from the strategy's side. Positive
means the trade was profitable when marked h steps later. Fills whose horizon
runs past the end of the run are left out.
For a liquidity provider the markout at a short horizon is close to the spread
it earned. As the horizon grows, the markout falls, because the fills that came
from informed traders are followed by prices moving against them. The decay of
markouts with horizon measures adverse selection.
markouts() computes the curve for any list of fills:
from microbook.sim import markouts
curve = markouts(sim.exec_metrics["mm"].fills, sim.result.mid_path, (1, 10, 100, 1000), end_time=sim.now)
print({h: round(v / 0.01, 2) for h, v in curve.items()}) # in ticks
# {1: 0.41, 10: 0.3, 100: 0.32, 1000: 0.22}
The market maker keeps about 0.4 ticks per share one step after a fill, and about half of that 1,000 steps later. A single seed is noisy, which is why the 10- and 100-step values are out of order. Comparing strategies averages markouts over many seeds and shows how latency and informed flow change the curve.
The book itself¶
sim.ts samples the book at every snapshot: the
midprice, the spread, and the top-3 depth imbalance
(imbalance()): (bid size − ask size) ÷ (bid size + ask size)
over the best three levels, in [−1, 1]. Imbalance predicts the next mid move
and is one of the signals AdaptiveMarketMaker
leans on.