Price-time order matching
Allocate a synthetic buy instruction across individual asks and reconcile priority, partial fills, cost and cancellation.
32 minutes · intermediate · Original Poker4Quant lessonWhat you will learn
Construct price-time allocations for a frozen limit-order book, derive weighted execution cost and distinguish IOC partial fills from FOK preflight cancellation.
Prerequisites: Reconstructing an action ledger · Net payoff and sunk costs · Fair prices and margins
Define the book and the experiment
An order book records unexecuted instructions to buy or sell a specified instrument. A bid offers to buy; an ask offers to sell. We study one incoming buyer against individual resting asks. Prices are chips per indivisible unit, and quantities count units. Order identifiers distinguish instructions even when their prices agree. Earlier arrival means a smaller timestamp. Screen position, alphabetic order and order size confer no priority in our stipulated price-time rule.
Assume one venue, a frozen fully displayed book, sufficient buyer funds, no fees, no hidden orders, no self-trade restrictions and no competing events during this single match. Every unit executes at its resting ask price. There are no auctions, routing decisions or changes to orders while matching occurs. These assumptions define an original educational mechanism rather than actual exchange behavior. Real venues can use different allocation algorithms, protections and instructions. The sources support order terminology and FIFO priority; they do not validate this synthetic book as market data.
Separate price eligibility from allocation priority
The four resting sellers are A: two units at 101, arrival 3; B: three units at 101, arrival 1; C: four units at 102, arrival 0; and D: two units at 104, arrival 2. A buy limit L permits prices at or below L. It does not instruct the engine to pay L for every unit. At L=102, A, B and C qualify while D does not. At L=100 none qualify, even though the book contains eleven units in total.
Among eligible asks, sort by increasing price first and increasing arrival time second. Thus B precedes A, both precede C, and D comes last if its price qualifies. C has the earliest timestamp in the entire book, but its higher price keeps it behind both cheaper sellers. This is the essential two-key comparison. Equal-price orders are separate queue entries, so aggregating them into five units at 101 preserves depth but conceals which seller receives a partial execution. For a two-unit buyer, B receives two and A receives zero.
Derive the matching recurrence and preflight condition
Write requested quantity as Q. For an immediate-or-cancel instruction, abbreviated IOC, visit eligible sellers in priority order. Start with remaining quantity R=Q. At each seller with available quantity q, assign f=min(q,R), subtract f from R, and proceed. Once R reaches zero, assign zero to every later seller. The resulting fills obey capacity constraints, the incoming quantity constraint and the priority rule together. Capacity and total quantity alone are insufficient: allocating two units to A while B still has available units violates FIFO even though those two numerical constraints hold.
A fill-or-kill instruction, abbreviated FOK, adds a condition before this recurrence. Sum eligible available depth H. If H<Q, execute nothing and cancel all Q units. If H is at least Q, use the same priority recurrence and complete the full request. FOK does not mean executing available units and canceling the rest. In this frozen model the preflight calculation and execution see the same book, so passing preflight guarantees enough depth for the recurrence. That implication depends on our event boundary. Do not transplant it into an asynchronous system without specifying how the book is protected during a match.
Reconcile execution cost and the unfilled instruction
For Q=6, L=102 and IOC, B supplies three units, A supplies two and C supplies one. In displayed identifier order the allocation vector is (2,3,1,0), while the actual execution sequence is B,B,B,A,A,C. Cost is 3 times 101 plus 2 times 101 plus 1 times 102 = 607 chips. The average execution price is 607/6, or approximately 101.166667 chips per filled unit. Independently group by price: five units at 101 cost 505, and one at 102 adds 102. The two cost calculations agree.
The buy ceiling of 102 would give an incorrect cost of 612 if multiplied by all six units. Using the best ask 101 for all six would instead give 606, ignoring exhausted cheap depth. Averaging the two price labels equally gives 101.5, ignoring unequal fill sizes. Each mistake confuses a different object with the weighted execution ledger. After matching, C retains three units and D retains two; A and B retain none. Incoming quantity satisfies Q=filled+canceled. Resting seller availability satisfies available=filled+residual for each identifier. Chips paid by the buyer equal chips received by the sellers because fees are excluded.
Construct counterfactual instructions and audit their limits
Keep Q=6 but lower L to 101. Only five units qualify. IOC buys those five for 505 chips, averages 101 and cancels one incoming unit. FOK instead buys zero, costs zero and cancels six. Its average execution price is undefined because no execution exists; enter none in the workshop rather than inventing a zero price. A zero cost and an undefined average are compatible. They answer different questions. Neither IOC nor FOK leaves an incoming remainder resting in this activity.
Now cancel B before the incoming order arrives and restore L=102. Two units at 101 and four at 102 remain eligible. All six requested units execute for 202+408=610 chips, averaging 305/3, or approximately 101.666667. Relative to the original cost 607, removing three cheap units raises the cost by three chips because three additional units must now execute at 102 instead of 101. The cancellation is a separate event completed before our frozen match. It changes available depth; it is not a permission to delete a seller after observing an inconvenient execution. Under FOK, a seventh requested unit would now cause complete cancellation because only six eligible units remain.
The workshop asks you to change quantity, ceiling, timing instruction and the pre-arrival cancellation, then construct every seller allocation and audit cost, average and canceled quantity. An edited model invalidates earlier answers. Inspect a low-quantity case to expose queue order and a zero-eligible-depth case to expose undefined averages. This is deterministic execution accounting: it supplies neither a probability of later execution nor a recommendation about which instruction a person should place. Future arrivals, adverse selection, market impact and economic value require additional models.
Worked example
Match a six-unit buy with limit 102 against the four resting sellers under IOC, then change the ceiling to 101 under IOC and FOK.
- Priority is B then A then C then D, based on price first and timestamp second. Eligible depth at 102 is 3+2+4=9.
- At B, min(3,6)=3 fills and R=3. At A, min(2,3)=2 fills and R=1. At C, min(4,1)=1 fills and R=0. D receives zero.
- Cost=3×101+2×101+1×102=303+202+102=607. Grouped cross-check: 5×101+102=607. Average=607/6.
- Resting residuals in A,B,C,D order are (0,0,3,2). Incoming balance is 6=6+0. Each seller residual plus its fill equals its available quantity.
- At ceiling 101, IOC fills five for 505 chips and cancels one. FOK sees eligible depth five below requested six, fills zero and cancels six; its average is undefined.
Price eligibility, queue priority and timing instruction jointly determine execution. The buy ceiling bounds individual fill prices; it is not the weighted average or a future-fill guarantee.
Hands-on: Allocate a frozen order book
Change the incoming instruction and pre-arrival cancellation, construct seller fills and audit the execution ledger.
Allocate a frozen order book
Buy only. One frozen venue; integer units; resting ask prices; no fees or hidden liquidity. Earlier arrival means smaller time. Draft lasts for this visit; no game or lesson credit.
Build the incoming instruction
Requested: 6 units. Buy ceiling: 102 chips per unit. Policy: IOC.
Eligible available depth: 9. IOC cancels any incoming remainder; FOK requires the full requested quantity before any execution.
Construct individual seller fills
Displayed A–D order is not matching priority. Adjust fills, then test the whole allocation. Buttons permit a wrong allocation so you can diagnose its rule violation.
Seller A
Ask 101; arrival 3; available 2; price eligible.
Draft A fill: 0; seller residual: 2.
Seller B
Ask 101; arrival 1; available 3; price eligible.
Draft B fill: 0; seller residual: 3.
Seller C
Ask 102; arrival 0; available 4; price eligible.
Draft C fill: 0; seller residual: 4.
Seller D
Ask 104; arrival 2; available 2; above ceiling.
Draft D fill: 0; seller residual: 2.
Draft executed units: 0. Draft cost: 0 chips. Capacity alone does not establish priority.
Construct an allocation before auditing.
Common mistakes
- Sorting timestamps globally before checking price.
- Allocating pro rata in a model that specifies FIFO.
- Multiplying requested quantity by the limit instead of summing actual fills.
- Executing a partial FOK order or leaving an IOC remainder resting.
- Reporting a zero average when filled quantity is zero.
Keep these ideas
- Filter eligible prices, then sort price before arrival time.
- IOC executes eligible depth up to the request; FOK first requires enough depth for the entire request.
- Total cost is the sum of quantity times resting price; average divides by executed units.
- Reconcile each seller balance and the incoming filled-plus-canceled balance.
Further reading
- SEC Investor.gov: Understanding Order Types, market/limit and IOC/FOK sections ↗
Read market/limit definitions, multiple execution prices and IOC/FOK timing paragraphs. Updated August 18, 2026. Used for terminology only; original frozen-book examples, arithmetic and workshop. No source exercises or video review claimed. Checked 2026-10-25.
- CME Group: How Agricultural Markets Operate (August 2016), matching section, printed page 6 ↗
Read sequential processing, FIFO price/time priority and contrasting hybrid allocation discussion. Historical explanation supports the FIFO principle only; no current product-specific algorithm claim. Original book and fill derivation; no video reviewed. Checked 2026-10-25.