1. How does order matching work?
Orders are matched according to price-time priority, i.e. orders with more aggressive price (higher for buy, lower for sell) have higher priority for matching, and for orders with the same price, their priorities are sorted by submission time ascending. "Open Long" and "Close Short" orders belong to the bid side, while "Close Long" and "Open Short" orders belong to the offer side. On each side, the orders are sorted according to price-time priority. Matching happens when the highest bid price is higher than or equal to the lowest offer price.
2. What happens after the orders are matched?
Once an open long or open short order is matched, corresponding position will be opened. The Average Position Price and Settlement Reference Price will be updated accordingly. When a close long or close short order is matched, corresponding position will be closed. The Average Position Price and Settlement Reference Price will remain the same.
Average Position Price = Face Value x (Number of Perpetuals Held + Number of Perpetuals Opened) / (Face Value x Number of Perpetuals Held / Original Average Position Price + Face Value x Number of Perpetuals Opened / Filled Price)
Settlement Reference Price = Face Value x (Number of Perpetuals Held + Number of Perpetuals Opened) / (Face Value x Number of Perpetuals Held / Original Settlement Reference Price + Face Value x Number of Perpetuals Opened / Filled Price)
Settlement Reference Price is set to be the Settlement Price at 5pm Hong Kong time everyday, and will be updated whenever there are new perpetuals opened. It is equal to the Average Position Price if the position has never been settled.
Example: The latest mark price is USD 600. A user holds 6 BTC long perpetuals and his average position price is USD 500.
The user now opens extra 5 long perpetuals with the details below:
The Average Position Price of the 5 perpetuals is: 100 * 5 / ( 100 * 1 / 580 + 100 *1 / 570 + 100 * 3 / 560) = 565.89
Therefore, the new Average Position Price will be: 100 * ( 6 + 5 ) / ( 100 * 5 / 565.89 + 100 * 6 / 500) = 527.94