forked from ajtulloch/quantcup-orderbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_book.cpp
150 lines (127 loc) · 4.03 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
#include <string.h>
#include "engine.h"
#include "limits.h"
#include "order_book.h"
#include "constants.h"
namespace OB {
namespace {
/* Report trade execution */
void executeTrade(const Field& symbol, const Field& buyTrader,
const Field& sellTrader, t_price tradePrice,
t_size tradeSize) {
t_execution exec;
if (tradeSize == 0) /* Skip orders that have been cancelled */
return;
exec.symbol = symbol;
exec.price = tradePrice;
exec.size = tradeSize;
exec.side = 0;
exec.trader = buyTrader;
execution(exec); /* Report the buy-side trade */
exec.side = 1;
exec.trader = sellTrader;
execution(exec); /* Report the sell-side trade */
}
}
OrderBook& OrderBook::get() {
static OrderBook ob;
return ob;
}
void OrderBook::initialize() {
/* Initialize the price point array */
pricePoints.resize(kMaxPrice);
for (auto& el : pricePoints) {
el.clear();
}
arenaBookEntries.resize(kMaxNumOrders);
curOrderID = 0;
askMin = kMaxPrice;
bidMax = kMinPrice;
}
void OrderBook::shutdown() {}
t_orderid OrderBook::limit(t_order& order) {
t_price price = order.price;
t_size orderSize = order.size;
if (order.side == 0) {/* Buy order */
/* Look for outstanding sell orders that cross with the incoming order */
if (price >= askMin) {
auto ppEntry = pricePoints.begin() + askMin;
do {
auto bookEntry = ppEntry->begin();
while (bookEntry != ppEntry->end()) {
if (bookEntry->size < orderSize) {
executeTrade(order.symbol, order.trader, bookEntry->trader, price,
bookEntry->size);
orderSize -= bookEntry->size;
++bookEntry;
} else {
executeTrade(order.symbol, order.trader, bookEntry->trader, price,
orderSize);
if (bookEntry->size > orderSize)
bookEntry->size -= orderSize;
else
++bookEntry;
ppEntry->erase(ppEntry->begin(), bookEntry);
while (ppEntry->begin() != bookEntry) {
ppEntry->pop_front();
}
return ++curOrderID;
}
}
/* We have exhausted all orders at the askMin price point. Move on to
the next price level. */
ppEntry->clear();
ppEntry++;
askMin++;
} while (price >= askMin);
}
auto entry = arenaBookEntries.begin() + (++curOrderID);
entry->size = orderSize;
entry->trader = order.trader;
pricePoints[price].push_back(*entry);
if (bidMax < price) bidMax = price;
return curOrderID;
} else {/* Sell order */
/* Look for outstanding Buy orders that cross with the incoming order */
if (price <= bidMax) {
auto ppEntry = pricePoints.begin() + bidMax;
do {
auto bookEntry = ppEntry->begin();
while (bookEntry != ppEntry->end()) {
if (bookEntry->size < orderSize) {
executeTrade(order.symbol, bookEntry->trader, order.trader, price,
bookEntry->size);
orderSize -= bookEntry->size;
++bookEntry;
} else {
executeTrade(order.symbol, bookEntry->trader, order.trader, price,
orderSize);
if (bookEntry->size > orderSize)
bookEntry->size -= orderSize;
else
++bookEntry;
while (ppEntry->begin() != bookEntry) {
ppEntry->pop_front();
}
return ++curOrderID;
}
}
/* We have exhausted all orders at the bidMax price point. Move on to
the next price level. */
ppEntry->clear();
ppEntry--;
bidMax--;
} while (price <= bidMax);
}
auto entry = arenaBookEntries.begin() + (++curOrderID);
entry->size = orderSize;
entry->trader = order.trader;
pricePoints[price].push_back(*entry);
if (askMin > price) askMin = price;
return curOrderID;
}
}
void OrderBook::cancel(t_orderid orderid) {
arenaBookEntries[orderid].size = 0;
}
}