-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathMarket.cpp
More file actions
559 lines (419 loc) · 19.3 KB
/
Copy pathMarket.cpp
File metadata and controls
559 lines (419 loc) · 19.3 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
Copyright (C) 2021 Mike Kipnis
This file is part of DistributedATS, a free-software/open-source project
that integrates QuickFIX and LiquiBook over DDS. This project simplifies
the process of having multiple FIX gateways communicating with multiple
matching engines in realtime.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <boost/lockfree/spsc_queue.hpp>
#include "Market.h"
#include "OrderException.h"
#include <cctype>
#include <functional>
#include <locale>
#include <ExecutionReportLogger.hpp>
#include <LoggerHelper.h>
#include <MarketDataRequestLogger.hpp>
#include <OrderCancelRejectLogger.hpp>
#include <OrderMassCancelReportLogger.hpp>
#include <SecurityListRequestLogger.hpp>
#include <MarketDataIncrementalRefreshLogger.hpp>
namespace DistributedATS {
uint32_t Market::orderIdSeed_ = 0;
uint32_t Market::executionIdSeed_ = 1000000;
Market::Market(DataWriterContainerPtr dataWriterContainerPtr,
std::string marketName, std::string dataServiceName,
PriceDepthPublisherQueuePtr&
price_depth_publisher_queue_ptr)
: dataWriterContainerPtr_(dataWriterContainerPtr), _marketName(marketName), _dataServiceName(dataServiceName),
_price_depth_publisher_queue_ptr( price_depth_publisher_queue_ptr ), _ready_to_trade(false)
{
stats_ptr_ = std::make_shared<OrderBookStatsMap>();
}
Market::~Market() {}
void Market::populatePriceLevelDepth(
const std::string &symbol, DistributedATS::Market *market,
const DistributedATS::BookDepth &depth,
DistributedATS_MarketDataIncrementalRefresh::MarketDataIncrementalRefresh
*marketDataRefresh) {
DistributedATS_MarketDataIncrementalRefresh::NoMDEntries mdEntry;
mdEntry.Symbol(symbol);
mdEntry.SecurityExchange(market->getMarketName());
mdEntry.MDUpdateAction('0');
uint16_t md_index = 0;
auto pos = depth.bids();
while (pos != depth.end()) {
mdEntry.MDEntrySize(pos->aggregate_qty());
if (mdEntry.MDEntrySize() == 0)
mdEntry.MDEntryPx(0);
else
mdEntry.MDEntryPx(pos->price());
mdEntry.MDEntrySize(pos->aggregate_qty());
mdEntry.MDEntryType(md_index < MARKET_DATA_PRICE_DEPTH ? FIX::MDEntryType_BID
: FIX::MDEntryType_OFFER);
marketDataRefresh->c_NoMDEntries()[md_index++] = mdEntry;
pos++;
};
}
bool Market::submit(const OrderBookPtr &book, const OrderPtr &order) {
auto contra_party_orders = orders_.find(order->contra_party());
OrderMapPtr customerOrders;
if (contra_party_orders == orders_.end()) {
customerOrders.reset(new OrderMap());
orders_.emplace(order->contra_party(), customerOrders);
} else {
customerOrders = contra_party_orders->second;
}
auto existing_order = customerOrders->find(order->order_id());
if (existing_order != customerOrders->end()) {
throw DistributedATS::OrderException(order->contra_party(),
order->order_id(),
FIX::OrdRejReason_DUPLICATE_ORDER);
}
customerOrders->emplace(order->order_id(), order);
//const liquibook::book::OrderConditions NOC(liquibook::book::oc_no_conditions);
book->add(order, order->conditions());
return true;
}
void Market::publishSecurityListRequest( eprosima::fastdds::dds::DataWriter* dwr )
{
DistributedATS_SecurityListRequest::SecurityListRequest securityListRequest;
securityListRequest.DATS_Destination("DATA_SERVICE");
securityListRequest.DATS_DestinationUser(_dataServiceName);
securityListRequest.DATS_Source("MATCHINE_ENGINE");
securityListRequest.DATS_SourceUser(getMarketName());
LoggerHelper::log_info<std::stringstream, SecurityListRequestLogger, DistributedATS_SecurityListRequest::SecurityListRequest>(logger,securityListRequest, "SecurityListRequest");
auto ret = dwr->write(&securityListRequest);
if ( ret != eprosima::fastdds::dds::RETCODE_OK ) {
LOG4CXX_ERROR(logger, "SecurityListRequest write returned : " << ret);
}
}
void Market::publishMarketDataRequest()
{
DistributedATS_MarketDataRequest::MarketDataRequest marketDataRequest;
marketDataRequest.DATS_Destination("DATA_SERVICE");
marketDataRequest.DATS_DestinationUser(_dataServiceName);
marketDataRequest.DATS_Source("MATCHING_ENGINE");
marketDataRequest.DATS_SourceUser(getMarketName());
marketDataRequest.c_NoMDEntryTypes().resize(1);
marketDataRequest.c_NoMDEntryTypes()[0].MDEntryType(FIX::MDEntryType_TRADE); // Last trade price
marketDataRequest.c_NoRelatedSym().resize(books_.size());
int index = 0;
for (auto &book : books_)
{
marketDataRequest.c_NoRelatedSym()[index].Symbol(book.first);
marketDataRequest.c_NoRelatedSym()[index].SecurityExchange(getMarketName());
index++;
}
LoggerHelper::log_info<std::stringstream, MarketDataRequestLogger,DistributedATS_MarketDataRequest::MarketDataRequest>(logger,marketDataRequest, "MarketDataRequest");
auto ret = dataWriterContainerPtr_->market_data_request_dw->write(&marketDataRequest);
if ( ret != eprosima::fastdds::dds::RETCODE_OK )
{
LOG4CXX_ERROR(logger, "MarketDataRequest write returned : " << ret);
}
}
bool Market::cancel_order(const OrderBookPtr &book,
const std::string &counter_party,
const std::string &client_order_id)
{
auto contra_party_orders = orders_.find(counter_party);
if (contra_party_orders == orders_.end())
{
std::cerr << "Order for given counter party not found : " << counter_party
<< std::endl;
return false;
};
auto orderIter = contra_party_orders->second->find(client_order_id);
if (orderIter == contra_party_orders->second->end()) {
throw DistributedATS::OrderException(counter_party, client_order_id,
FIX::OrdRejReason_UNKNOWN_ORDER);
}
book->cancel(orderIter->second);
return true;
}
/// @brief attempts to replace existing order
bool Market::replace_order(const OrderBookPtr &book, const std::string &counter_party,
const std::string &orig_client_order_id, const std::string& client_order_id,
int32_t size_delta, liquibook::book::Price new_price)
{
auto contra_party_orders = orders_.find(counter_party);
if (contra_party_orders == orders_.end()) {
std::cerr << "Order for given counter party not found : " << counter_party
<< std::endl;
return false;
};
auto orderIter = contra_party_orders->second->find(orig_client_order_id);
if (orderIter == contra_party_orders->second->end()) {
throw DistributedATS::OrderException(counter_party, client_order_id,
FIX::OrdRejReason_UNKNOWN_ORDER);
}
orderIter->second->onReplaceRequested(client_order_id, size_delta, new_price);
book->replace(orderIter->second, size_delta, new_price);
contra_party_orders->second->emplace(client_order_id, orderIter->second );
contra_party_orders->second->erase(orig_client_order_id);
return true;
};
bool Market::mass_cancel(const std::string &counter_party) {
auto contra_party_orders = orders_.find(counter_party);
if (contra_party_orders == orders_.end()) {
std::cerr << "Orders for given counter party not found : " << counter_party
<< std::endl;
return false;
};
size_t order_to_be_cancelled = contra_party_orders->second->size();
for (auto orderIter : *contra_party_orders->second) {
OrderPtr order = orderIter.second;
auto book = findBook(order->symbol());
if ( order->quantityOnMarket() > 0 )
{
book->cancel(order);
}
}
orders_.erase(contra_party_orders);
return true;
}
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;
}
void Market::publishExecutionReport(DistributedATS_ExecutionReport::ExecutionReport &executionReport)
{
executionReport.ExecID(getNextExecutionReportID());
LoggerHelper::log_debug<std::stringstream, ExecutionReportLogger, DistributedATS_ExecutionReport::ExecutionReport>(logger,
executionReport, "ExecutionReport");
auto ret = dataWriterContainerPtr_->execution_report_dw->write(&executionReport);
if ( ret != eprosima::fastdds::dds::RETCODE_OK )
{
LOG4CXX_ERROR(logger, "Execution Report write returned : " << ret);
}
}
void Market::publishOrderMassCancelReport(DistributedATS_OrderMassCancelReport::OrderMassCancelReport &orderMassCancelReport)
{
LoggerHelper::log_debug<std::stringstream, OrderMassCancelReportLogger, DistributedATS_OrderMassCancelReport::OrderMassCancelReport>(logger, orderMassCancelReport, "OrderMassCancelReport");
auto ret = dataWriterContainerPtr_->order_mass_cancel_report_dw->write(&orderMassCancelReport);
if ( ret != eprosima::fastdds::dds::RETCODE_OK )
{
LOG4CXX_ERROR(logger, "Order Mass Cancel Report write returned: " << ret);
}
}
void Market::publishOrderCancelReject(
DistributedATS_OrderCancelReject::OrderCancelReject &orderCancelReject) {
LoggerHelper::log_debug<std::stringstream, OrderCancelRejectLogger,
DistributedATS_OrderCancelReject::OrderCancelReject>(logger, orderCancelReject, "OrderCancelReject");
auto ret = dataWriterContainerPtr_->order_cancel_reject_dw->write(&orderCancelReject);
if ( ret != eprosima::fastdds::dds::RETCODE_OK )
{
LOG4CXX_ERROR(logger, "Order Cancel Reject write returned: " << ret);
}
}
/////////////////////////////////////
// Implement OrderListener interface
void Market::on_accept(const OrderPtr &order)
{
order->onAccepted();
DistributedATS_ExecutionReport::ExecutionReport executionReport;
order->populateExecutionReport(executionReport, FIX::ExecType_NEW);
publishExecutionReport(executionReport);
}
void Market::on_reject(const OrderPtr &order, const char *reason) {
order->onRejected(reason);
DistributedATS_ExecutionReport::ExecutionReport executionReport;
order->populateExecutionReport(executionReport, FIX::ExecType_REJECTED);
publishExecutionReport(executionReport);
}
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);
DistributedATS_ExecutionReport::ExecutionReport executionReport;
DistributedATS_ExecutionReport::ExecutionReport executionReportMatched;
if (order->quantityOnMarket() == 0)
order->populateExecutionReport(executionReport, FIX::ExecType_FILL);
else
order->populateExecutionReport(executionReport, FIX::ExecType_PARTIAL_FILL);
if (matched_order->quantityOnMarket() == 0)
matched_order->populateExecutionReport(executionReportMatched,
FIX::ExecType_FILL);
else
matched_order->populateExecutionReport(executionReportMatched,
FIX::ExecType_PARTIAL_FILL);
auto last_px = fill_cost / fill_qty;
executionReport.LastPx(last_px);
executionReportMatched.LastPx(last_px);
executionReport.LastQty(fill_qty);
executionReportMatched.LastQty(fill_qty);
publishExecutionReport(executionReport);
publishExecutionReport(executionReportMatched);
}
void Market::on_cancel(const OrderPtr &order) {
order->onCancelled();
DistributedATS_ExecutionReport::ExecutionReport executionReport;
order->populateExecutionReport(executionReport, FIX::ExecType_CANCELED);
publishExecutionReport(executionReport);
// out() << "\tCanceled: " << *order<< std::endl;
}
void Market::on_cancel_reject(const OrderPtr &order, const char *reason) {
order->onCancelRejected(reason);
// out() << "\tCancel Reject: " <<*order<< " : " << reason << std::endl;
}
void Market::on_replace(const OrderPtr &order, const int64_t &size_delta,
liquibook::book::Price new_price) {
// out() << "\tCancel Reject: " <<*order<< " : " << size_delta << " : " <<
// new_price << std::endl;
order->onReplaced(size_delta, new_price);
DistributedATS_ExecutionReport::ExecutionReport executionReport;
order->populateExecutionReport(executionReport, FIX::ExecType_REPLACED);
publishExecutionReport(executionReport);
}
void Market::on_replace_reject(const OrderPtr &order, const char *reason) {
order->onReplaceRejected(reason);
// out() << "\tCancel Reject: " <<*order<< " : " << reason << std::endl;
}
void Market::on_trade(const OrderBook *book, liquibook::book::Quantity qty,
liquibook::book::Cost cost) {
auto price = book->market_price();
update_symbol_stats(book, price, qty);
}
void Market::on_order_book_change(const OrderBook *book) {}
void Market::on_bbo_change(const DepthOrderBook *book, const BookDepth *depth) {
}
void Market::on_depth_change(const DepthOrderBook *book,
const BookDepth *depth) {
auto md_update = std::make_shared<DistributedATS::MarketDataUpdate>();
md_update->symbol = book->symbol();
md_update->priceDepth.DATS_Source("MATCHING_ENGINE");
md_update->priceDepth.fix_header().MsgType("X");
md_update->priceDepth.c_NoMDEntries().resize(15);
populatePriceLevelDepth(book->symbol(), this, *depth, &md_update->priceDepth);
std::stringstream ss;
MarketDataIncrementalRefreshLogger::log(ss, md_update->priceDepth);
LOG4CXX_INFO(logger, "MarketDataIncrementalRefresh : [" << ss.str() << "]");
int market_data_index = MARKET_DATA_PRICE_DEPTH * 2; // bids and asks
auto current_stats = stats_ptr_->find(book->symbol());
// populating stats
if (current_stats != stats_ptr_->end()) {
if (current_stats->second->volume > 0) {
// Last traded price and traded volume
DistributedATS_MarketDataIncrementalRefresh::NoMDEntries mdEntry;
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_TRADE, book->market_price(), 0);
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_TRADE_VOLUME, 0, current_stats->second->volume);
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_OPENING_PRICE, current_stats->second->open, 0);
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_TRADING_SESSION_LOW_PRICE,
current_stats->second->low, 0);
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_TRADING_SESSION_HIGH_PRICE,
current_stats->second->high, 0);
} else {
set_market_data_stats_entry(
md_update->priceDepth.c_NoMDEntries()[market_data_index++],
getMarketName(), book->symbol(), FIX::MDUpdateAction_NEW,
FIX::MDEntryType_OPENING_PRICE, current_stats->second->open, 0);
}
}
_price_depth_publisher_queue_ptr->push(md_update);
}
void Market::cancel_all_orders() {
for (auto counter_party_iter : orders_) {
for (auto orderIter : *counter_party_iter.second) {
OrderPtr order = orderIter.second;
auto book = findBook(order->symbol());
book->cancel(order);
}
}
};
void Market::set_market_price(const std::string &symbol, liquibook::book::Price price)
{
auto book = findBook(symbol);
if (book != NULL)
{
std::stringstream ss;
LOG4CXX_INFO(logger, "Setting market price :" << symbol << price);
update_symbol_stats(book.get(), price, 0);
book->set_market_price(price);
} else {
LOG4CXX_ERROR(logger, "Book not found :" << symbol << price);
}
};
void Market::update_symbol_stats(const OrderBook *book,
liquibook::book::Price price,
liquibook::book::Quantity qty) {
auto symbol_stats = stats_ptr_->find(book->symbol());
if (symbol_stats == stats_ptr_->end()) {
auto stats = std::make_shared<DistributedATS::OrderBookStats>();
stats->volume = qty;
stats->open = stats->low = stats->high = price;
stats_ptr_->emplace(book->symbol(), stats);
} else {
symbol_stats->second->volume += qty;
if (symbol_stats->second->low > price)
symbol_stats->second->low = price;
else if (symbol_stats->second->high < price)
symbol_stats->second->high = price;
}
}
void Market::set_market_data_stats_entry(
DistributedATS_MarketDataIncrementalRefresh::NoMDEntries &mdEntry,
const std::string &market, const std::string &symbol, const char md_action,
const char md_entry_type, const float MDEntryPx, const float MDEntrySize) {
mdEntry.SecurityExchange (getMarketName());
mdEntry.Symbol(symbol);
mdEntry.MDUpdateAction(md_action);
mdEntry.MDEntryType(md_entry_type);
if (md_entry_type == FIX::MDEntryType_TRADE_VOLUME)
mdEntry.MDEntrySize(MDEntrySize);
else
mdEntry.MDEntryPx(MDEntryPx);
}
} // namespace DistributedATS