forked from chronoxor/CppTrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
market_manager.h
291 lines (250 loc) · 9.79 KB
/
market_manager.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
/*!
\file market_manager.h
\brief Market manager definition
\author Ivan Shynkarenka
\date 03.08.2017
\copyright MIT License
*/
#ifndef CPPTRADER_MATCHING_MARKET_MANAGER_H
#define CPPTRADER_MATCHING_MARKET_MANAGER_H
#include "fast_hash.h"
#include "market_handler.h"
#include "containers/hashmap.h"
#include "memory/allocator_pool.h"
#include <cassert>
#include <vector>
namespace CppTrader {
/*!
\namespace CppTrader::Matching
\brief Matching engine definitions
*/
namespace Matching {
//! Market manager
/*!
Market manager is used to manage the market with symbols, orders and order books.
Automatic orders matching can be enabled with EnableMatching() method or can be
manually performed with Match() method.
Not thread-safe.
*/
class MarketManager
{
friend class OrderBook;
public:
//! Symbols container
typedef std::vector<Symbol*> Symbols;
//! Order books container
typedef std::vector<OrderBook*> OrderBooks;
//! Orders container
typedef CppCommon::HashMap<uint64_t, OrderNode*, FastHash> Orders;
MarketManager();
MarketManager(MarketHandler& market_handler);
MarketManager(const MarketManager&) = delete;
MarketManager(MarketManager&&) = delete;
~MarketManager();
MarketManager& operator=(const MarketManager&) = delete;
MarketManager& operator=(MarketManager&&) = delete;
//! Get the symbols container
const Symbols& symbols() const noexcept { return _symbols; }
//! Get the order books container
const OrderBooks& order_books() const noexcept { return _order_books; }
//! Get the orders container
const Orders& orders() const noexcept { return _orders; }
//! Get the symbol with the given Id
/*!
\param id - Symbol Id
\return Pointer to the symobl with the given Id or nullptr
*/
const Symbol* GetSymbol(uint32_t id) const noexcept;
//! Get the order book for the given symbol Id
/*!
\param id - Symbol Id of the order book
\return Pointer to the order book with the given symbol Id or nullptr
*/
const OrderBook* GetOrderBook(uint32_t id) const noexcept;
//! Get the order with the given Id
/*!
\param id - Order Id
\return Pointer to the order with the given Id or nullptr
*/
const Order* GetOrder(uint64_t id) const noexcept;
//! Add a new symbol
/*!
\param symbol - Symbol to add
\return Error code
*/
ErrorCode AddSymbol(const Symbol& symbol);
//! Delete the symbol
/*!
\param id - Symbol Id
\return Error code
*/
ErrorCode DeleteSymbol(uint32_t id);
//! Add a new order book
/*!
\param symbol - Symbol of the order book to add
\return Error code
*/
ErrorCode AddOrderBook(const Symbol& symbol);
//! Delete the order book
/*!
\param id - Symbol Id of the order book
\return Error code
*/
ErrorCode DeleteOrderBook(uint32_t id);
//! Add a new order
/*!
\param order - Order to add
\return Error code
*/
ErrorCode AddOrder(const Order& order);
//! Reduce the order by the given quantity
/*!
\param id - Order Id
\param quantity - Order quantity to reduce
\return Error code
*/
ErrorCode ReduceOrder(uint64_t id, uint64_t quantity);
//! Modify the order
/*!
Order new quantity will be calculated in a following way:
\code{.cpp}
oder.Quantity = new_quantity;
oder.LeavesQuantity = new_quantity;
\endcode
\param id - Order Id
\param new_price - Order price to modify
\param new_quantity - Order quantity to modify
\return Error code
*/
ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity);
//! Mitigate the order
/*!
The in-flight mitigation functionality prevents an order from being filled
for a quantity greater than the quantity requested by the user. It protects
from the risk of a resting order being filled between the time an order
modification is submitted and the time the order modification is processed
and applied to the order.
Order new quantity will be calculated in a following way:
\code{.cpp}
if (new_quantity > oder.ExecutedQuantity)
{
oder.Quantity = new_quantity;
oder.LeavesQuantity = new_quantity - oder.ExecutedQuantity;
// Order will be modified...
}
else
{
oder.Quantity = new_quantity;
oder.LeavesQuantity = 0;
// Order will be canceled...
}
\endcode
\param id - Order Id
\param new_price - Order price to mitigate
\param new_quantity - Order quantity to mitigate
\return Error code
*/
ErrorCode MitigateOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity);
//! Replace the order with a similar order but different Id, price and quantity
/*!
\param id - Order Id
\param new_id - Order Id to replace
\param new_price - Order price to replace
\param new_quantity - Order quantity to replace
\return Error code
*/
ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity);
//! Replace the order with a new one
/*!
\param id - Order Id
\param new_order - Order to replace
\return Error code
*/
ErrorCode ReplaceOrder(uint64_t id, const Order& new_order);
//! Delete the order
/*!
\param id - Order Id
\return Error code
*/
ErrorCode DeleteOrder(uint64_t id);
//! Execute the order
/*!
\param id - Order Id
\param quantity - Order executed quantity
\return Error code
*/
ErrorCode ExecuteOrder(uint64_t id, uint64_t quantity);
//! Execute the order
/*!
\param id - Order Id
\param price - Order executed price
\param quantity - Order executed quantity
\return Error code
*/
ErrorCode ExecuteOrder(uint64_t id, uint64_t price, uint64_t quantity);
//! Is automatic matching enabled?
bool IsMatchingEnabled() const noexcept { return _matching; }
//! Enable automatic matching
void EnableMatching() { _matching = true; Match(); }
//! Disable automatic matching
void DisableMatching() { _matching = false; }
//! Match crossed orders in all order books
/*!
Method will match all crossed orders in each order book. Buy orders will be
matched with sell orders at arbitrage price starting from the top of the book.
Matched orders will be executed with deleted form the order book. After the
matching operation each order book will have the best bid price guarantied
less than the best ask price!
*/
void Match();
private:
// Market handler
static MarketHandler _default;
MarketHandler& _market_handler;
// Auxiliary memory manager
CppCommon::DefaultMemoryManager _auxiliary_memory_manager;
// Bid/Ask price levels
CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _level_memory_manager;
CppCommon::PoolAllocator<LevelNode, CppCommon::DefaultMemoryManager> _level_pool;
// Symbols
CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _symbol_memory_manager;
CppCommon::PoolAllocator<Symbol, CppCommon::DefaultMemoryManager> _symbol_pool;
Symbols _symbols;
// Order books
CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _order_book_memory_manager;
CppCommon::PoolAllocator<OrderBook, CppCommon::DefaultMemoryManager> _order_book_pool;
OrderBooks _order_books;
// Orders
CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _order_memory_manager;
CppCommon::PoolAllocator<OrderNode, CppCommon::DefaultMemoryManager> _order_pool;
Orders _orders;
ErrorCode AddMarketOrder(const Order& order, bool recursive);
ErrorCode AddLimitOrder(const Order& order, bool recursive);
ErrorCode AddStopOrder(const Order& order, bool recursive);
ErrorCode AddStopLimitOrder(const Order& order, bool recursive);
ErrorCode ReduceOrder(uint64_t id, uint64_t quantity, bool recursive);
ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity, bool mitigate, bool recursive);
ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity, bool recursive);
ErrorCode DeleteOrder(uint64_t id, bool recursive);
// Matching
bool _matching;
void Match(OrderBook* order_book_ptr);
void MatchMarket(OrderBook* order_book_ptr, Order* order_ptr);
void MatchLimit(OrderBook* order_book_ptr, Order* order_ptr);
void MatchOrder(OrderBook* order_book_ptr, Order* order_ptr);
bool ActivateStopOrders(OrderBook* order_book_ptr);
bool ActivateStopOrders(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t stop_price);
bool ActivateStopOrder(OrderBook* order_book_ptr, OrderNode* order_ptr);
bool ActivateStopLimitOrder(OrderBook* order_book_ptr, OrderNode* order_ptr);
uint64_t CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume);
uint64_t CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* bid_level_ptr, LevelNode* ask_level_ptr);
void ExecuteMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume);
void RecalculateTrailingStopPrice(OrderBook* order_book_ptr, LevelNode* level_ptr);
void UpdateLevel(const OrderBook& order_book, const LevelUpdate& update) const;
};
/*! \example market_manager.cpp Market manager example */
/*! \example matching_engine.cpp Matching engine example */
} // namespace Matching
} // namespace CppTrader
#include "market_manager.inl"
#endif // CPPTRADER_MATCHING_MARKET_MANAGER_H