Skip to content

Comparing strategies

One simulated run is one random day. To say that one strategy is better than another, you need many days, an error bar, and a comparison that is not swamped by the day-to-day noise of the market. This guide shows the method that examples/compare_market_makers.py uses for the results in the README.

Many seeds, same days

Run each strategy on the same set of seeds. build_market() derives the fundamental path and every background agent's arrivals from MarketConfig.seed alone, so two strategies run with the same seed face the same news and the same exogenous order flow (common random numbers).

import math
import statistics

from microbook.sim import AdaptiveConfig, AdaptiveMarketMaker, MarketConfig, MarketMaker, build_market, strategy_report

SEEDS = range(1, 7)
HORIZON = 5_000


def symmetric(latency=0):
    return MarketMaker("mm", half_spread_ticks=1, size=5, latency=latency)


def adaptive(latency=0):
    cfg = AdaptiveConfig(base_half_spread_ticks=0.5, inv_k=0.3, inv_limit=50, vol_k=1.0, imb_k=1.0, latency=latency)
    return AdaptiveMarketMaker(cfg, name="mm")


def run(strategy, seed, **market):
    sim = build_market(MarketConfig(seed=seed, **market), [strategy], horizon=HORIZON).run(HORIZON)
    return strategy_report(sim, ["mm"])["mm"]


def mean_se(xs):
    return statistics.fmean(xs), statistics.stdev(xs) / math.sqrt(len(xs))


pnl = {
    "symmetric": [run(symmetric(), seed)["final"] for seed in SEEDS],
    "adaptive": [run(adaptive(), seed)["final"] for seed in SEEDS],
}
for name, xs in pnl.items():
    print("{:9}  PnL {:.2f} ± {:.2f}".format(name, *mean_se(xs)))
# symmetric  PnL 1.19 ± 0.17
# adaptive   PnL 1.58 ± 0.10

A strategy object keeps state (its portfolio, its quotes), so build a fresh one for every run.

Pair the runs

The two error bars overlap, but that understates what the experiment shows. Because both strategies saw the same days, compare them day by day:

diff = [a - s for a, s in zip(pnl["adaptive"], pnl["symmetric"])]
print("adaptive - symmetric: {:.2f} ± {:.2f}".format(*mean_se(diff)))
# adaptive - symmetric: 0.39 ± 0.11

The paired standard error (0.11) is about half of what two independent samples would give (√(0.17² + 0.10²) ≈ 0.20): the market's own noise cancels out. With six short days, the adaptive market maker is ahead by more than three standard errors.

Latency

A market maker's quotes are only as good as their freshness. Rerun with an order-entry latency of 20 steps:

slow = [run(adaptive(latency=20), seed)["final"] for seed in SEEDS]
print("latency 20: PnL {:.2f} ± {:.2f}".format(*mean_se(slow)))
# latency 20: PnL -0.83 ± 0.39

The same strategy now loses money. Its quotes sit in the book after the price has moved, and the informed traders pick them off. Over 16 seeds of 20,000 steps, the full experiment gives:

Latency (steps) 0 5 20 50
PnL 5.04 ± 0.21 1.59 ± 0.26 −7.27 ± 0.59 −18.63 ± 1.19
Markout @100 (ticks) 0.35 0.33 0.18 −0.26

At 50 steps the 100-step markout turns negative: the maker loses on its fills on average, before counting inventory.

Adverse selection

To see where the money goes, look at markouts across horizons rather than at PnL alone, averaged over the seeds:

from microbook.sim import markouts

horizons = (1, 10, 100, 1_000)
curves = []
for seed in SEEDS:
    mm = adaptive()
    sim = build_market(MarketConfig(seed=seed), [mm], horizon=HORIZON).run(HORIZON)
    curves.append(markouts(sim.exec_metrics["mm"].fills, sim.result.mid_path, horizons, end_time=sim.now))
print({h: round(statistics.fmean(c[h] for c in curves) / 0.01, 2) for h in horizons})
# {1: 0.52, 10: 0.45, 100: 0.36, 1000: 0.22}

Shortly after a fill the maker is up about half a tick per share, the spread it earned. The further out you mark, the more of that is gone: fills against informed traders are followed by prices moving against the maker. The decay is adverse selection, and it grows with the share of informed flow:

Informed arrivals per step 0.0 0.2 0.4 0.8
PnL 5.50 ± 0.09 6.78 ± 0.13 5.04 ± 0.21 0.52 ± 0.22
Sharpe 16.3 12.4 6.9 0.5
Markout @100 (ticks) 0.48 0.41 0.35 0.16

Informed flow cuts both ways: it brings volume, so PnL first rises, but the Sharpe ratio and the markouts fall steadily, and at high intensity adverse selection overwhelms the spread. To run a sweep like this, vary the market: run(adaptive(), seed, informed=InformedConfig(rate=0.8)).

Execution algorithms

The same method measures execution quality for a taker. Give an execution strategy such as TWAPExecutor a parent order, run it on the same seeds, and compare buy_slippage() or sell_slippage() in ExecutionMetrics across variants: slippage is measured against the mid at decision time, so it charges for latency too. Remember that PnL also contains the market's move during execution; for an execution algorithm, slippage is the number to compare.

The full experiment

python examples/compare_market_makers.py                        # 16 seeds x 20,000 steps, ~30 s
python examples/compare_market_makers.py --plot docs/figures    # and the figures
python examples/compare_market_makers.py --seeds 4 --horizon 5000  # a quick run

Market-maker experiments