Skip to main content
Matching is the process by which orders are filled and corresponding positions are created, closed, and updated. All matches execute at the current oracle price. The matching process is triggered by two separate scenarios:
  • A new order is placed (matching runs automatically for that market)
  • A matching transaction is sent to the protocol

How matching works

Each time matching is triggered for a market, the algorithm runs through three steps:
1

Collect executable orders

Every unfilled order whose execution price threshold permits execution at the current oracle price is collected. Orders whose threshold is outside that range are skipped this round.
2

Build two queues

The collected orders are split into two opposing queues by the direction they trade:
  • The long queue holds every order that buys. Orders opening a long position and orders closing a short position.
  • The short queue holds every order that sells. Orders opening a short position and orders closing a long position.
Within each queue, orders are arranged by priority (see Order priority below).
3

Queues are filled against each other

The two queues are filled against each other by total notional value. The smaller queue is filled completely; the larger queue is filled up to the same notional, working down from the top of the queue.For example, if the long queue totals 1,000 of notional and the short queue totals 500, the short queue is fully filled and the first 500 of notional in the long queue is filled. The remaining long orders stay unfilled this round.
If an order is only partially filled in a cycle, the unfilled portion remains in its queue for the next matching cycle.

Order priority

Within each queue, orders are filled in a strict priority order. There is no price priority. An order’s execution price threshold only determines whether it is executable, not its place in the queue. Orders are filled in the following order of priority:
  1. Liquidations: Close orders generated by the liquidation engine are filled first.
  2. Reduce orders: orders that close or reduce an existing position are filled next.
  3. Increase orders: orders that open or add to a position are filled last.
Within each level of priority, orders are filled in time priority (FIFO). The order placed earliest is filled first.