-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlob.cc
More file actions
186 lines (161 loc) · 6.01 KB
/
Copy pathlob.cc
File metadata and controls
186 lines (161 loc) · 6.01 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <map>
#include <list>
#include <unordered_map>
#include <algorithm>
using namespace std;
using ll = long long;
enum Side {Buy, Sell};
struct Order {
u_int64_t id;
ll price;
u_int32_t quantity;
Side side;
};
using lo = list<Order>;
class OrderBook {
public:
bool addOrder(Order x) {
if (order_ref.count(x.id)) {
cout << "[ERROR] Order ID " << x.id << " already exists\n";
return false;
}
match(x);
if (x.quantity > 0) {
if (x.side == Buy) {
auto& lst = bids[x.price];
lst.push_back(x);
order_ref[x.id] = {x.price, Buy, prev(lst.end())};
} else {
auto& lst = asks[x.price];
lst.push_back(x);
order_ref[x.id] = {x.price, Sell, prev(lst.end())};
}
}
return true;
}
bool modifyOrder(u_int64_t orderID, u_int32_t newQuantity) {
if (order_ref.find(orderID) == order_ref.end()) {
cout << "[ERROR] Order ID " << orderID << " not found\n";
return false;
}
if (newQuantity == 0) {
cancelOrder(orderID);
return true;
}
auto& ref = order_ref[orderID];
ref.it->quantity = newQuantity;
cout << "Order " << orderID << " modified to qty " << newQuantity << "\n";
return true;
}
void cancelOrder(u_int64_t orderID) {
if (order_ref.find(orderID) == order_ref.end()) return;
auto ref = order_ref[orderID];
if (ref.side == Buy) {
bids[ref.price].erase(ref.it);
if (bids[ref.price].empty()) bids.erase(ref.price);
}
else {
asks[ref.price].erase(ref.it);
if (asks[ref.price].empty()) asks.erase(ref.price);
}
order_ref.erase(orderID);
cout << "Order " << orderID << " cancelled\n";
}
void display() {
cout << "\n===========================================" << endl;
cout << " ORDER BOOK " << endl;
cout << "===========================================" << endl;
cout << "--- ASKS (Sellers) ---" << endl;
// Asks displayed from highest to lowest so Best Ask is near the bids
for (auto it = asks.rbegin(); it != asks.rend(); ++it) {
u_int32_t totalQty = 0;
for (const auto& o : it->second) totalQty += o.quantity;
cout << " Price: " << it->first << " | Quantity: " << totalQty << endl;
}
cout << "-------------------------------------------" << endl;
cout << " Spread: " << ( (!asks.empty() && !bids.empty()) ? (asks.begin()->first - bids.begin()->first) : 0 ) << endl;
cout << "-------------------------------------------" << endl;
cout << "--- BIDS (Buyers) ---" << endl;
for (auto const& [price, orders] : bids) {
u_int32_t totalQty = 0;
for (const auto& o : orders) totalQty += o.quantity;
cout << " Price: " << price << " | Quantity: " << totalQty << endl;
}
cout << "===========================================\n" << endl;
}
ll getBestBid() const { return bids.empty() ? -1 : bids.begin()->first; }
ll getBestAsk() const { return asks.empty() ? -1 : asks.begin()->first; }
ll getSpread() const {
if (bids.empty() || asks.empty()) return -1;
return asks.begin()->first - bids.begin()->first;
}
bool empty() const { return bids.empty() && asks.empty(); }
private:
map<ll, lo, greater<ll>> bids;
map<ll, lo> asks;
struct OrderEntry {
ll price;
Side side;
lo::iterator it;
};
unordered_map<u_int64_t, OrderEntry> order_ref;
void match(Order& new_order) {
if (new_order.side == Buy)
matchSide(new_order, asks);
else
matchSide(new_order, bids);
}
template<typename T_MAP>
void matchSide(Order& new_order, T_MAP& op_side) {
auto it = op_side.begin();
while (it != op_side.end() && new_order.quantity > 0) {
ll best = it -> first;
if((new_order.side == Buy) ? (new_order.price >= best) : (new_order.price <= best)) {
auto& listatprice = it -> second;
while(!listatprice.empty() && new_order.quantity > 0) {
Order& rest = listatprice.front();
u_int32_t tradeQuant = min(new_order.quantity, rest.quantity);
cout << "[TRADE] ID " << new_order.id << " matched with ID " << rest.id
<< " | Qty: " << tradeQuant << " | Price: " << best << endl;
new_order.quantity -= tradeQuant;
rest.quantity -= tradeQuant;
if(!rest.quantity) {
order_ref.erase(rest.id);
listatprice.pop_front();
}
}
if (listatprice.empty())
it = op_side.erase(it);
else
break;
} else {
break; // price doesn't cross, no more matches possible
}
}
}
};
int main() {
OrderBook book;
cout << "--- Placing initial orders ---" << endl;
book.addOrder({1, 100, 10, Buy});
book.addOrder({2, 105, 5, Sell});
book.addOrder({3, 106, 15, Sell});
book.display();
cout << "--- Executing aggressive buy (ID 4) ---" << endl;
book.addOrder({4, 107, 10, Buy});
book.display();
cout << "--- Modifying order ID 3 (qty 15 -> 8) ---" << endl;
book.modifyOrder(3, 8);
book.display();
cout << "--- Cancelling order ID 3 ---" << endl;
book.cancelOrder(3);
book.display();
cout << "--- Attempting duplicate ID ---" << endl;
book.addOrder({1, 99, 5, Buy});
cout << "\n--- Info ---" << endl;
cout << "Best Bid: " << book.getBestBid() << endl;
cout << "Best Ask: " << book.getBestAsk() << endl;
cout << "Spread: " << book.getSpread() << endl;
return 0;
}