-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (216 loc) · 11.2 KB
/
main.cpp
File metadata and controls
255 lines (216 loc) · 11.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <ostream>
#include "src/order_book.h"
#include "src/models/book_ticker.h"
static constexpr std::string_view Delimiter = "\n**************************************************************\n";
struct StepPrinter {
std::string_view StepName;
std::string PreviousTop;
std::string ExpectedTop;
StepPrinter(std::string_view stepName, std::string previousTop, std::string expectedTop)
: StepName(stepName)
, PreviousTop(std::move(previousTop))
, ExpectedTop(std::move(expectedTop)) {
}
};
std::ostream& operator<<(std::ostream& out, StepPrinter step) {
std::cout << Delimiter;
std::cout << step.StepName << std::endl;
std::cout << "Previous top: " << step.PreviousTop << std::endl;
std::cout << "Expected top: " << step.ExpectedTop << std::endl;
std::cout << std::endl;
return out;
}
template <typename ...TArgs>
std::string ToString(TArgs&& ...args) {
boost::format formatter("[%9s] %.3f | %.3f [%-9s]");
return (std::stringstream() << ((formatter % args), ...)).str();
}
int main() {
using TPrice = double;
using TQuantity = double;
using TPriceQuantity = OrderBook::Models::PriceQuantity<TPrice, TQuantity>;
OrderBook::BinanceBook<TPrice, TQuantity, 20> book;
boost::format formatter("[%9s] %.3f | %.3f [%-9s]\n");
{
std::cout << StepPrinter("Depth update empty book",
"N/A",
ToString(0.00431, 20078.540, 20078.91, 0.03437));
book.DepthUpdate([]() -> OrderBook::Utils::Generator<TPriceQuantity> {
co_yield { .Price = 20078.54000000, .Quantity = 0.00431000 };
co_yield { .Price = 20078.39000000, .Quantity = 0.00100000 };
co_yield { .Price = 20078.27000000, .Quantity = 0.00070000 };
co_yield { .Price = 20078.21000000, .Quantity = 0.00066000 };
co_yield { .Price = 20077.91000000, .Quantity = 0.03781000 };
co_yield { .Price = 20077.90000000, .Quantity = 0.00110000 };
co_yield { .Price = 20077.86000000, .Quantity = 0.00070000 };
co_yield { .Price = 20077.80000000, .Quantity = 0.00650000 };
co_yield { .Price = 20077.73000000, .Quantity = 0.00055000 };
co_yield { .Price = 20077.71000000, .Quantity = 0.00100000 };
co_yield { .Price = 20077.69000000, .Quantity = 0.00984000 };
co_yield { .Price = 20077.66000000, .Quantity = 0.00066000 };
co_yield { .Price = 20077.61000000, .Quantity = 0.04000000 };
co_yield { .Price = 20077.60000000, .Quantity = 0.02484000 };
co_yield { .Price = 20077.56000000, .Quantity = 0.05481000 };
co_yield { .Price = 20077.52000000, .Quantity = 0.28882000 };
co_yield { .Price = 20077.51000000, .Quantity = 0.00064000 };
co_yield { .Price = 20077.45000000, .Quantity = 0.00070000 };
co_yield { .Price = 20077.43000000, .Quantity = 0.05181000 };
co_yield { .Price = 20077.39000000, .Quantity = 0.00689000 };
}(), []() -> OrderBook::Utils::Generator<TPriceQuantity> {
co_yield { .Price = 20078.91000000, .Quantity = 0.03437000 };
co_yield { .Price = 20078.95000000, .Quantity = 0.00100000 };
co_yield { .Price = 20078.99000000, .Quantity = 0.00498000 };
co_yield { .Price = 20079.01000000, .Quantity = 0.04981000 };
co_yield { .Price = 20079.09000000, .Quantity = 0.00070000 };
co_yield { .Price = 20079.15000000, .Quantity = 0.24902000 };
co_yield { .Price = 20079.30000000, .Quantity = 0.04110000 };
co_yield { .Price = 20079.31000000, .Quantity = 0.00066000 };
co_yield { .Price = 20079.35000000, .Quantity = 0.00864000 };
co_yield { .Price = 20079.42000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.44000000, .Quantity = 0.09402000 };
co_yield { .Price = 20079.46000000, .Quantity = 0.09402000 };
co_yield { .Price = 20079.49000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.50000000, .Quantity = 0.00070000 };
co_yield { .Price = 20079.51000000, .Quantity = 0.17430000 };
co_yield { .Price = 20079.53000000, .Quantity = 0.09602000 };
co_yield { .Price = 20079.60000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.61000000, .Quantity = 0.22853000 };
co_yield { .Price = 20079.62000000, .Quantity = 0.08741000 };
co_yield { .Price = 20079.66000000, .Quantity = 0.00400000 };
}());
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Best Bid/Ask update - New price is better than previous",
ToString(0.00431, 20078.540, 20078.910, 0.03437),
ToString(0.00125, 20079.110, 20077.310, 0.02425));
book.BBOUpdate({
.BestBidPrice = 20079.110,
.BestBidQty = 0.00125,
.BestAskPrice = 20077.310,
.BestAskQty = 0.02425
});
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Best Bid/Ask update - New price is in the middle of previous top 20 levels",
ToString(0.00125, 20079.110, 20077.310, 0.02425),
ToString(0.00254, 20077.830, 20079.410, 0.05454));
book.BBOUpdate({
.BestBidPrice = 20077.830,
.BestBidQty = 0.00254,
.BestAskPrice = 20079.410,
.BestAskQty = 0.05454
});
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Best Bid/Ask update - Only quantity changed, but price is same",
ToString(0.00254, 20077.830, 20079.410, 0.05454),
ToString(0.00105, 20077.830, 20079.410, 0.08482));
book.BBOUpdate({
.BestBidPrice = 20077.830,
.BestBidQty = 0.00105,
.BestAskPrice = 20079.410,
.BestAskQty = 0.08482
});
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Best Bid/Ask update - New price is below previous top 20 levels",
ToString(0.00105, 20077.830, 20079.410, 0.08482),
ToString(0.00254, 20076.140, 20080.230, 0.05454));
book.BBOUpdate({
.BestBidPrice = 20076.140,
.BestBidQty = 0.00254,
.BestAskPrice = 20080.230,
.BestAskQty = 0.05454
});
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << Delimiter;
std::cout << "Is book empty: ";
std::cout << (book.IsEmpty() ? "True" : "False") << std::endl << std::endl;
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << Delimiter;
std::cout << "Clear book" << std::endl;
book.Clear();
std::cout << "Is book empty: ";
std::cout << (book.IsEmpty() ? "True" : "False") << std::endl << std::endl;
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Best Bid/Ask update - BBO update in the empty book",
"N/a",
ToString(0.00254, 20077.830, 20079.410, 0.05454));
book.BBOUpdate({
.BestBidPrice = 20077.830,
.BestBidQty = 0.00254,
.BestAskPrice = 20079.410,
.BestAskQty = 0.05454
});
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
{
std::cout << StepPrinter("Depth update of not empty book",
ToString(0.00254, 20077.830, 20079.410, 0.05454),
ToString(0.00431, 20078.540, 20078.91, 0.03437));
book.DepthUpdate([]() -> OrderBook::Utils::Generator<TPriceQuantity> {
co_yield { .Price = 20078.54000000, .Quantity = 0.00431000 };
co_yield { .Price = 20078.39000000, .Quantity = 0.00100000 };
co_yield { .Price = 20078.27000000, .Quantity = 0.00070000 };
co_yield { .Price = 20078.21000000, .Quantity = 0.00066000 };
co_yield { .Price = 20077.91000000, .Quantity = 0.03781000 };
co_yield { .Price = 20077.90000000, .Quantity = 0.00110000 };
co_yield { .Price = 20077.86000000, .Quantity = 0.00070000 };
co_yield { .Price = 20077.80000000, .Quantity = 0.00650000 };
co_yield { .Price = 20077.73000000, .Quantity = 0.00055000 };
co_yield { .Price = 20077.71000000, .Quantity = 0.00100000 };
co_yield { .Price = 20077.69000000, .Quantity = 0.00984000 };
co_yield { .Price = 20077.66000000, .Quantity = 0.00066000 };
co_yield { .Price = 20077.61000000, .Quantity = 0.04000000 };
co_yield { .Price = 20077.60000000, .Quantity = 0.02484000 };
co_yield { .Price = 20077.56000000, .Quantity = 0.05481000 };
co_yield { .Price = 20077.52000000, .Quantity = 0.28882000 };
co_yield { .Price = 20077.51000000, .Quantity = 0.00064000 };
co_yield { .Price = 20077.45000000, .Quantity = 0.00070000 };
co_yield { .Price = 20077.43000000, .Quantity = 0.05181000 };
co_yield { .Price = 20077.39000000, .Quantity = 0.00689000 };
}(), []() -> OrderBook::Utils::Generator<TPriceQuantity> {
co_yield { .Price = 20078.91000000, .Quantity = 0.03437000 };
co_yield { .Price = 20078.95000000, .Quantity = 0.00100000 };
co_yield { .Price = 20078.99000000, .Quantity = 0.00498000 };
co_yield { .Price = 20079.01000000, .Quantity = 0.04981000 };
co_yield { .Price = 20079.09000000, .Quantity = 0.00070000 };
co_yield { .Price = 20079.15000000, .Quantity = 0.24902000 };
co_yield { .Price = 20079.30000000, .Quantity = 0.04110000 };
co_yield { .Price = 20079.31000000, .Quantity = 0.00066000 };
co_yield { .Price = 20079.35000000, .Quantity = 0.00864000 };
co_yield { .Price = 20079.42000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.44000000, .Quantity = 0.09402000 };
co_yield { .Price = 20079.46000000, .Quantity = 0.09402000 };
co_yield { .Price = 20079.49000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.50000000, .Quantity = 0.00070000 };
co_yield { .Price = 20079.51000000, .Quantity = 0.17430000 };
co_yield { .Price = 20079.53000000, .Quantity = 0.09602000 };
co_yield { .Price = 20079.60000000, .Quantity = 0.00100000 };
co_yield { .Price = 20079.61000000, .Quantity = 0.22853000 };
co_yield { .Price = 20079.62000000, .Quantity = 0.08741000 };
co_yield { .Price = 20079.66000000, .Quantity = 0.00400000 };
}());
std::cout << "Book: " << std::endl;
std::cout << book << std::endl;
}
return 0;
}