-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderbook.cpp
More file actions
159 lines (127 loc) · 5.37 KB
/
orderbook.cpp
File metadata and controls
159 lines (127 loc) · 5.37 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <map>
#include <deque>
#include <unordered_map>
#include <algorithm>
enum class Side { Buy, Sell };
class Order {
public:
Order(int id, Side side, double price, int quantity)
: id(id), side(side), price(price), quantity(quantity) {}
int getId() const { return id; }
Side getSide() const { return side; }
double getPrice() const { return price; }
int getQuantity() const { return quantity; }
void setQuantity(int qty) { quantity = qty; }
private:
int id;
Side side;
double price;
int quantity;
};
class OrderBook {
private:
std::map<double, std::deque<Order>, std::greater<double>> buyOrders;
std::map<double, std::deque<Order>, std::less<double>> sellOrders;
std::unordered_map<int, std::tuple<Side, double, std::deque<Order>::iterator>> orderIndex;
std::deque<Order>& getOrderQueue(Side side, double price) {
return (side == Side::Buy) ? buyOrders[price] : sellOrders[price];
}
void removeEmptyPriceLevel(Side side, double price) {
if (side == Side::Buy && buyOrders[price].empty()) {
buyOrders.erase(price);
} else if (side == Side::Sell && sellOrders[price].empty()) {
sellOrders.erase(price);
}
}
void matchOrder(Order& incomingOrder) {
if (incomingOrder.getSide() == Side::Buy) {
while (incomingOrder.getQuantity() > 0 && !sellOrders.empty()) {
auto& [price, orders] = *sellOrders.begin();
if (incomingOrder.getPrice() < price) break;
while (!orders.empty() && incomingOrder.getQuantity() > 0) {
Order& existingOrder = orders.front();
int matchQuantity = std::min(incomingOrder.getQuantity(), existingOrder.getQuantity());
incomingOrder.setQuantity(incomingOrder.getQuantity() - matchQuantity);
existingOrder.setQuantity(existingOrder.getQuantity() - matchQuantity);
if (existingOrder.getQuantity() == 0) {
orderIndex.erase(existingOrder.getId());
orders.pop_front();
}
}
if (orders.empty()) {
sellOrders.erase(sellOrders.begin());
}
}
} else {
while (incomingOrder.getQuantity() > 0 && !buyOrders.empty()) {
auto& [price, orders] = *buyOrders.begin();
if (incomingOrder.getPrice() > price) break;
while (!orders.empty() && incomingOrder.getQuantity() > 0) {
Order& existingOrder = orders.front();
int matchQuantity = std::min(incomingOrder.getQuantity(), existingOrder.getQuantity());
incomingOrder.setQuantity(incomingOrder.getQuantity() - matchQuantity);
existingOrder.setQuantity(existingOrder.getQuantity() - matchQuantity);
if (existingOrder.getQuantity() == 0) {
orderIndex.erase(existingOrder.getId());
orders.pop_front();
}
}
if (orders.empty()) {
buyOrders.erase(buyOrders.begin());
}
}
}
}
public:
bool addOrder(int id, Side side, double price, int quantity) {
if (orderIndex.find(id) != orderIndex.end()) return false;
Order newOrder(id, side, price, quantity);
matchOrder(newOrder);
if (newOrder.getQuantity() > 0) {
auto& queue = getOrderQueue(side, price);
queue.push_back(newOrder);
orderIndex[id] = std::make_tuple(side, price, std::prev(queue.end()));
}
return true;
}
bool cancelOrder(int id) {
auto it = orderIndex.find(id);
if (it == orderIndex.end()) return false;
auto [side, price, orderIt] = it->second;
auto& queue = getOrderQueue(side, price);
queue.erase(orderIt);
orderIndex.erase(it);
removeEmptyPriceLevel(side, price);
return true;
}
void printBook() const {
std::cout << "\n=== Order Book ===\n";
std::cout << "SELLS:\n";
for (auto it = sellOrders.rbegin(); it != sellOrders.rend(); ++it) {
int totalQty = 0;
for (const auto& order : it->second) totalQty += order.getQuantity();
std::cout << " " << it->first << " x " << totalQty << "\n";
}
std::cout << "BUYS:\n";
for (const auto& [price, orders] : buyOrders) {
int totalQty = 0;
for (const auto& order : orders) totalQty += order.getQuantity();
std::cout << " " << price << " x " << totalQty << "\n";
}
}
};
int main() {
OrderBook book;
std::cout << "Order Book Demo\n";
book.addOrder(1, Side::Buy, 99.50, 100);
book.addOrder(2, Side::Buy, 99.00, 200);
book.addOrder(3, Side::Sell, 100.50, 150);
book.addOrder(4, Side::Sell, 101.00, 100);
book.printBook();
std::cout << "\nAdding matching order...\n";
book.addOrder(5, Side::Buy, 100.50, 75);
book.printBook();
std::cout << "\nDemo completed!\n";
return 0;
}