Complete Guide to Order Matching: Core Mechanism of Order Execution

Complete Guide to Order Matching: Core Mechanism of Order Execution

OKX教程团队

Do you want to know how exchanges match buy and sell orders? Are you curious why sometimes orders execute immediately while other times they wait? Do you want to understand price priority and time priority rules?

In March 2023, Zhang Ming from Shenzhen placed a limit buy order for BTC at $30,000. Seconds later, someone placed a sell order at $30,000, and the matching engine immediately matched the two orders, completing the trade. Order matching allows buyers and sellers to trade without direct communication, with the system automatically completing transactions.

In August 2023, Ms. Li from Shanghai placed two buy orders simultaneously: $29,900 and $29,800. When someone placed a sell order at $29,900, the matching engine prioritized her $29,900 order (price priority principle) over the $29,800 order.

In November 2023, Mr. Wang from Beijing and Ms. Liu from Guangzhou both placed buy orders at $30,000 simultaneously. When someone placed a sell order at $30,000, the matching engine prioritized Mr. Wang's order (time priority principle) because his order was earlier.

  • Partial Fill Risk: Large orders may only be partially filled, with the remainder continuing as pending orders
  • Price Slippage Risk: Market orders may experience slippage due to insufficient order book depth
  • Order Queue Risk: Limit orders may queue for extended periods without execution
  • Cancellation Delay Risk: Order cancellations may be delayed, during which orders may execute
  • Extreme Market Risk: Order books may be instantly cleared during extreme market conditions
  • System Latency Risk: Network delays may affect order submission and execution

What is Order Matching?

The system matches orders based on price priority and time priority principles.

"Buy to open long" and "buy to close short" are buy order directions. "Sell to open short" and "sell to close long" are sell order directions.

For buy orders, the highest price and earliest time ranks first. For sell orders, the lowest price and earliest time ranks first.

When the first buy order price is greater than or equal to the first sell order price, a match can be executed.

Basic Concepts of Order Matching

Order Book: Displays all pending buy and sell prices and quantities.

In March 2023, Mr. Chen from Hangzhou checked the BTC order book:

  • Buy Orders (Bid):
    • $29,900 - 2 BTC (highest price)
    • $29,800 - 5 BTC
    • $29,700 - 10 BTC
  • Sell Orders (Ask):
    • $30,000 - 3 BTC (lowest price)
    • $30,100 - 4 BTC
    • $30,200 - 8 BTC

Bid-Ask Spread: The difference between the highest bid and lowest ask.

In June 2023, Ms. Liu from Guangzhou:

  • Highest bid: $29,900
  • Lowest ask: $30,000
  • Bid-ask spread: $100

The smaller the spread, the better the liquidity.

How the Matching Engine Works

Matching Engine: System that automatically matches buy and sell orders.

In September 2023, Mr. Zhao from Chengdu:

  1. He placed a buy order: $30,000, 1 BTC
  2. Order enters the order book
  3. Someone placed a sell order: $30,000, 1 BTC
  4. Matching engine detects price match
  5. Automatically completes the trade
  6. Both parties receive execution notifications

The entire process completes in milliseconds.

Price Priority Principle

Orders with better prices execute first.

Buy Order Price Priority

Higher buy order prices execute first.

In October 2023, Ms. Wu from Xi'an and Mr. Zheng from Changsha:

  • Ms. Wu's buy order: $30,000, 1 BTC
  • Mr. Zheng's buy order: $29,900, 1 BTC
  • Someone placed a sell order: $29,900, 1 BTC
  • Matching engine prioritizes Ms. Wu's order (higher price)

Sell Order Price Priority

Lower sell order prices execute first.

In November 2023, Mr. Huang from Chongqing and Ms. Lin from Tianjin:

  • Mr. Huang's sell order: $30,000, 1 BTC
  • Ms. Lin's sell order: $30,100, 1 BTC
  • Someone placed a buy order: $30,100, 1 BTC
  • Matching engine prioritizes Mr. Huang's order (lower price)

Price Priority in Practice

In December 2023, Mr. Peng from Hefei wanted to buy BTC quickly:

  • Current lowest ask: $30,000
  • He placed a buy order: $30,100 (above market price)
  • Immediate execution (price priority)
  • Actual execution price: $30,000 (executed at sell order price)

