-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.h
321 lines (282 loc) · 11.1 KB
/
broker.h
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
#pragma once
#include <cstdint>
#include <cstring>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <utility>
#include "floatOp.h"
#include "message.h"
#include "tscClock.h"
#include "zAllocator.h"
// not thread safe
struct Broker {
// todo: instead of std::map with absl::btree_map
using BidsT = std::map<Price, Qty, std::greater<Price>, zAllocator<std::pair<const Price, Qty>>>;
using AsksT = std::map<Price, Qty, std::less<Price>, zAllocator<std::pair<const Price, Qty>>>;
Broker() = default;
Broker(Broker &&) = delete;
Broker(const Broker &) = delete;
Broker &operator=(Broker &&) = delete;
Broker &operator=(const Broker &) = delete;
HintHot void insertOrder(const Order &order) {
/* lookup table avoid switch case, for performance but useless for readability
and actually it's invalid for performance improvement, need to verify again
using MemFuncT = void (Broker::*)(const Order &);
static constexpr int32_t kFunNum = 4;
static constexpr MemFuncT FuncTab[kFunNum] = {&Broker::onLimitBuyOrder, &Broker::onLimitSellOrder,
&Broker::onMarketBuyOrder, &Broker::onMarketSellOrder};
const int32_t funcIndex = ((order.type_ > OrderType::Limit) << 1) | (order.side_ > QuoteType::Buy);
MemFuncT func = FuncTab[funcIndex];
return (this->*func)(order);
*/
switch (order.type_) {
case OrderType::Limit: {
switch (order.side_) {
case QuoteType::Buy:
return onLimitBuyOrder(order);
case QuoteType::Sell:
return onLimitSellOrder(order);
default:
break;
}
case OrderType::Market: {
switch (order.side_) {
case QuoteType::Buy:
return onMarketBuyOrder(order);
case QuoteType::Sell:
return onMarketSellOrder(order);
default:
break;
}
}
default:
break;
}
}
}
void cancelOrder(const Order &order) {
if (order.orderStatus_ != OrderStatus::Canceled) {
return;
}
switch (order.type_) {
case OrderType::Limit: {
switch (order.side_) {
case QuoteType::Buy:
return onCancelLimitBuyOrder(order);
case QuoteType::Sell:
return onCancelLimitSellOrder(order);
default:
break;
}
// ignore market order
}
default:
break;
}
}
template <size_t DEPTH>
void getOrderBook(Orderbook<DEPTH> &obRef, size_t depth = DEPTH) const {
size_t i = 0;
const size_t constMaxDepth = (depth > DEPTH) ? DEPTH : depth;
for (auto it = bids_.begin(); it != bids_.end() && i < constMaxDepth; it++) {
PriceLevel &priceLevelRef = obRef.bid(i++);
priceLevelRef.price_ = it->first;
priceLevelRef.qty_ = it->second;
}
obRef.bidSize_ = i;
i = 0;
for (auto it = asks_.begin(); it != asks_.end() && i < constMaxDepth; it++) {
PriceLevel &priceLevelRef = obRef.ask(i++);
priceLevelRef.price_ = it->first;
priceLevelRef.qty_ = it->second;
}
obRef.askSize_ = i;
}
private:
HintHot void onLimitBuyOrder(const Order &buyOrder) {
Qty remainQty = buyOrder.remainQty_;
const bool shouldBeMatch = !lessThan(buyOrder.price_, bestAskPrice_);
if (shouldBeMatch) [[likely]] {
for (auto it = asks_.begin(); it != asks_.upper_bound(buyOrder.price_);) {
if (it->second > remainQty) [[likely]] {
bestAskPrice_ = it->first;
it->second -= remainQty;
remainQty = 0;
break;
} else {
remainQty -= it->second;
it = asks_.erase(it);
}
}
if (asks_.empty()) [[unlikely]] {
bestAskPrice_ = std::numeric_limits<Price>::max();
}
}
// consider that time priority including GTC/FOK/IOC,
// only GTC&FAK order be handled here
if (remainQty) {
updateBids(buyOrder, remainQty);
}
}
void onCancelLimitBuyOrder(const Order &buyOrder) {
// at first, should determine whether the entry exist in order book
// order should be stored in hashmap
Qty remainQty = buyOrder.remainQty_;
auto it = bids_.find(buyOrder.price_);
if (it != bids_.end()) {
if (it->second > remainQty) [[likely]] {
it->second -= remainQty;
} else {
it = bids_.erase(it);
updateBestBidPrice(it, buyOrder.price_);
}
}
}
HintHot void onLimitSellOrder(const Order &sellOrder) {
Qty remainQty = sellOrder.remainQty_;
const bool shouldBeMatch = !greator(sellOrder.price_, bestBidPrice_);
if (shouldBeMatch) [[likely]] {
for (auto it = bids_.begin(); it != bids_.upper_bound(sellOrder.price_);) {
if (it->second > remainQty) [[likely]] {
bestBidPrice_ = it->first;
it->second -= remainQty;
remainQty = 0;
break;
} else {
remainQty -= it->second;
it = bids_.erase(it);
}
}
if (bids_.empty()) [[unlikely]] {
bestBidPrice_ = std::numeric_limits<Price>::min();
}
}
// consider that time priority including GTC/FOK/IOC,
// only GTC&FAK order be handled here
if (remainQty) {
updateAsks(sellOrder, remainQty);
}
}
void onCancelLimitSellOrder(const Order &sellOrder) {
// at first, should determine whether the entry exist in order book
Qty remainQty = sellOrder.remainQty_;
auto it = asks_.find(sellOrder.price_);
if (it != asks_.end()) {
if (it->second > remainQty) [[likely]] {
it->second -= remainQty;
} else {
it = asks_.erase(it);
updateBestAskPrice(it, sellOrder.price_);
}
}
}
// Futures contracts for market orders to be limited to 1% worse than the best bid or ask
// protect traders from things like slippage and “fat finger trade” (trader mistakes).
void onMarketBuyOrder(const Order &buyOrder) {
Qty remainQty = buyOrder.remainQty_;
const bool shouldBeMatch = lessThan(buyOrder.price_, bestAskPrice_);
if (shouldBeMatch) [[likely]] {
for (auto it = asks_.begin(); it != asks_.end();) {
if (it->second > remainQty) [[likely]] {
bestAskPrice_ = it->first;
it->second -= remainQty;
remainQty = 0;
break;
} else {
// when filled qty hit 1% of total limit order qty should give up fill
remainQty -= it->second;
it = asks_.erase(it);
}
}
if (asks_.empty()) [[unlikely]] {
bestAskPrice_ = std::numeric_limits<Price>::max();
}
}
// transfer market order into limit order
// when filled qty hit 1% of total limit order qty
// time priority of market order should be IOC
// so remainQty must be cancelled or
// transfer market order into limit order
// do not send orderRsp and trade to trader
// remainQty of marketOrder great than 0 is impossible
// risk control should handle this
}
// Futures contracts for market orders to be limited
// to 1% worse than the best bid or ask
// protect traders from things like slippage
// and “fat finger trade” (trader mistakes).
void onMarketSellOrder(const Order &sellOrder) {
Qty remainQty = sellOrder.remainQty_;
const bool shouldBeMatch = !greator(sellOrder.price_, bestBidPrice_);
if (shouldBeMatch) [[likely]] {
for (auto it = bids_.begin(); it != bids_.end();) {
if (it->second > remainQty) [[likely]] {
bestBidPrice_ = it->first;
it->second -= remainQty;
remainQty = 0;
break;
} else {
// when filled qty hit 1% of total limit order qty should give up fill
remainQty -= it->second;
it = bids_.erase(it);
}
}
if (bids_.empty()) [[unlikely]] {
bestBidPrice_ = std::numeric_limits<Price>::min();
}
}
// transfer market order into limit order
// when filled qty hit 1% of total limit order qty
// time priority of market order should be IOC
// so remainQty must be cancelled
// or transfer market order into limit order
// do not send orderRsp and trade to trader
// remainQty of marketOrder great than 0 is impossible
// risk control should handle this
}
inline void updateBestBidPrice(BidsT::iterator &it, Price price) {
if (!bids_.empty()) [[likely]] {
if (equal(price, bestBidPrice_)) [[unlikely]] {
bestBidPrice_ = it->first;
}
} else {
bestBidPrice_ = std::numeric_limits<Price>::min();
}
}
inline void updateBestAskPrice(AsksT::iterator &it, Price price) {
if (!asks_.empty()) [[likely]] {
if (equal(price, bestAskPrice_)) [[unlikely]] {
bestAskPrice_ = it->first;
}
} else {
bestAskPrice_ = std::numeric_limits<Price>::max();
}
}
void updateAsks(const Order &orderRef, Qty remainQty) {
auto result = asks_.emplace(orderRef.price_, remainQty);
if (!result.second) {
result.first->second += remainQty;
} else {
if (lessThan(orderRef.price_, bestAskPrice_)) {
bestAskPrice_ = orderRef.price_;
}
}
}
void updateBids(const Order &orderRef, Qty remainQty) {
auto result = bids_.emplace(orderRef.price_, remainQty);
if (!result.second) {
result.first->second += remainQty;
} else {
if (greator(orderRef.price_, bestBidPrice_)) {
bestBidPrice_ = orderRef.price_;
}
}
}
private:
Price bestBidPrice_ = std::numeric_limits<Price>::min();
BidsT bids_;
Price bestAskPrice_ = std::numeric_limits<Price>::max();
AsksT asks_;
};