Cancels, Amends and the State Machine

Real order flow is mostly cancels. A market maker adjusting quotes generates far more cancel and replace messages than trades, and the reported "cancel/replace storm" problem exists because handling that volume correctly is a different skill from matching.

An order is a state machine

Every order is in exactly one state, and only some transitions are legal.

  1. 1
    New. Received and validated, not yet in the book.
  2. 2
    Working. Resting in the book with some quantity available.
  3. 3
    Partially filled. Still working, with less quantity than it started with.
  4. 4
    Terminal. Filled, cancelled, or rejected. No further transition is possible.

Writing it out this way answers most of the questions the statement will ask. A cancel for a terminal order is not an error in the sense of a bug, it is a normal race: the cancel was in flight while the fill happened. A real venue answers it with a rejection saying "too late to cancel", and an assessment problem will tell you whether to no-op or to report. What you must not do is let it resurrect a filled order, which is what happens when cancel is implemented as an unconditional removal from the index without checking state.

The rest of this lesson is for subscribers

Unlock every lesson in Programming for Quantitative Developers, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

Keep reading Programming for Quantitative Developers

25 lessons in this course, and every other premium course, on one subscription.

  • Every lesson in every course, with the worked examples and interactive simulators
  • Graded questions on every lesson, with explanations for the wrong answers as well as the right one
  • The trainers, timed assessments and brainteaser library that go with them