commit | author | age
|
bb2353
|
1 |
#include "book.hpp" |
JG |
2 |
#include <chrono> |
|
3 |
#include <iostream> |
|
4 |
|
|
5 |
Order::Order(double price, OrderSideEnum side, int volume, |
|
6 |
std::chrono::nanoseconds timestamp, std::string id) |
|
7 |
: price{price}, side{side}, remaining_volume{volume}, |
|
8 |
filled_volume(0), timestamp{timestamp}, id{id} {} |
|
9 |
|
|
10 |
Level::Level(Order &order) |
|
11 |
: price{order.price}, volume(order.remaining_volume), side{order.side}, |
|
12 |
timestamp{order.timestamp}, id{order.id} {} |
|
13 |
|
|
14 |
bool operator<(const Level &a, const Level &b) { |
|
15 |
if (a.price < b.price) |
|
16 |
return true; |
|
17 |
else if (a.price == b.price && a.timestamp < b.timestamp) |
|
18 |
return true; |
|
19 |
else |
|
20 |
return false; |
|
21 |
} |
|
22 |
|
|
23 |
bool operator>(const Level &a, const Level &b) { |
|
24 |
if (a.price > b.price) |
|
25 |
return true; |
|
26 |
else if (a.price == b.price && a.timestamp > b.timestamp) |
|
27 |
return true; |
|
28 |
else |
|
29 |
return false; |
|
30 |
} |
|
31 |
|
|
32 |
bool operator<=(const Level &a, const Level &b) { |
|
33 |
if (a.price <= b.price) |
|
34 |
return true; |
|
35 |
else if (a.price == b.price && a.timestamp <= b.timestamp) |
|
36 |
return true; |
|
37 |
else |
|
38 |
return false; |
|
39 |
} |
|
40 |
|
|
41 |
bool operator>=(const Level &a, const Level &b) { |
|
42 |
if (a.price >= b.price) |
|
43 |
return true; |
|
44 |
else if (a.price == b.price && a.timestamp >= b.timestamp) |
|
45 |
return true; |
|
46 |
else |
|
47 |
return false; |
|
48 |
} |
|
49 |
|
|
50 |
bool operator==(const Level &a, const Level &b) { |
|
51 |
if (a.price == b.price && a.timestamp == b.timestamp) |
|
52 |
return true; |
|
53 |
else |
|
54 |
return false; |
|
55 |
} |
|
56 |
|
|
57 |
Book::Book() |
|
58 |
: bidSide(), askSide(), productType(TEST), product("a"), stationId("b"), |
|
59 |
unit("c"), expiry(std::chrono::nanoseconds(0)), aggFee(1), pasFee(-1), |
|
60 |
broFee(2) {} |
|
61 |
|
|
62 |
Book::Book(ProductTypeEnum productType, std::string product, |
|
63 |
std::string stationId, std::string unit, |
|
64 |
std::chrono::nanoseconds expiry, double aggFee, double pasFee, |
|
65 |
double broFee) |
|
66 |
: bidSide{}, askSide{}, productType{productType}, product(product), |
|
67 |
stationId(stationId), unit(unit), expiry(expiry), aggFee(aggFee), |
|
68 |
pasFee(pasFee), broFee(broFee) {} |
|
69 |
|
|
70 |
void Book::ask(Order &order) { |
|
71 |
while (this->bidSide.size() && this->bidSide.top().price > order.price) { |
|
72 |
if (this->bidSide.top().volume > order.remaining_volume) { |
|
73 |
int temp = this->bidSide.top().volume; |
|
74 |
order.filled_volume += temp; |
|
75 |
this->bidSide.topRemoveVolume(order.remaining_volume); |
|
76 |
order.remaining_volume -= temp; |
|
77 |
} else { |
|
78 |
order.remaining_volume -= this->bidSide.top().volume; |
|
79 |
order.filled_volume += this->bidSide.top().volume; |
|
80 |
this->bidSide.pop(); |
|
81 |
} |
|
82 |
if (order.remaining_volume > 0) { |
|
83 |
this->askSide.emplace(order); |
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
void Book::bid(Order &order) { |
|
89 |
while (this->askSide.size() && this->askSide.top().price > order.price) { |
|
90 |
if (this->askSide.top().volume > order.remaining_volume) { |
|
91 |
int temp = this->askSide.top().volume; |
|
92 |
order.filled_volume += temp; |
|
93 |
this->askSide.topRemoveVolume(order.remaining_volume); |
|
94 |
order.remaining_volume -= temp; |
|
95 |
} else { |
|
96 |
order.remaining_volume -= this->askSide.top().volume; |
|
97 |
order.filled_volume += this->askSide.top().volume; |
|
98 |
this->askSide.pop(); |
|
99 |
} |
|
100 |
if (order.remaining_volume > 0) { |
|
101 |
this->bidSide.emplace(order); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
void Book::printBook() { |
|
107 |
std::cout << "Sell side"; |
|
108 |
this->askSide.printTop(); |
|
109 |
std::cout << "Buy side"; |
|
110 |
this->bidSide.printTop(); |
|
111 |
} |
|
112 |
|
|
113 |
Book testBook(int orders, bool printBook) { |
|
114 |
Book b = Book(); |
|
115 |
std::chrono::nanoseconds time(1); |
|
116 |
for (int i = 0; i < orders; i++) { |
|
117 |
Order t(i, Buy, 10, time++, "a"); |
|
118 |
b.bid(t); |
|
119 |
} |
|
120 |
for (int i = orders + 1; i < 2 * orders; i++) { |
|
121 |
Order t(i, Sell, 10, time++, "b"); |
|
122 |
b.ask(t); |
|
123 |
} |
|
124 |
if (printBook) |
|
125 |
b.printBook(); |
|
126 |
return b; |
|
127 |
} |