write a basic matching engine in c++
- User
write a basic matching engine in c++
Assistant
Here's a basic matching engine in C++:
#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <string> struct Order { std::string id; bool isBuy; double price; int quantity; std::string userId; Order(std::string id, bool isBuy, double price, int quantity, std::string userId) : id(id), isBuy(isBuy), price(price), quantity(quantity), userId(userId) {} }; class MatchingEngine { private: std::vector<Order> buyOrders; std::vector<Order> sellOrders; std::unordered_map<std::string, Order*> orderMap; // Sort buy orders by price (highest first) static bool compareBuyOrders(const Order& a, const Order& b) { return a.price > b.price; } // Sort sell orders by price (lowest first) static bool compareSellOrders(const Order& a, const Order& b) { return a.price < b.price; } public: void addOrder(const Order& order) { if (order.isBuy) { buyOrders.push_back(order); orderMap[order.id] = &buyOrders.back(); std::sort(buyOrders.begin(), buyOrders.end(), compareBuyOrders); matchOrders(); } else { sellOrders.push_back(order); orderMap[order.id] = &sellOrders.back(); std::sort(sellOrders.begin(), sellOrders.end(), compareSellOrders); matchOrders(); } } void cancelOrder(const std::string& orderId) { if (orderMap.find(orderId) != orderMap.end()) { Order* order = orderMap[orderId]; if (order->isBuy) { buyOrders.erase(std::remove_if(buyOrders.begin(), buyOrders.end(), [&orderId](const Order& o) { return o.id == orderId; }), buyOrders.end()); } else { sellOrders.erase(std::remove_if(sellOrders.begin(), sellOrders.end(), [&orderId](const Order& o) { return o.id == orderId; }), sellOrders.end()); } orderMap.erase(orderId); } } void matchOrders() { bool matchFound = true; while (matchFound && !buyOrders.empty() && !sellOrders.empty()) { matchFound = false; Order& topBuy = buyOrders.front(); Order& topSell = sellOrders.front(); if (topBuy.price >= topSell.price) { matchFound = true; // Determine trade quantity and price int tradeQty = std::min(topBuy.quantity, topSell.quantity); double tradePrice = topSell.price; // Could use different price strategies // Execute the trade std::cout << "MATCH: " << topBuy.id << " and " << topSell.id << " for " << tradeQty << " units at $" << tradePrice << std::endl; // Update order quantities topBuy.quantity -= tradeQty; topSell.quantity -= tradeQty; // Remove filled orders if (topBuy.quantity == 0) { orderMap.erase(topBuy.id); buyOrders.erase(buyOrders.begin()); } if (topSell.quantity == 0) { orderMap.erase(topSell.id); sellOrders.erase(sellOrders.begin()); } } } } void printOrderBook() { std::cout << "--- ORDER BOOK ---" << std::endl; std::cout << "BUY ORDERS:" << std::endl; for (const auto& order : buyOrders) { std::cout << "ID: " << order.id << ", Price: " << order.price << ", Qty: " << order.quantity << std::endl; } std::cout << "SELL ORDERS:" << std::endl; for (const auto& order : sellOrders) { std::cout << "ID: " << order.id << ", Price: " << order.price << ", Qty: " << order.quantity << std::endl; } std::cout << "----------------" << std::endl; } }; int main() { MatchingEngine engine; // Add some sample orders engine.addOrder(Order("buy1", true, 100.0, 10, "user1")); engine.addOrder(Order("sell1", false, 102.0, 5, "user2")); engine.addOrder(Order("buy2", true, 101.0, 7, "user3")); engine.addOrder(Order("sell2", false, 99.0, 3, "user4")); engine.printOrderBook(); return 0; }Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.