Python signals, C++20 execution

Prediction-market trading system

An automated trading system for prediction markets. Python finds fee-adjusted price gaps between markets, and a separate C++20 engine sizes and places the trades, with messages crossing over shared memory in about 10 microseconds.

  • Python
  • C++20
  • Cap'n Proto

Key numbers

  • 10.4µs

    median, Python to C++

  • 58.8kmsg/s

    single-thread peak

  • 0

    dropped writes at any rate

  • 55×

    faster than JSON over HTTP

How it works

  1. Market feedsPolymarket, Kalshi
  2. Gap scannerPython, fee-adjusted
  3. Shared-memory ringCap'n Proto, one writer
  4. C++20 engineKelly sizing
  5. Circuit breakertrip, then halt
  6. Exchangeor mock exchange
Price feeds from prediction markets go to a Python scanner that looks for fee-adjusted gaps between venues. Each opportunity is written to a shared-memory ring. A separate C++20 engine reads the ring, sizes the trade with a fraction of the Kelly bet, and passes it through a circuit breaker before it reaches the exchange or a mock exchange used for testing.

Decisions that matter

  • Python decides, C++ executes

    Python is good for models and bad at sharing a process: a Python reader on the same ring stalls on the interpreter lock for about 8 ms. Moving execution to its own C++ process brings the median to 10.4 µs.

  • Benchmarks that cannot flatter

    Latency is timed from when each message was scheduled to leave, not when it did. The 55× figure is quoted only at the one rate the JSON baseline could keep up with, and most of the 10.4 µs turns out to be encoding in Python, not the ring.

  • Bets shrink under stress

    Trades start at a quarter of the Kelly bet. One loss over 1% of the bankroll cuts the size for the next ten trades; a 5% drawdown cancels every order and stops the engine until a person resets it.

Results

A separate C++ reader is 55 times faster than JSON over HTTP.
  • Shared memory to C++10.5 µs
  • JSON over local HTTP577.1 µs
  • Shared memory to Python, same process7,611.0 µs

Median one-way latency at 5,900 messages a second, the highest rate all three paths could hold. Log scale; lower is better.

Limits. Tested against a mock exchange that simulates fills and delays. Several strategies in its plan are not built yet.

Next projectSatellite constellations for disaster response