Skip to main content
Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Trade the spread between two cointegrated prediction markets. When the spread deviates from its mean, enter positions expecting reversion.

Full Code

How It Works

  1. cointegration_test() verifies the two price series share a long-run equilibrium
  2. StatArbConfig sets entry/exit z-score thresholds and the hedge ratio
  3. stat_arb() monitors the spread z-score each cycle:
  • When z > entry_z: short the spread (sell A, buy B)
  • When z < -entry_z: long the spread (buy A, sell B)
  • When |z| < exit_z: close the position
  1. The hedge ratio scales the B-side size to maintain dollar-neutrality

Spread Z-Score

You can also compute the z-score manually for custom logic:

Run It

Simpler Alternative

For a lighter approach without cointegration testing, use spread_convergence():
See Statistical Arbitrage for the full method reference.