Below is a basic implementation of a matching engine in C++. This engine matches buy and sell orders based on price and quantity. It's a simplified version and does not include features like order prioritization (e.g., time priority), but it demonstrates the core logic of matching orders.
cpp
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
// Order structure
struct Order {
int id;
bool isBuy; // true for buy, false for sell
double price;
int quantity;
Order(int id, bool isBuy, double price, int quantity)
: id(id), isBuy(isBuy), price(price), quantity(quantity) {}
};
// Matching engine class
class MatchingEngine {
private:
std::priority_queue<Order, std::vector<Order>, std::greater<>> buyOrders; // Min-heap for buy orders (highest price first)
std::priority_queue<Order, std::vector<Order>, std::less<>> sellOrders; // Max-heap for sell orders (lowest price first)
public:
void addOrder(const Order& order) {
if (order.isBuy) {
buyOrders.push(order);
} else {
sellOrders.push(order);
}
matchOrders();
}
void matchOrders() {
while (!buyOrders.empty() && !sellOrders.empty() &&
buyOrders.top().price >= sellOrders.top().price) {
Order buyOrder = buyOrders.top();
Order sellOrder = sellOrders.top();
buyOrders.pop();
sellOrders.pop();
int tradeQuantity = std::min(buyOrder.quantity, sellOrder.quantity);
double tradePrice = sellOrder.price; // Price is set by the sell order
std::cout << "Trade executed: Price=" << tradePrice
<< ", Quantity=" << tradeQuantity << std::endl;
if (buyOrder.quantity > tradeQuantity) {
buyOrders.push(Order(buyOrder.id, true, buyOrder.price, buyOrder.quantity - tradeQuantity));
}
if (sellOrder.quantity > tradeQuantity) {
sellOrders.push(Order(sellOrder.id, false, sellOrder.price, sellOrder.quantity - tradeQuantity));
}
}
}
};
int main() {
MatchingEngine engine;
// Example orders
engine.addOrder(Order(1, true, 100.0, 10)); // Buy order
engine.addOrder(Order(2, false, 90.0, 15)); // Sell order
engine.addOrder(Order(3, false, 100.0, 5)); // Sell order
engine.addOrder(Order(4, true, 110.0, 8)); // Buy order
return 0;
}
Explanation:
Order Structure: Each order has an id, a flag isBuy to indicate if it's a buy or sell order, a price, and a quantity.
Matching Engine:
Uses two priority queues: one for buy orders (min-heap, sorted by highest price first) and one for sell orders (max-heap, sorted by lowest price first).
The addOrder function adds orders to the appropriate queue and then attempts to match orders.
The matchOrders function checks if the best buy order's price is greater than or equal to the best sell order's price. If so, it executes a trade and updates the remaining quantities.
Main Function: Demonstrates adding orders to the engine, which triggers matching logic.
Limitations:
No time priority for orders.
No cancellation or modification of orders.
Assumes infinite liquidity if no matching orders are present.
This is a basic implementation and can be extended with additional features like order books, order prioritization, and more sophisticated matching algorithms.