Order Types: The Physics of Execution

Why a 'Market Order' is an algorithm, not a request. Price-Time Priority, Slippage Physics, and why IOC is the HFT weapon of choice.

Intermediate 40 min read Expert Version →

🎯 What You'll Learn

  • Deconstruct the Price-Time Priority algorithm (FIFO Queues)
  • Analyze the Matching Engine State Machine (`New` -> `Filled`)
  • Calculate Slippage using Order Book Depth physics
  • Differentiate Maker (Post-Only) vs Taker (IOC) mechanics
  • Implement a basic Matching Engine loop in Python

Introduction

To a retail trader, a “Limit Order” is a wish: “I hope to buy at $100.” To an Engineer, a “Limit Order” is a State Object injected into a sorted data structure.

The Matching Engine does not “hope”. It iterates. It loops through asks, checks constraints (price, qty, time), and mutates state. In this lesson, we stop clicking buttons and start parsing the algorithms of execution.


The Physics: Price-Time Priority

Matching Engines follow strict laws. The most common is FIFO (First In, First Out).

The Algorithm:

  1. Price: Better prices execute first. (Buy High / Sell Low goes to front).
  2. Time: If prices are equal, the order that arrived earlier executes first.

Physics of the Queue: Imagine a bucket at Price $100.00.

  • Alice puts 100 shares in at t=0.
  • Bob puts 100 shares in at t=1.
  • Charlie sells 150 shares at market.

Result: Alice gets filled (100). Bob gets partially filled (50). Bob’s latency cost him 50 shares. This is why HFTs fight for nanoseconds: Queue Position is Money.


Deep Dive: The Market Order (Slippage)

A Market Order is not “Buy at Current Price”. It is: “Sweep the Book until Quantity = 0.”

The Physics of Slippage:

  • Order: Buy 1000 BTC.
  • Book:
    • 100 @ $50,000 (Level 1)
    • 200 @ $50,100 (Level 2)
    • 700 @ $50,500 (Level 3)

Execution: You eat Level 1. Then Level 2. Then Level 3. Your Average Fill Price is not $50,000. It is a weighted average of all three levels. The difference is Slippage.


Code: The Matching Engine Loop

How does an engine actually match?

def match_order(new_order, book):
    fills = []
    
    # While we need qty AND book has orders
    while new_order.qty > 0 and book.best_ask:
        best_ask = book.best_ask
        
        # 1. Price Check
        if new_order.price < best_ask.price:
            break # No overlap, add to book
            
        # 2. Match Logic
        match_qty = min(new_order.qty, best_ask.qty)
        
        # 3. State Mutation
        new_order.qty -= match_qty
        best_ask.qty -= match_qty
        
        fills.append({"price": best_ask.price, "qty": match_qty})
        
        # 4. Cleanup
        if best_ask.qty == 0:
            book.remove(best_ask)
            
    if new_order.qty > 0:
        book.add(new_order) # Remaining qty rests on book

Advanced Types: IOC vs Post-Only

Maker (Post-Only)

“I strictly want to add liquidity.” If your Buy Limit 100crossesaSellLimit100 crosses a Sell Limit 99, standard matching would fill you at $99 (Taker). Post-Only Physics: The engine rejects or adjusts your order instead of matching. You ensure the Maker Fee rebate.

Taker (IOC - Immediate or Cancel)

“I strictly want to remove liquidity, but never wait.” Used by HFT Snipers. “If I can’t catch this stale quote right now, cancel me. Do not let me rest on the book as a sitting duck.”


Practice Exercises

Exercise 1: Queue Position (Beginner)

Scenario: 3 orders at $100. A (100 qty), B (100 qty), C (100 qty). Task: A Sell order for 250 qty hits. Who gets what? (A=100, B=100, C=50).

Exercise 2: Slippage Calculation (Intermediate)

Scenario:

  • Ask 1: 100 @ 10.00
  • Ask 2: 100 @ 10.10 Task: Market Buy 150. Calculate total cost and average price per share. (10010 + 5010.10) / 150.

Exercise 3: Engine Design (Advanced)

Task: Why can’t a Matching Engine be multithreaded easily? Hint: Race conditions on the Order Book state. If Thread A matches Order 1 against Bid X, Thread B cannot match Order 2 against Bid X simultaneously.


Knowledge Check

  1. What determines priority if two orders have the same price?
  2. Why does a Market Order cause slippage?
  3. What happens to an IOC order if it can’t be fully filled?
  4. Why do HFTs use Post-Only orders?
  5. Is “Limit Buy 100"guaranteedtofillifthepricehits100" guaranteed to fill if the price hits 100?
Answers
  1. Time. The order that arrived first (lower timestamp/ID) executes first.
  2. Depth. It consumes liquidity at progressively worse price levels.
  3. Cancellation. The remainder is cancelled immediately, never added to the book.
  4. Rebates. To ensure they get paid the Maker rebate and avoid Taker fees.
  5. No. You might be at the back of the queue. 1 million shares could trade at $100 before your 1 share gets filled.

Summary

  • Matching: A state machine driven by Price and Time.
  • Slippage: The cost of eating liquidity.
  • Post-Only/IOC: Tools for controlling Maker/Taker status.

Questions about this lesson? Working on related infrastructure?

Let's discuss