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