By raising his buy order price, he ensured priority execution.

Time Priority Principle

When prices are equal, earlier orders execute first.

Time Priority Rules

Same-price orders queue by time.

In January 2024, Ms. Zeng from Suzhou, Mr. Deng from Wuxi, and Mr. Gong from Changzhou:

  • Ms. Zeng's buy order: $30,000, 1 BTC (10:00:00)
  • Mr. Deng's buy order: $30,000, 1 BTC (10:00:05)
  • Mr. Gong's buy order: $30,000, 1 BTC (10:00:10)
  • Someone placed a sell order: $30,000, 1 BTC
  • Matching engine prioritizes Ms. Zeng's order (earliest time)

Time Priority in Practice

In February 2024, Ms. Yao from Yangzhou wanted to buy a popular coin:

  • She placed a buy order in advance: $1.00
  • Others also placed buy orders: $1.00
  • After coin listing, her order executed first (earliest time)

Timestamp Precision

The matching engine uses millisecond-level timestamps.

In March 2024, Mr. Yuan from Nantong and Mr. Qin from Zhenjiang:

  • Mr. Yuan's order: 10:00:00.123
  • Mr. Qin's order: 10:00:00.456
  • Time difference: 333 milliseconds
  • Mr. Yuan's order executes first

Order Execution Process

Complete Fill

Order fully executed.

In April 2024, Ms. Shi from Taizhou:

  • Placed buy order: $30,000, 1 BTC
  • Someone placed sell order: $30,000, 2 BTC
  • Her order completely filled: 1 BTC
  • Sell order remainder: 1 BTC continues as pending

Partial Fill

Order partially executed, remainder continues as pending.

In May 2024, Mr. Tang from Yancheng:

  • Placed buy order: $30,000, 10 BTC
  • Someone placed sell order: $30,000, 3 BTC
  • Partial fill: 3 BTC
  • Remainder: 7 BTC continues as pending

Immediate or Cancel (IOC)

Order executes immediately, unfilled portion automatically cancels.

