-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
219 lines (194 loc) · 7.81 KB
/
Copy pathengine.cpp
File metadata and controls
219 lines (194 loc) · 7.81 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "engine.hpp"
#include <vector>
#include <unordered_map>
#include <optional>
#include <stdexcept>
#include <cstdint>
#include <algorithm>
#include <functional>
// Core matching helper: walk through price levels in ordersMap and fill order
// until quantity reaches zero or no more qualifying price levels remain
template <typename OrderMap, typename Condition>
static uint32_t process_orders(Order &order,
OrderMap &ordersMap,
std::unordered_map<Id, OrderLocation> &orderIndex,
Condition cond) {
uint32_t matchCount = 0;
auto it = ordersMap.begin();
while (it != ordersMap.end() && order.quantity > 0 && cond(it->first, order.price)) {
auto &priceLevel = it->second;
auto &ordersAtPrice = priceLevel.orders;
size_t idx = 0;
while (idx < ordersAtPrice.size() && order.quantity > 0) {
Order &matchedOrder = ordersAtPrice[idx];
Quantity trade = std::min<Quantity>(order.quantity, matchedOrder.quantity);
order.quantity -= trade;
matchedOrder.quantity -= trade;
priceLevel.cachedVolume -= trade;
++matchCount;
if (matchedOrder.quantity == 0) {
// swap with last and pop (O(1) removal)
Id removedId = matchedOrder.id;
if (idx < ordersAtPrice.size() - 1) {
// swap with last element
std::swap(ordersAtPrice[idx], ordersAtPrice.back());
Id swappedId = ordersAtPrice[idx].id;
auto &swappedLoc = orderIndex[swappedId];
swappedLoc.index = idx;
}
orderIndex.erase(removedId);
ordersAtPrice.pop_back();
} else {
++idx;
}
}
if (ordersAtPrice.empty()) {
it = ordersMap.erase(it);
} else {
++it;
}
}
return matchCount;
}
// modify or cancel an order using index (O(1) lookup)
static bool modify_order_using_index(Orderbook &orderbook,
Id order_id,
Quantity new_quantity) {
auto indexIt = orderbook.orderIndex.find(order_id);
if (indexIt == orderbook.orderIndex.end()) {
return false;
}
OrderLocation &loc = indexIt->second;
PriceLevel *priceLevel = nullptr;
if (loc.side == Side::BUY) {
auto mapIt = orderbook.buyOrders.find(loc.price);
if (mapIt == orderbook.buyOrders.end()) return false;
priceLevel = &mapIt->second;
} else {
auto mapIt = orderbook.sellOrders.find(loc.price);
if (mapIt == orderbook.sellOrders.end()) return false;
priceLevel = &mapIt->second;
}
if (loc.index >= priceLevel->orders.size()) return false;
Order &order = priceLevel->orders[loc.index];
if (order.id != order_id) return false;
int32_t volumeDelta = static_cast<int32_t>(new_quantity) - static_cast<int32_t>(order.quantity);
if (new_quantity == 0) {
// remove order: swap with last and pop
priceLevel->cachedVolume -= order.quantity;
if (loc.index < priceLevel->orders.size() - 1) {
// swap with last element
std::swap(priceLevel->orders[loc.index], priceLevel->orders.back());
// update index for the swapped order
Id swappedId = priceLevel->orders[loc.index].id;
orderbook.orderIndex[swappedId].index = loc.index;
}
priceLevel->orders.pop_back();
orderbook.orderIndex.erase(indexIt);
if (priceLevel->orders.empty()) {
if (loc.side == Side::BUY) {
orderbook.buyOrders.erase(loc.price);
} else {
orderbook.sellOrders.erase(loc.price);
}
}
} else {
order.quantity = new_quantity;
priceLevel->cachedVolume += volumeDelta;
}
return true;
}
// lookup an order by ID using the index (O(1) lookup)
static std::optional<Order> lookup_order_using_index(const Orderbook &orderbook,
Id order_id) {
auto indexIt = orderbook.orderIndex.find(order_id);
if (indexIt == orderbook.orderIndex.end()) {
return std::nullopt;
}
const OrderLocation &loc = indexIt->second;
if (loc.side == Side::BUY) {
auto mapIt = orderbook.buyOrders.find(loc.price);
if (mapIt == orderbook.buyOrders.end()) return std::nullopt;
const auto &priceLevel = mapIt->second;
if (loc.index >= priceLevel.orders.size()) return std::nullopt;
return priceLevel.orders[loc.index];
} else {
auto mapIt = orderbook.sellOrders.find(loc.price);
if (mapIt == orderbook.sellOrders.end()) return std::nullopt;
const auto &priceLevel = mapIt->second;
if (loc.index >= priceLevel.orders.size()) return std::nullopt;
return priceLevel.orders[loc.index];
}
}
extern "C" {
// match an incoming order against the book
uint32_t match_order(Orderbook &orderbook, const Order &incoming) {
Order order = incoming;
uint32_t matchCount = 0;
if (order.side == Side::BUY) {
// match sell orders priced <= limit
matchCount = process_orders(order,
orderbook.sellOrders,
orderbook.orderIndex,
[](Price p, Price limit) { return p <= limit; });
if (order.quantity > 0) {
// add remaining order to book
PriceLevel &priceLevel = orderbook.buyOrders[order.price];
size_t index = priceLevel.orders.size();
priceLevel.orders.push_back(order);
priceLevel.cachedVolume += order.quantity;
orderbook.orderIndex[order.id] = {Side::BUY, order.price, index};
}
} else {
// match buy orders priced >= limit
matchCount = process_orders(order,
orderbook.buyOrders,
orderbook.orderIndex,
[](Price p, Price limit) { return p >= limit; });
if (order.quantity > 0) {
// add remaining order to book
PriceLevel &priceLevel = orderbook.sellOrders[order.price];
size_t index = priceLevel.orders.size();
priceLevel.orders.push_back(order);
priceLevel.cachedVolume += order.quantity;
orderbook.orderIndex[order.id] = {Side::SELL, order.price, index};
}
}
return matchCount;
}
// modify or cancel an existing order by ID
void modify_order_by_id(Orderbook &orderbook,
Id order_id,
Quantity new_quantity) {
modify_order_using_index(orderbook, order_id, new_quantity);
}
// get total quantity at a given price level (O(1) using cached volume)
uint32_t get_volume_at_level(Orderbook &orderbook,
Side side,
Price price) {
if (side == Side::BUY) {
auto it = orderbook.buyOrders.find(price);
if (it != orderbook.buyOrders.end()) {
return it->second.cachedVolume;
}
} else {
auto it = orderbook.sellOrders.find(price);
if (it != orderbook.sellOrders.end()) {
return it->second.cachedVolume;
}
}
return 0;
}
Order lookup_order_by_id(Orderbook &orderbook, Id order_id) {
if (auto opt = lookup_order_using_index(orderbook, order_id)) {
return *opt;
}
throw std::runtime_error("Order not found");
}
bool order_exists(Orderbook &orderbook, Id order_id) {
return orderbook.orderIndex.find(order_id) != orderbook.orderIndex.end();
}
Orderbook *create_orderbook() {
return new Orderbook();
}
}