Skip to main content
Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
A market maker that uses a Hidden Markov Model to detect market regimes and adapts its quoting strategy: tight spreads in calm markets, wide spreads in volatile markets, and no quotes during crises.

Full Code

How It Works

  1. markov_regime() fits an HMM to recent price returns from the feed
  2. Each cycle, it classifies the market into one of 3 states and stores the label + probabilities in ctx.params
  3. The quoter reads the regime and adjusts:
  • Calm - tight 2-cent spread, 15 contracts (maximize fill rate)
  • Volatile - wide 6-cent spread, 5 contracts (reduce adverse selection)
  • Crisis - no quotes (preserve capital)
  1. Regime probabilities provide confidence: if the model is 90% sure it’s calm, you can trust the tight spread

Standalone HMM

You can also use the Rust HMM directly for research:

Run It

Extending

Combine regime detection with inventory skew for a more robust strategy:
See Markov Regime Detection for the full HMM reference.