In June 2024, Ms. He from Lianyungang:

  • Placed IOC buy order: $30,000, 10 BTC
  • Order book only has 3 BTC sell orders
  • Executed: 3 BTC
  • Remaining 7 BTC automatically canceled (doesn't continue as pending)

Fill or Kill (FOK)

Order must be completely filled, otherwise entirely canceled.

In July 2024, Mr. Wei from Huai'an:

  • Placed FOK buy order: $30,000, 10 BTC
  • Order book only has 3 BTC sell orders
  • Cannot be completely filled
  • Order entirely canceled (none executed)

Market Order Matching

Market orders execute progressively through order book price levels.

Market Order Execution Process

In August 2024, Mr. Wei from Suqian used a market order to buy 10 BTC:

  • Order book sell orders:
    • $30,000 - 3 BTC
    • $30,100 - 4 BTC
    • $30,200 - 5 BTC
  • Execution process:
    • First takes 3 BTC at $30,000
    • Then takes 4 BTC at $30,100
    • Then takes 3 BTC at $30,200
  • Average execution price: $30,110

Market Order Slippage

When order book depth is insufficient, market orders experience slippage.

In September 2024, Ms. Jiang from Xuzhou used a market order to buy 100 BTC:

  • Insufficient order book depth
  • Execution price slipped from $30,000 to $32,000
  • Slippage: $2,000/BTC
  • Total slippage loss: $200,000

Prevention Method: Use limit orders in batches for large orders.

Practical Strategies for Order Matching

Strategy 1: Early Order Placement

Place orders in advance at key price levels, utilizing time priority.

In October 2024, Mr. Xie from Nanjing:

  • Predicted BTC would drop to $29,000
  • Placed buy order in advance: $29,000
  • When BTC dropped to $29,000, his order executed first

Strategy 2: Price Advantage

Raise buy order prices or lower sell order prices, utilizing price priority.

In November 2024, Mr. Gong from Changzhou urgently needed to buy:

  • Lowest ask: $30,000
  • He placed buy order: $30,050
  • Immediate execution (price priority)
  • Actual execution price: $30,000

Strategy 3: Batch Orders

Split large orders into batches to avoid slippage.

In December 2024, Ms. Yao from Yangzhou wanted to buy 100 BTC:

  • Split into 10 orders, 10 BTC each
  • Prices: $30,000-$30,500
  • Avoided massive slippage from market orders

Strategy 4: Monitor Order Book

Adjust strategy based on order book depth.

In January 2025, Mr. Yuan from Nantong:

  • Good order book depth: Use market orders for quick execution
  • Poor order book depth: Use limit orders to avoid slippage

Common Issues with Order Matching

Issue 1: Why Isn't My Order Executing?

In February 2025, Mr. Qin from Zhenjiang:

  • Placed buy order: $29,000
  • Current price: $30,000
  • Reason: Buy order price below market price, cannot execute

Solution: Raise buy order price or wait for price to drop.

Issue 2: Why Is Execution Price Different from Order Price?

In March 2025, Ms. Shi from Taizhou:

  • Placed buy order: $30,100
  • Execution price: $30,000
  • Reason: Matching engine executes at sell order price (favorable to buyer)

This is normal; the buyer received a price advantage.

Issue 3: Why Was My Order Partially Filled?

In April 2025, Mr. Tang from Yancheng:

  • Placed buy order: 10 BTC
  • Only executed: 3 BTC
  • Reason: Order book only had 3 BTC sell orders

Solution: Wait for more sell orders or raise buy order price.

Issue 4: Why Did Cancellation Fail?

In May 2025, Ms. He from Lianyungang:

  • Cancellation failed
  • Reason: Order already executed

Cancellation and execution occurred almost simultaneously; cancellation request was too late.

Advanced Order Matching Techniques

Technique 1: Iceberg Orders

Display only a portion of large orders to avoid market impact.

In June 2025, Mr. Wei from Huai'an wanted to buy 100 BTC:

  • Total order: 100 BTC
  • Only displays: 10 BTC at a time
  • After 10 BTC executes, automatically displays next 10 BTC

Technique 2: Time-Weighted Average Price (TWAP)

Place orders evenly over time to achieve average price.

In July 2025, Mr. Wei from Suqian:

  • Wants to buy 50 BTC
  • Split into 10 orders, 5 BTC each
  • One order per hour
  • Complete in 10 hours

Technique 3: Volume-Weighted Average Price (VWAP)

Allocate orders based on market volume to achieve near-market average price execution.

In August 2025, Ms. Jiang from Xuzhou:

  • Observed market volume distribution
  • Placed more orders during high-volume periods
  • Achieved near-market average price execution

Frequently Asked Questions

1. How does order matching work?

The matching engine automatically matches buy and sell orders based on price priority and time priority principles. Orders execute when buy price ≥ sell price.

2. Why is my order ranked lower?

Because other orders have better prices or earlier times. Raising price or placing orders earlier can improve priority.

3. Will market orders always execute?

Usually yes, but during extreme market conditions, order books may be cleared, causing non-execution or severe slippage.

4. When do limit orders execute?

When market price reaches your limit price. If price never reaches it, the order remains pending.

5. How to avoid slippage?

Use limit orders instead of market orders, or place orders in batches to avoid large single transactions.

6. Does the matching engine prioritize large trader orders?

No. The matching engine treats all orders equally, processing only by price and time priority principles.

Key Takeaways

  1. Order matching follows price priority and time priority principles, automatically matching buy and sell orders
  2. Orders with better prices execute first, same-price orders queue by time
  3. Market orders may experience slippage, recommend using limit orders in batches for large orders
  4. Monitor order book depth, adjust trading strategy based on liquidity
  5. Early order placement or better prices can improve order priority

Further Reading

  • Order Book: Understanding Market Depth
  • Limit Orders vs Market Orders: Order Type Details
  • Maker and Taker Orders: Reducing Trading Costs
  • Slippage: Causes of Price Deviation
  • Liquidity: Key Factor Affecting Trading Costs
  • Advanced Order Types: Iceberg Orders and TWAP

Ready to Start Trading?

Register on OKX with invite code OKK329 and enjoy 20% trading fee discount

Register Now

Invite Code: OKK329

Related Articles