forked from chronoxor/CppTrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
market_manager.cpp
197 lines (168 loc) · 9.63 KB
/
market_manager.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
//
// Created by Ivan Shynkarenka on 05.08.2017
//
#include "trader/matching/market_manager.h"
#include "trader/providers/nasdaq/itch_handler.h"
#include "benchmark/reporter_console.h"
#include "filesystem/file.h"
#include "system/stream.h"
#include "time/timestamp.h"
#include <OptionParser.h>
using namespace CppCommon;
using namespace CppTrader::ITCH;
using namespace CppTrader::Matching;
class MyMarketHandler : public MarketHandler
{
public:
MyMarketHandler()
: _updates(0),
_symbols(0),
_max_symbols(0),
_order_books(0),
_max_order_books(0),
_max_order_book_levels(0),
_max_order_book_orders(0),
_orders(0),
_max_orders(0),
_add_orders(0),
_update_orders(0),
_delete_orders(0),
_execute_orders(0)
{}
size_t updates() const { return _updates; }
size_t max_symbols() const { return _max_symbols; }
size_t max_order_books() const { return _max_order_books; }
size_t max_order_book_levels() const { return _max_order_book_levels; }
size_t max_order_book_orders() const { return _max_order_book_orders; }
size_t max_orders() const { return _max_orders; }
size_t add_orders() const { return _add_orders; }
size_t update_orders() const { return _update_orders; }
size_t delete_orders() const { return _delete_orders; }
size_t execute_orders() const { return _execute_orders; }
protected:
void onAddSymbol(const Symbol& symbol) override { ++_updates; ++_symbols; _max_symbols = std::max(_symbols, _max_symbols); }
void onDeleteSymbol(const Symbol& symbol) override { ++_updates; --_symbols; }
void onAddOrderBook(const OrderBook& order_book) override { ++_updates; ++_order_books; _max_order_books = std::max(_order_books, _max_order_books); }
void onUpdateOrderBook(const OrderBook& order_book, bool top) override { _max_order_book_levels = std::max(std::max(order_book.bids().size(), order_book.asks().size()), _max_order_book_levels); }
void onDeleteOrderBook(const OrderBook& order_book) override { ++_updates; --_order_books; }
void onAddLevel(const OrderBook& order_book, const Level& level, bool top) override { ++_updates; }
void onUpdateLevel(const OrderBook& order_book, const Level& level, bool top) override { ++_updates; _max_order_book_orders = std::max(level.Orders, _max_order_book_orders); }
void onDeleteLevel(const OrderBook& order_book, const Level& level, bool top) override { ++_updates; }
void onAddOrder(const Order& order) override { ++_updates; ++_orders; _max_orders = std::max(_orders, _max_orders); ++_add_orders; }
void onUpdateOrder(const Order& order) override { ++_updates; ++_update_orders; }
void onDeleteOrder(const Order& order) override { ++_updates; --_orders; ++_delete_orders; }
void onExecuteOrder(const Order& order, uint64_t price, uint64_t quantity) override { ++_updates; ++_execute_orders; }
private:
size_t _updates;
size_t _symbols;
size_t _max_symbols;
size_t _order_books;
size_t _max_order_books;
size_t _max_order_book_levels;
size_t _max_order_book_orders;
size_t _orders;
size_t _max_orders;
size_t _add_orders;
size_t _update_orders;
size_t _delete_orders;
size_t _execute_orders;
};
class MyITCHHandler : public ITCHHandler
{
public:
MyITCHHandler(MarketManager& market)
: _market(market),
_messages(0),
_errors(0)
{}
size_t messages() const { return _messages; }
size_t errors() const { return _errors; }
protected:
bool onMessage(const SystemEventMessage& message) override { ++_messages; return true; }
bool onMessage(const StockDirectoryMessage& message) override { ++_messages; Symbol symbol(message.StockLocate, message.Stock); _market.AddSymbol(symbol); _market.AddOrderBook(symbol); return true; }
bool onMessage(const StockTradingActionMessage& message) override { ++_messages; return true; }
bool onMessage(const RegSHOMessage& message) override { ++_messages; return true; }
bool onMessage(const MarketParticipantPositionMessage& message) override { ++_messages; return true; }
bool onMessage(const MWCBDeclineMessage& message) override { ++_messages; return true; }
bool onMessage(const MWCBStatusMessage& message) override { ++_messages; return true; }
bool onMessage(const IPOQuotingMessage& message) override { ++_messages; return true; }
bool onMessage(const AddOrderMessage& message) override { ++_messages; _market.AddOrder(Order::Limit(message.OrderReferenceNumber, message.StockLocate, (message.BuySellIndicator == 'B') ? OrderSide::BUY : OrderSide::SELL, message.Price, message.Shares)); return true; }
bool onMessage(const AddOrderMPIDMessage& message) override { ++_messages; _market.AddOrder(Order::Limit(message.OrderReferenceNumber, message.StockLocate, (message.BuySellIndicator == 'B') ? OrderSide::BUY : OrderSide::SELL, message.Price, message.Shares)); return true; }
bool onMessage(const OrderExecutedMessage& message) override { ++_messages; _market.ExecuteOrder(message.OrderReferenceNumber, message.ExecutedShares); return true; }
bool onMessage(const OrderExecutedWithPriceMessage& message) override { ++_messages; _market.ExecuteOrder(message.OrderReferenceNumber, message.ExecutionPrice, message.ExecutedShares); return true; }
bool onMessage(const OrderCancelMessage& message) override { ++_messages; _market.ReduceOrder(message.OrderReferenceNumber, message.CanceledShares); return true; }
bool onMessage(const OrderDeleteMessage& message) override { ++_messages; _market.DeleteOrder(message.OrderReferenceNumber); return true; }
bool onMessage(const OrderReplaceMessage& message) override { ++_messages; _market.ReplaceOrder(message.OriginalOrderReferenceNumber, message.NewOrderReferenceNumber, message.Price, message.Shares); return true; }
bool onMessage(const TradeMessage& message) override { ++_messages; return true; }
bool onMessage(const CrossTradeMessage& message) override { ++_messages; return true; }
bool onMessage(const BrokenTradeMessage& message) override { ++_messages; return true; }
bool onMessage(const NOIIMessage& message) override { ++_messages; return true; }
bool onMessage(const RPIIMessage& message) override { ++_messages; return true; }
bool onMessage(const LULDAuctionCollarMessage& message) override { ++_messages; return true; }
bool onMessage(const UnknownMessage& message) override { ++_errors; return true; }
private:
MarketManager& _market;
size_t _messages;
size_t _errors;
};
int main(int argc, char** argv)
{
auto parser = optparse::OptionParser().version("1.0.0.0");
parser.add_option("-i", "--input").dest("input").help("Input file name");
optparse::Values options = parser.parse_args(argc, argv);
// Print help
if (options.get("help"))
{
parser.print_help();
return 0;
}
MyMarketHandler market_handler;
MarketManager market(market_handler);
MyITCHHandler itch_handler(market);
// Open the input file or stdin
std::unique_ptr<Reader> input(new StdInput());
if (options.is_set("input"))
{
File* file = new File(Path(options.get("input")));
file->Open(true, false);
input.reset(file);
}
// Perform input
size_t size;
uint8_t buffer[8192];
std::cout << "ITCH processing...";
uint64_t timestamp_start = Timestamp::nano();
while ((size = input->Read(buffer, sizeof(buffer))) > 0)
{
// Process the buffer
itch_handler.Process(buffer, size);
}
uint64_t timestamp_stop = Timestamp::nano();
std::cout << "Done!" << std::endl;
std::cout << std::endl;
std::cout << "Errors: " << itch_handler.errors() << std::endl;
std::cout << std::endl;
size_t total_messages = itch_handler.messages();
size_t total_updates = market_handler.updates();
std::cout << "Processing time: " << CppBenchmark::ReporterConsole::GenerateTimePeriod(timestamp_stop - timestamp_start) << std::endl;
std::cout << "Total ITCH messages: " << total_messages << std::endl;
std::cout << "ITCH message latency: " << CppBenchmark::ReporterConsole::GenerateTimePeriod((timestamp_stop - timestamp_start) / total_messages) << std::endl;
std::cout << "ITCH message throughput: " << total_messages * 1000000000 / (timestamp_stop - timestamp_start) << " msg/s" << std::endl;
std::cout << "Total market updates: " << total_updates << std::endl;
std::cout << "Market update latency: " << CppBenchmark::ReporterConsole::GenerateTimePeriod((timestamp_stop - timestamp_start) / total_updates) << std::endl;
std::cout << "Market update throughput: " << total_updates * 1000000000 / (timestamp_stop - timestamp_start) << " upd/s" << std::endl;
std::cout << std::endl;
std::cout << "Market statistics: " << std::endl;
std::cout << "Max symbols: " << market_handler.max_symbols() << std::endl;
std::cout << "Max order books: " << market_handler.max_order_books() << std::endl;
std::cout << "Max order book levels: " << market_handler.max_order_book_levels() << std::endl;
std::cout << "Max order book orders: " << market_handler.max_order_book_orders() << std::endl;
std::cout << "Max orders: " << market_handler.max_orders() << std::endl;
std::cout << std::endl;
std::cout << "Order statistics: " << std::endl;
std::cout << "Add order operations: " << market_handler.add_orders() << std::endl;
std::cout << "Update order operations: " << market_handler.update_orders() << std::endl;
std::cout << "Delete order operations: " << market_handler.delete_orders() << std::endl;
std::cout << "Execute order operations: " << market_handler.execute_orders() << std::endl;
return 0;
}