-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderBook.h
More file actions
60 lines (55 loc) · 2.26 KB
/
OrderBook.h
File metadata and controls
60 lines (55 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef ORDER_BOOK_H
#define ORDER_BOOK_H
#include "Order.h"
#include "AVLTree.h"
#include <vector>
class OrderBook {
private:
AVLTree buyOrders; // AVL tree for buy orders (sorted by descending price)
AVLTree sellOrders; // AVL tree for sell orders (sorted by ascending price)
public:
// Add a new order to the book
void addOrder(const Order& order) {
if (order.side == 'B') {
buyOrders.insert(order.price, order);
} else if (order.side == 'S') {
sellOrders.insert(order.price, order);
}
}
// Match an incoming order with existing orders in the book
std::pair<bool, Order> matchOrder(Order& incomingOrder) {
if (incomingOrder.side == 'B') {
// Find the lowest sell order
auto sellOrdersAtPrice = sellOrders.find(incomingOrder.price);
if (!sellOrdersAtPrice.empty()) {
// Match with the first order at the price level
Order matchedOrder = sellOrdersAtPrice.front();
if (incomingOrder.quantity >= matchedOrder.quantity) {
incomingOrder.quantity -= matchedOrder.quantity;
sellOrdersAtPrice.erase(sellOrdersAtPrice.begin());
} else {
matchedOrder.quantity -= incomingOrder.quantity;
incomingOrder.quantity = 0;
}
return {true, matchedOrder};
}
} else if (incomingOrder.side == 'S') {
// Find the highest buy order
auto buyOrdersAtPrice = buyOrders.find(incomingOrder.price);
if (!buyOrdersAtPrice.empty()) {
// Match with the first order at the price level
Order matchedOrder = buyOrdersAtPrice.front();
if (incomingOrder.quantity >= matchedOrder.quantity) {
incomingOrder.quantity -= matchedOrder.quantity;
buyOrdersAtPrice.erase(buyOrdersAtPrice.begin());
} else {
matchedOrder.quantity -= incomingOrder.quantity;
incomingOrder.quantity = 0;
}
return {true, matchedOrder};
}
}
return {false, Order()}; // No match found
}
};
#endif // ORDER_BOOK_H