DEV Community

Cover image for Backtesting a Market Making Strategy: Simulating Spread, Depth & Toxic Order Flow
Emir Taner
Emir Taner

Posted on

Backtesting a Market Making Strategy: Simulating Spread, Depth & Toxic Order Flow

If your “backtest” is just OHLC candles and imaginary PnL, you’re not testing a market making - you’re LARPing as one. MM lives in the order book, not on the chart. Let’s fix that 😏

1. Start with the Book, Not the Candle 📊

For a realistic MM backtest you need at least:

  • Best bid/ask spread over time
  • Depth at each level (how much size is really there)
  • Queue position (how many orders are in front of you)

If your simulator assumes “I quoted, price printed there, so I’m filled” - congrats, you’ve just built a fantasy engine.

2. Simulating Fills: You’re Not First in Line 🎯

Basic fill logic should:

1. Replay historical trades (not just prices).
2. For each trade:

  • Check if the trade price crosses your quote.
  • Check if traded size ≥ your queue position + order size.

3. Only then mark your order as filled.

No queue modeling = magical thinking. Real MM is about being somewhere in the stack, not always at the front.

3. Toxic Order Flow: When Smart Money Farms You 🧪

“Toxic flow” = you get filled only when the market is about to run against you.

In a backtest you can approximate it by:

  • Tagging windows before big moves as high toxicity.
  • Increasing the chance your quotes fill right before a breakout.
  • Separating PnL from “normal” vs “toxic” fills.

If your profits disappear once toxicity is modeled, your “edge” was just being the designated exit liquidity.

4. Don’t Skip Crash Mode 💥

Now stress it: spreads widen, depth disappears, volatility explodes.
This is exactly where a Market Making Program can quietly save you - better fees, infra, and incentives can be the difference between controlled inventory bleed and a full liquidation party.

If you want to dive deeper into how a market making program can protect you during market crashes, there’s a separate article - worth a read before the next -30% day 🚀

Top comments (0)