-
Notifications
You must be signed in to change notification settings - Fork 256
/
order_book.cpp
521 lines (421 loc) · 17.1 KB
/
order_book.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*!
\file order_book.cpp
\brief Order book implementation
\author Ivan Shynkarenka
\date 02.08.2017
\copyright MIT License
*/
#include "trader/matching/market_manager.h"
#include "trader/matching/order_book.h"
namespace CppTrader {
namespace Matching {
OrderBook::OrderBook(MarketManager& manager, const Symbol& symbol)
: _manager(manager),
_symbol(symbol),
_best_bid(nullptr),
_best_ask(nullptr),
_best_buy_stop(nullptr),
_best_sell_stop(nullptr),
_best_trailing_buy_stop(nullptr),
_best_trailing_sell_stop(nullptr),
_last_bid_price(0),
_last_ask_price(std::numeric_limits<uint64_t>::max()),
_matching_bid_price(0),
_matching_ask_price(std::numeric_limits<uint64_t>::max()),
_trailing_bid_price(0),
_trailing_ask_price(std::numeric_limits<uint64_t>::max())
{
}
OrderBook::~OrderBook()
{
// Release bid price levels
for (auto& bid : _bids)
_manager._level_pool.Release(&bid);
_bids.clear();
// Release ask price levels
for (auto& ask : _asks)
_manager._level_pool.Release(&ask);
_asks.clear();
// Release buy stop orders levels
for (auto& buy_stop : _buy_stop)
_manager._level_pool.Release(&buy_stop);
_buy_stop.clear();
// Release sell stop orders levels
for (auto& sell_stop : _sell_stop)
_manager._level_pool.Release(&sell_stop);
_sell_stop.clear();
// Release trailing buy stop orders levels
for (auto& trailing_buy_stop : _trailing_buy_stop)
_manager._level_pool.Release(&trailing_buy_stop);
_trailing_buy_stop.clear();
// Release trailing sell stop orders levels
for (auto& trailing_sell_stop : _trailing_sell_stop)
_manager._level_pool.Release(&trailing_sell_stop);
_trailing_sell_stop.clear();
}
LevelNode* OrderBook::AddLevel(OrderNode* order_ptr)
{
LevelNode* level_ptr = nullptr;
if (order_ptr->IsBuy())
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::BID, order_ptr->Price);
// Insert the price level into the bid collection
_bids.insert(*level_ptr);
// Update the best bid price level
if ((_best_bid == nullptr) || (level_ptr->Price > _best_bid->Price))
_best_bid = level_ptr;
}
else
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::ASK, order_ptr->Price);
// Insert the price level into the ask collection
_asks.insert(*level_ptr);
// Update the best ask price level
if ((_best_ask == nullptr) || (level_ptr->Price < _best_ask->Price))
_best_ask = level_ptr;
}
return level_ptr;
}
LevelNode* OrderBook::DeleteLevel(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
if (order_ptr->IsBuy())
{
// Update the best bid price level
if (level_ptr == _best_bid)
_best_bid = (_best_bid->left != nullptr) ? _best_bid->left : ((_best_bid->parent != nullptr) ? _best_bid->parent : _best_bid->right);
// Erase the price level from the bid collection
_bids.erase(Levels::iterator(&_bids, level_ptr));
}
else
{
// Update the best ask price level
if (level_ptr == _best_ask)
_best_ask = (_best_ask->right != nullptr) ? _best_ask->right : ((_best_ask->parent != nullptr) ? _best_ask->parent : _best_ask->left);
// Erase the price level from the ask collection
_asks.erase(Levels::iterator(&_asks, level_ptr));
}
// Release the price level
_manager._level_pool.Release(level_ptr);
return nullptr;
}
LevelUpdate OrderBook::AddOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->IsBuy() ? (LevelNode*)GetBid(order_ptr->Price) : (LevelNode*)GetAsk(order_ptr->Price);
// Create a new price level if no one found
UpdateType update = UpdateType::UPDATE;
if (level_ptr == nullptr)
{
level_ptr = AddLevel(order_ptr);
update = UpdateType::ADD;
}
// Update the price level volume
level_ptr->TotalVolume += order_ptr->LeavesQuantity;
level_ptr->HiddenVolume += order_ptr->HiddenQuantity();
level_ptr->VisibleVolume += order_ptr->VisibleQuantity();
// Link the new order to the orders list of the price level
level_ptr->OrderList.push_back(*order_ptr);
++level_ptr->Orders;
// Cache the price level in the given order
order_ptr->Level = level_ptr;
// Price level was changed. Return top of the book modification flag.
return LevelUpdate(update, *order_ptr->Level, (order_ptr->Level == (order_ptr->IsBuy() ? _best_bid : _best_ask)));
}
LevelUpdate OrderBook::ReduceOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= quantity;
level_ptr->HiddenVolume -= hidden;
level_ptr->VisibleVolume -= visible;
// Unlink the empty order from the orders list of the price level
if (order_ptr->LeavesQuantity == 0)
{
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
}
Level level(*level_ptr);
// Delete the empty price level
UpdateType update = UpdateType::UPDATE;
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteLevel(order_ptr);
update = UpdateType::DELETE;
}
// Price level was changed. Return top of the book modification flag.
return LevelUpdate(update, level, ((order_ptr->Level == nullptr) || (order_ptr->Level == (order_ptr->IsBuy() ? _best_bid : _best_ask))));
}
LevelUpdate OrderBook::DeleteOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= order_ptr->LeavesQuantity;
level_ptr->HiddenVolume -= order_ptr->HiddenQuantity();
level_ptr->VisibleVolume -= order_ptr->VisibleQuantity();
// Unlink the empty order from the orders list of the price level
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
Level level(*level_ptr);
// Delete the empty price level
UpdateType update = UpdateType::UPDATE;
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteLevel(order_ptr);
update = UpdateType::DELETE;
}
// Price level was changed. Return top of the book modification flag.
return LevelUpdate(update, level, ((order_ptr->Level == nullptr) || (order_ptr->Level == (order_ptr->IsBuy() ? _best_bid : _best_ask))));
}
LevelNode* OrderBook::AddStopLevel(OrderNode* order_ptr)
{
LevelNode* level_ptr = nullptr;
if (order_ptr->IsBuy())
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::ASK, order_ptr->StopPrice);
// Insert the price level into the buy stop orders collection
_buy_stop.insert(*level_ptr);
// Update the best buy stop order price level
if ((_best_buy_stop == nullptr) || (level_ptr->Price < _best_buy_stop->Price))
_best_buy_stop = level_ptr;
}
else
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::BID, order_ptr->StopPrice);
// Insert the price level into the sell stop orders collection
_sell_stop.insert(*level_ptr);
// Update the best sell stop order price level
if ((_best_sell_stop == nullptr) || (level_ptr->Price > _best_sell_stop->Price))
_best_sell_stop = level_ptr;
}
return level_ptr;
}
LevelNode* OrderBook::DeleteStopLevel(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
if (order_ptr->IsBuy())
{
// Update the best buy stop order price level
if (level_ptr == _best_buy_stop)
_best_buy_stop = (_best_buy_stop->right != nullptr) ? _best_buy_stop->right : _best_buy_stop->parent;
// Erase the price level from the buy stop orders collection
_buy_stop.erase(Levels::iterator(&_buy_stop, level_ptr));
}
else
{
// Update the best sell stop order price level
if (level_ptr == _best_sell_stop)
_best_sell_stop = (_best_sell_stop->left != nullptr) ? _best_sell_stop->left : _best_sell_stop->parent;
// Erase the price level from the sell stop orders collection
_sell_stop.erase(Levels::iterator(&_sell_stop, level_ptr));
}
// Release the price level
_manager._level_pool.Release(level_ptr);
return nullptr;
}
void OrderBook::AddStopOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->IsBuy() ? (LevelNode*)GetBuyStopLevel(order_ptr->StopPrice) : (LevelNode*)GetSellStopLevel(order_ptr->StopPrice);
// Create a new price level if no one found
if (level_ptr == nullptr)
level_ptr = AddStopLevel(order_ptr);
// Update the price level volume
level_ptr->TotalVolume += order_ptr->LeavesQuantity;
level_ptr->HiddenVolume += order_ptr->HiddenQuantity();
level_ptr->VisibleVolume += order_ptr->VisibleQuantity();
// Link the new order to the orders list of the price level
level_ptr->OrderList.push_back(*order_ptr);
++level_ptr->Orders;
// Cache the price level in the given order
order_ptr->Level = level_ptr;
}
void OrderBook::ReduceStopOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= quantity;
level_ptr->HiddenVolume -= hidden;
level_ptr->VisibleVolume -= visible;
// Unlink the empty order from the orders list of the price level
if (order_ptr->LeavesQuantity == 0)
{
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
}
// Delete the empty price level
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteStopLevel(order_ptr);
}
}
void OrderBook::DeleteStopOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= order_ptr->LeavesQuantity;
level_ptr->HiddenVolume -= order_ptr->HiddenQuantity();
level_ptr->VisibleVolume -= order_ptr->VisibleQuantity();
// Unlink the empty order from the orders list of the price level
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
// Delete the empty price level
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteStopLevel(order_ptr);
}
}
LevelNode* OrderBook::AddTrailingStopLevel(OrderNode* order_ptr)
{
LevelNode* level_ptr = nullptr;
if (order_ptr->IsBuy())
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::ASK, order_ptr->StopPrice);
// Insert the price level into the trailing buy stop orders collection
_trailing_buy_stop.insert(*level_ptr);
// Update the best trailing buy stop order price level
if ((_best_trailing_buy_stop == nullptr) || (level_ptr->Price < _best_trailing_buy_stop->Price))
_best_trailing_buy_stop = level_ptr;
}
else
{
// Create a new price level
level_ptr = _manager._level_pool.Create(LevelType::BID, order_ptr->StopPrice);
// Insert the price level into the trailing sell stop orders collection
_trailing_sell_stop.insert(*level_ptr);
// Update the best trailing sell stop order price level
if ((_best_trailing_sell_stop == nullptr) || (level_ptr->Price > _best_trailing_sell_stop->Price))
_best_trailing_sell_stop = level_ptr;
}
return level_ptr;
}
LevelNode* OrderBook::DeleteTrailingStopLevel(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
if (order_ptr->IsBuy())
{
// Update the best trailing buy stop order price level
if (level_ptr == _best_trailing_buy_stop)
_best_trailing_buy_stop = (_best_trailing_buy_stop->right != nullptr) ? _best_trailing_buy_stop->right : _best_trailing_buy_stop->parent;
// Erase the price level from the trailing buy stop orders collection
_trailing_buy_stop.erase(Levels::iterator(&_trailing_buy_stop, level_ptr));
}
else
{
// Update the best trailing sell stop order price level
if (level_ptr == _best_trailing_sell_stop)
_best_trailing_sell_stop = (_best_trailing_sell_stop->left != nullptr) ? _best_trailing_sell_stop->left : _best_trailing_sell_stop->parent;
// Erase the price level from the trailing sell stop orders collection
_trailing_sell_stop.erase(Levels::iterator(&_trailing_sell_stop, level_ptr));
}
// Release the price level
_manager._level_pool.Release(level_ptr);
return nullptr;
}
void OrderBook::AddTrailingStopOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->IsBuy() ? (LevelNode*)GetTrailingBuyStopLevel(order_ptr->StopPrice) : (LevelNode*)GetTrailingSellStopLevel(order_ptr->StopPrice);
// Create a new price level if no one found
if (level_ptr == nullptr)
level_ptr = AddTrailingStopLevel(order_ptr);
// Update the price level volume
level_ptr->TotalVolume += order_ptr->LeavesQuantity;
level_ptr->HiddenVolume += order_ptr->HiddenQuantity();
level_ptr->VisibleVolume += order_ptr->VisibleQuantity();
// Link the new order to the orders list of the price level
level_ptr->OrderList.push_back(*order_ptr);
++level_ptr->Orders;
// Cache the price level in the given order
order_ptr->Level = level_ptr;
}
void OrderBook::ReduceTrailingStopOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= quantity;
level_ptr->HiddenVolume -= hidden;
level_ptr->VisibleVolume -= visible;
// Unlink the empty order from the orders list of the price level
if (order_ptr->LeavesQuantity == 0)
{
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
}
// Delete the empty price level
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteTrailingStopLevel(order_ptr);
}
}
void OrderBook::DeleteTrailingStopOrder(OrderNode* order_ptr)
{
// Find the price level for the order
LevelNode* level_ptr = order_ptr->Level;
// Update the price level volume
level_ptr->TotalVolume -= order_ptr->LeavesQuantity;
level_ptr->HiddenVolume -= order_ptr->HiddenQuantity();
level_ptr->VisibleVolume -= order_ptr->VisibleQuantity();
// Unlink the empty order from the orders list of the price level
level_ptr->OrderList.pop_current(*order_ptr);
--level_ptr->Orders;
// Delete the empty price level
if (level_ptr->TotalVolume == 0)
{
// Clear the price level cache in the given order
order_ptr->Level = DeleteTrailingStopLevel(order_ptr);
}
}
uint64_t OrderBook::CalculateTrailingStopPrice(const Order& order) const noexcept
{
// Get the current market price
uint64_t market_price = order.IsBuy() ? GetMarketTrailingStopPriceAsk() : GetMarketTrailingStopPriceBid();
int64_t trailing_distance = order.TrailingDistance;
int64_t trailing_step = order.TrailingStep;
// Convert percentage trailing values into absolute ones
if (trailing_distance < 0)
{
trailing_distance = (int64_t)((-trailing_distance * market_price) / 10000);
trailing_step = (int64_t)((-trailing_step * market_price) / 10000);
}
uint64_t old_price = order.StopPrice;
if (order.IsBuy())
{
// Calculate a new stop price
uint64_t new_price = (market_price < (std::numeric_limits<uint64_t>::max() - trailing_distance)) ? (market_price + trailing_distance) : std::numeric_limits<uint64_t>::max();
// If the new price is better and we get through the trailing step
if (new_price < old_price)
if ((old_price - new_price) >= (uint64_t)trailing_step)
return new_price;
}
else
{
// Calculate a new stop price
uint64_t new_price = (market_price > (uint64_t)trailing_distance) ? (market_price - trailing_distance) : 0;
// If the new price is better and we get through the trailing step
if (new_price > old_price)
if ((new_price - old_price) >= (uint64_t)trailing_step)
return new_price;
}
return old_price;
}
} // namespace Matching
} // namespace CppTrader