Skip to main content
A correct book is a dictionary keyed by (sport, event_id, bet_type) whose values are the latest price_list. Keep it correct with three rules.

The three rules

  1. offer is a full replacement for that triple, not a delta. Overwrite, do not merge.
  2. remove_offer means that bet type has no liquidity left. Delete it. Do not leave last-known prices in the book.
  3. clear_events means the server lost its upstream feed. Drop everything and wait for the fresh snapshot and sync.

The code

Reading the book

price_list is sorted by decimal price descending, so price_list[0] is the highest advertised price and its max is the stake advertised there. Offer prices are indicative. A book can show prices that cross, and not every level can be traded. The price you can trade is the pmm quote for your betslip. An against offer is priced as odds on the outcome not happening, so do not compare its price directly with the for price. To put both sides on one scale, convert an against price p to the equivalent lay price 1 / (1 - 1/p).

Depth

Each entry in price_list is one price level. Summing max across levels gives the advertised depth on that side. Offer prices are indicative, and not every level can be traded. Convert the against offer as shown above, then compare it with the for offer for the same selection to see the spread and where the size sits.

Things that go wrong

  • Merging instead of replacing. Stale levels linger after the market moves. Always overwrite the whole list.
  • Ignoring remove_offer. You quote against liquidity that is gone.
  • Ignoring clear_events. Everything you hold is stale until the next sync.
  • Reading too slowly. Backpressure closes the socket with code 1008, or with a bare TCP close if you stopped reading. Hand frames to a queue if processing is heavy.
See WebSocket stream for the full protocol and Stream many events for scaling this up.