Skip to content

Commit 2c1b6e1

Browse files
committed
Pybind11 support backward compatibility
1 parent d1aff5d commit 2c1b6e1

File tree

3 files changed

+60
-259
lines changed

3 files changed

+60
-259
lines changed

src/lightmatchingengine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ PYBIND11_MODULE(lightmatchingengine, m) {
2828
double,
2929
Side,
3030
int>());
31+
32+
// Class LightMatchingEngine
33+
py::class_<LightMatchingEngine>(m, "LightMatchingEngine")
34+
.def(py::init())
35+
.def("add_order", &LightMatchingEngine::add_order);
3136
}

src/lightmatchingengine.h

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <deque>
88
#include <deque>
99
#include <algorithm>
10+
#include <stdexcept>
11+
#include <sstream>
1012

1113
using namespace std;
1214

@@ -22,20 +24,26 @@ using namespace std;
2224
#define qty_t double
2325
#define id_t long long
2426

25-
enum Side {
27+
enum class Side {
2628
BUY = 1,
2729
SELL = 2
2830
};
2931

3032
struct Order {
3133
id_t order_id;
32-
const string& instmt;
34+
string instmt;
3335
price_t price;
3436
qty_t qty;
3537
qty_t cum_qty;
3638
qty_t leaves_qty;
3739
Side side;
3840

41+
/* Order() = default; */
42+
/* Order(const Order&) = default; */
43+
/* Order& operator=(Order&) = default; */
44+
/* Order(Order&&) = default; */
45+
/* Order& operator=(Order&&) = default; */
46+
3947
Order(id_t order_id, const string& instmt, price_t price, qty_t qty, Side side):
4048
order_id(order_id),
4149
instmt(instmt),
@@ -239,6 +247,51 @@ class LightMatchingEngine {
239247
return make_tuple(order, trades);
240248
}
241249

250+
Order& cancel_order(id_t order_id, const string& instmt) {
251+
auto order_book_it = __order_books.find(instmt);
252+
if (order_book_it == __order_books.end()) {
253+
auto err_message = string("Order books do not have the instrument ") + instmt;
254+
throw runtime_error(err_message);
255+
}
256+
257+
auto order_book = order_book_it->second;
258+
auto order_it = order_book.order_id_map.find(order_id);
259+
if (order_it == order_book.order_id_map.end()) {
260+
ostringstream sstream;
261+
sstream << "Cannot find order " << order_id << " from instrument " << instmt;
262+
throw runtime_error(sstream.str());
263+
}
264+
265+
auto order = order_it->second;
266+
auto nprice = NORMALIZE_PRICE(order.price);
267+
268+
if (order.side == Side::BUY) {
269+
auto order_queue_it = order_book.bids.find(nprice);
270+
assert(order_queue_it != order_book.bids.end());
271+
auto order_queue = order_queue_it->second;
272+
auto found_order = find_if(
273+
order_queue.begin(), order_queue.end(), [&order](auto o) { return o.order_id == order.order_id; });
274+
275+
// Remove the order from the matching engine
276+
order_queue.erase(found_order);
277+
} else {
278+
auto order_queue_it = order_book.asks.find(nprice);
279+
assert(order_queue_it != order_book.asks.end());
280+
auto order_queue = order_queue_it->second;
281+
auto found_order = find_if(
282+
order_queue.begin(), order_queue.end(), [&order](auto o) { return o.order_id == order.order_id; });
283+
284+
// Remove the order from the matching engine
285+
order_queue.erase(found_order);
286+
}
287+
288+
// Finally set the leaves qty to 0
289+
order.leaves_qty = 0.0;
290+
order_book.order_id_map.erase(order_it);
291+
292+
return order;
293+
}
294+
242295
private:
243296
unordered_map<string, OrderBook> __order_books;
244297
int __curr_order_id;

src/lightmatchingengine.pyx

Lines changed: 0 additions & 257 deletions
This file was deleted.

0 commit comments

Comments
 (0)