📄 Matching Engine Challenge
In this challenge you are going to implement a matching engine that manages multiple central limit order books.
To fulfill the challenge you will have to implement the interface as described in
main.hpp
.🏦 Background
A matching engine matches orders from market participants. These matches will result in trades.
A trade occurs when someone wants to buy at an equal or higher price than someone else wants to sell.
- When someone wants to buy, a market participant sends a buy order for a given symbol (e.g.
AAPL
orTSLA
).
- A sent order contains:
- an id
- a side (buy/sell)
- a limit price
- a volume
The limit price indicates:
- In case of a buy order → you are willing to buy at the given price or lower.
- In case of a sell order → you are willing to sell at the given price or higher.
📘 Order Book
All orders are managed in a Central Limit Order Book (CLOB) which has 2 sides:
- The buy side
- The sell side
If a new order is received by the matching engine, it first checks whether it can match with orders already in the order book on the opposite side.
- The order will be matched with the opposite side until either:
- The volume of the new order is exhausted, OR
- There are no orders on the opposite side with which the new order can match.
Priority rules:
- Price priority: the "best" price matches first.
- Time priority: if there are multiple orders at the same price, orders inserted earlier are matched first.
⚖️ Trades
In case a match occurs, a trade is generated.
This trade has two orders:
- An aggressive order: the order that causes the trade through either an INSERT or an AMEND.
- A passive order: the matching order on the opposite side.
Trade properties:
- Volume = minimum of the active volumes of the matched orders.
- Price = determined by the price of the passive order.
🔄 Operations
There are two operations that can be applied to an order once it is in the order book:
- Pull: removes the order from the order book.
- Amend: changes the price and/or volume of the order.
📑 Amend Rules
An AMEND causes the order to lose time priority in the order book, unless the only change to the order is that the volume is decreased.
- ✅ If only volume is decreased → the order retains time priority (keeps its spot).
- ❌ If price is changed or volume is increased → the order loses time priority (treated as a new order, goes to the back).
- If the price of the order is amended, it needs to be re-evaluated for potential matches.
Solutions
交易系统中的专业术语:
在交易系统中:
// filled = 已成交
// partially filled = 部分成交
// fully filled = 完全成交
// unfilled = 未成交
ask : 讨价 (卖) BUY
bid : 出价 (买) SELL
Order // 订单
OrderBook // 订单簿
Order ID // 订单ID
Symbol // 交易品种/标的
Side // 买卖方向
Volume // 数量/成交量
Price // 价格
Timestamp // 时间戳
Price Level // 价格级别
Best Bid // 最优买价
Best Ask // 最优卖价
Ticks // 价格单位(最小价格变动)
Price Priority // 价格优先
Time Priority // 时间优先
FIFO // 先进先出(First In, First Out)
Timestamp // 时间戳
Trade // 交易/成交
Execute // 执行交易
Filled // 已成交
Fully Filled // 完全成交
Partially Filled // 部分成交
Aggressive // 主动方
Passive // 被动方
Match // 匹配
Matching Engine // 匹配引擎
Price-Time Priority // 价格时间优先
Insert // 下单
Pull // 撤单
Amend // 改单
Cancel // 取消
struct Order { int id; std::string symbol; Side side; int64_t price; // ticks int volume; int timestamp; };