- 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: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.
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.
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.
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:- Liquidations: Close orders generated by the liquidation engine are filled first.
- Reduce orders: orders that close or reduce an existing position are filled next.
- Increase orders: orders that open or add to a position are filled last.