-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMarket.cc
More file actions
344 lines (302 loc) · 8.57 KB
/
Market.cc
File metadata and controls
344 lines (302 loc) · 8.57 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
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
// Copyright (c) 2017 Object Computing, Inc.
// All rights reserved.
// See the file license.txt for licensing information.
#include "Market.h"
#include "Util.h"
#include <functional>
#include <cctype>
#include <locale>
namespace {
///////////////////////
// depth display helper
void displayDepthLevel(std::ostream & out, const liquibook::book::DepthLevel & level)
{
out << "\tPrice " << level.price();
out << " Count: " << level.order_count();
out << " Quantity: " << level.aggregate_qty();
if(level.is_excess())
{
out << " EXCESS";
}
out << " Change id#: " << level.last_change();
out << std::endl;
}
void publishDepth(std::ostream & out, const orderentry::BookDepth & depth)
{
liquibook::book::ChangeId published = depth.last_published_change();
bool needTitle = true;
// Iterate awkwardly
auto pos = depth.bids();
auto back = depth.last_bid_level();
bool more = true;
while(more)
{
if(pos->aggregate_qty() !=0 && pos->last_change() > published)
{
if(needTitle)
{
out << "\n\tBIDS:\n";
needTitle = false;
}
displayDepthLevel(out, *pos);
}
++pos;
more = pos != back;
}
needTitle = true;
pos = depth.asks();
back = depth.last_ask_level();
more = true;
while(more)
{
if(pos->aggregate_qty() !=0 && pos->last_change() > published)
{
if(needTitle)
{
out << "\n\tASKS:\n";
needTitle = false;
}
displayDepthLevel(out, *pos);
}
++pos;
more = pos != back;
}
}
}
namespace orderentry
{
Market::Market(std::ostream * out)
: logFile_(out)
{
}
Market::~Market()
{
}
void Market::orderSubmit(OrderBookPtr book, OrderPtr order,
const std::string& orderId,
liquibook::book::OrderConditions conditions)
{
order->genTimestamp();
order->onSubmitted();
out() << "ADDING order: " << *order << std::endl;
orders_[orderId] = order;
book->add(order, conditions);
}
///////////
// CANCEL
bool Market::orderCancel(const std::string & orderId)
{
OrderPtr order;
OrderBookPtr book;
if (!findExistingOrder(orderId, order, book))
{
return false;
}
out() << "Requesting Cancel: " << *order << std::endl;
book->cancel(order);
return true;
}
///////////
// MODIFY
bool Market::orderModify(const std::string & orderId,
int32_t quantityChange,
liquibook::book::Price price)
{
OrderPtr order;
OrderBookPtr book;
if(!findExistingOrder(orderId, order, book))
{
return false;
}
if (price != liquibook::book::PRICE_UNCHANGED) {
if ((price <= 0) ||
(price == INVALID_UINT32))
return false;
}
if (quantityChange != liquibook::book::SIZE_UNCHANGED) {
if (quantityChange == INVALID_INT32)
return false;
}
book->replace(order, quantityChange, price);
out() << "Requested Modify" ;
if(quantityChange != liquibook::book::SIZE_UNCHANGED)
{
out() << " QUANTITY += " << quantityChange;
}
if(price != liquibook::book::PRICE_UNCHANGED)
{
out() << " PRICE " << price;
}
out() << std::endl;
return true;
}
///////////
// getSymbols
void Market::getSymbols(std::vector<std::string> & symbols)
{
symbols.clear();
for(auto pBook = books_.begin(); pBook != books_.end(); ++pBook)
{
symbols.push_back(pBook->first);
}
}
/////////////////////////////
// Order book interactions
bool
Market::symbolIsDefined(const std::string & symbol)
{
auto book = books_.find(symbol);
return book != books_.end();
}
OrderBookPtr
Market::addBook(const std::string & symbol, bool useDepthBook)
{
OrderBookPtr result;
if(useDepthBook)
{
out() << "Create new depth order book for " << symbol << std::endl;
DepthOrderBookPtr depthBook = std::make_shared<DepthOrderBook>(symbol);
depthBook->set_bbo_listener(this);
depthBook->set_depth_listener(this);
result = depthBook;
}
else
{
out() << "Create new order book for " << symbol << std::endl;
result = std::make_shared<OrderBook>(symbol);
}
result->set_order_listener(this);
result->set_trade_listener(this);
result->set_order_book_listener(this);
books_[symbol] = result;
return result;
}
OrderBookPtr
Market::findBook(const std::string & symbol)
{
OrderBookPtr result;
auto entry = books_.find(symbol);
if(entry != books_.end())
{
result = entry->second;
}
return result;
}
bool Market::findExistingOrder(const std::string & orderId, OrderPtr & order, OrderBookPtr & book)
{
auto orderPosition = orders_.find(orderId);
if(orderPosition == orders_.end())
{
out() << "--Can't find OrderID #" << orderId << std::endl;
return false;
}
order = orderPosition->second;
std::string symbol = order->symbol();
book = findBook(symbol);
if(!book)
{
out() << "--No order book for symbol" << symbol << std::endl;
return false;
}
return true;
}
/////////////////////////////////////
// Implement OrderListener interface
void
Market::on_accept(const OrderPtr& order)
{
order->onAccepted();
out() << "\tEvent:Accepted: " <<*order<< std::endl;
}
void
Market::on_reject(const OrderPtr& order, const char* reason)
{
order->onRejected(reason);
out() << "\tEvent:Rejected: " <<*order<< ' ' << reason << std::endl;
}
void
Market::on_fill(const OrderPtr& order,
const OrderPtr& matched_order,
liquibook::book::Quantity fill_qty,
liquibook::book::Cost fill_cost)
{
order->onFilled(fill_qty, fill_cost);
matched_order->onFilled(fill_qty, fill_cost);
out() << (order->is_buy() ? "\tEvent:Fill-Bought: " : "\tEvent:Fill-Sold: ")
<< fill_qty << " Shares for " << fill_cost << ' ' <<*order<< std::endl;
out() << (matched_order->is_buy() ? "\tBought: " : "\tSold: ")
<< fill_qty << " Shares for " << fill_cost << ' ' << *matched_order << std::endl;
}
void
Market::on_cancel(const OrderPtr& order)
{
order->onCancelled();
out() << "\tEvent:Canceled: " << *order<< std::endl;
}
void Market::on_cancel_reject(const OrderPtr& order, const char* reason)
{
order->onCancelRejected(reason);
out() << "\tEvent:Cancel Reject: " <<*order<< ' ' << reason << std::endl;
}
void Market::on_replace(const OrderPtr& order,
const int32_t& size_delta,
liquibook::book::Price new_price)
{
order->onReplaced(size_delta, new_price);
out() << "\tEvent:Modify " ;
if(size_delta != liquibook::book::SIZE_UNCHANGED)
{
out() << " QUANTITY += " << size_delta;
}
if(new_price != liquibook::book::PRICE_UNCHANGED)
{
out() << " PRICE " << new_price;
}
out() <<*order<< std::endl;
}
void
Market::on_replace_reject(const OrderPtr& order, const char* reason)
{
order->onReplaceRejected(reason);
out() << "\tEvent:Replace Reject: " <<*order<< ' ' << reason << std::endl;
}
////////////////////////////////////
// Implement TradeListener interface
void
Market::on_trade(const OrderBook* book,
liquibook::book::Quantity qty,
liquibook::book::Cost cost)
{
out() << "\tEvent:Trade: " << qty << ' ' << book->symbol() << " Cost " << cost << std::endl;
}
/////////////////////////////////////////
// Implement OrderBookListener interface
void
Market::on_order_book_change(const OrderBook* book)
{
out() << "\tEvent:Book Change: " << ' ' << book->symbol() << std::endl;
}
/////////////////////////////////////////
// Implement BboListener interface
void
Market::on_bbo_change(const DepthOrderBook * book, const BookDepth * depth)
{
out() << "\tEvent:BBO Change: " << ' ' << book->symbol()
<< (depth->changed() ? " Changed" : " Unchanged")
<< " Change Id: " << depth->last_change()
<< " Published: " << depth->last_published_change()
<< std::endl;
}
/////////////////////////////////////////
// Implement DepthListener interface
void
Market::on_depth_change(const DepthOrderBook * book, const BookDepth * depth)
{
out() << "\tEvent:Depth Change: " << ' ' << book->symbol();
out() << (depth->changed() ? " Changed" : " Unchanged")
<< " Change Id: " << depth->last_change()
<< " Published: " << depth->last_published_change();
publishDepth(out(), *depth);
out() << std::endl;
}
} // namespace orderentry