-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderbook.cpp
More file actions
111 lines (96 loc) · 3.25 KB
/
Copy pathorderbook.cpp
File metadata and controls
111 lines (96 loc) · 3.25 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
#include <unordered_map>
#include <stdexcept>
enum Side { Buy, Sell, ShortSell };
struct Order {
int orderid;
double price;
int quantity;
Side side;
};
struct NewOrder : public Order {};
struct CancelOrder : public Order {};
struct ModifyOrder : public Order {};
struct Quote {
double price;
int quantity;
bool operator<(const Quote& other) const{
return price < other.price;
};
bool operator==(const Quote& other) const{
return price == other.price;
}
};
using BuySide = std::unordered_map<double,Quote>;
using SellSide = std::unordered_map<double,Quote>;
class Orderbook {
private:
BuySide buyside;
SellSide sellside;
public:
template <typename CONTAINER>
void add(const Order& order, CONTAINER& container) {
Quote newQuote = {order.price, order.quantity};
auto iter = container.find(newQuote);
if (iter != container.end()) {
newQuote.quantity += order.quantity;
container.erase(iter);
}
container.insert(newQuote);
}
template <typename CONTAINER>
void remove(const Order& order, CONTAINER& container) {
Quote cancelledQuote{order.price, order.quantity};
auto iter = container.find(cancelledQuote);
if (iter != container.end()) {
if (iter->quantity > order.quantity){
cancelledQuote.quantity -= order.quantity;
container.erase(iter);
container.insert(cancelledQuote);
}
else if (iter->quantity == order.quantity){
container.erase(iter);
}else{
throw std::runtime_error("Order quantity greater than the existing order");
}
}
else {
throw std::runtime_error("Order does not exists");
}
}
template <typename CONTAINER>
void modify(const Order& order, CONTAINER& container) {
Quote modifiedQuote{order.price, order.quantity};
auto iter = container.find(modifiedQuote);
if (iter != container.end()) {
modifiedQuote.quantity += order.quantity;
container.erase(iter);
container.insert(modifiedQuote);
} else {
throw std::runtime_error("Order does not exists");
}
}
void add(const NewOrder& order) {
if (order.side == Side::Buy)
add(order, buyside);
else if (order.side == Side::Sell || order.side == Side::ShortSell)
add(order, sellside);
else
throw std::runtime_error("Invalid Side in the New Order");
}
void remove(const CancelOrder& order) {
if (order.side == Side::Buy)
remove(order, buyside);
else if (order.side == Side::Sell || order.side == Side::ShortSell)
remove(order, sellside);
else
throw std::runtime_error("Invalid Side in the New Order");
}
void modify(const ModifyOrder& order) {
if (order.side == Side::Buy)
modify(order, buyside);
else if (order.side == Side::Sell || order.side == Side::ShortSell)
modify(order, sellside);
else
throw std::runtime_error("Invalid Side in the New Order");
}
};