CppTrader  1.0.4.0
C++ Trader
market_manager.h
Go to the documentation of this file.
1 
9 #ifndef CPPTRADER_MATCHING_MARKET_MANAGER_H
10 #define CPPTRADER_MATCHING_MARKET_MANAGER_H
11 
12 #include "fast_hash.h"
13 #include "market_handler.h"
14 
15 #include "containers/hashmap.h"
16 #include "memory/allocator_pool.h"
17 
18 #include <cassert>
19 #include <vector>
20 
21 namespace CppTrader {
22 
27 namespace Matching {
28 
30 
39 {
40  friend class OrderBook;
41 
42 public:
44  typedef std::vector<Symbol*> Symbols;
46  typedef std::vector<OrderBook*> OrderBooks;
48  typedef CppCommon::HashMap<uint64_t, OrderNode*, FastHash> Orders;
49 
50  MarketManager();
51  MarketManager(MarketHandler& market_handler);
52  MarketManager(const MarketManager&) = delete;
55 
58 
60  const Symbols& symbols() const noexcept { return _symbols; }
62  const OrderBooks& order_books() const noexcept { return _order_books; }
64  const Orders& orders() const noexcept { return _orders; }
65 
67 
71  const Symbol* GetSymbol(uint32_t id) const noexcept;
73 
77  const OrderBook* GetOrderBook(uint32_t id) const noexcept;
79 
83  const Order* GetOrder(uint64_t id) const noexcept;
84 
86 
90  ErrorCode AddSymbol(const Symbol& symbol);
92 
96  ErrorCode DeleteSymbol(uint32_t id);
97 
99 
103  ErrorCode AddOrderBook(const Symbol& symbol);
105 
109  ErrorCode DeleteOrderBook(uint32_t id);
110 
112 
116  ErrorCode AddOrder(const Order& order);
118 
123  ErrorCode ReduceOrder(uint64_t id, uint64_t quantity);
125 
137  ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity);
139 
169  ErrorCode MitigateOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity);
171 
178  ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity);
180 
185  ErrorCode ReplaceOrder(uint64_t id, const Order& new_order);
187 
191  ErrorCode DeleteOrder(uint64_t id);
192 
194 
199  ErrorCode ExecuteOrder(uint64_t id, uint64_t quantity);
201 
207  ErrorCode ExecuteOrder(uint64_t id, uint64_t price, uint64_t quantity);
208 
210  bool IsMatchingEnabled() const noexcept { return _matching; }
212  void EnableMatching() { _matching = true; Match(); }
214  void DisableMatching() { _matching = false; }
215 
217 
224  void Match();
225 
226 private:
227  // Market handler
228  static MarketHandler _default;
229  MarketHandler& _market_handler;
230 
231  // Auxiliary memory manager
232  CppCommon::DefaultMemoryManager _auxiliary_memory_manager;
233 
234  // Bid/Ask price levels
235  CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _level_memory_manager;
236  CppCommon::PoolAllocator<LevelNode, CppCommon::DefaultMemoryManager> _level_pool;
237 
238  // Symbols
239  CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _symbol_memory_manager;
240  CppCommon::PoolAllocator<Symbol, CppCommon::DefaultMemoryManager> _symbol_pool;
241  Symbols _symbols;
242 
243  // Order books
244  CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _order_book_memory_manager;
245  CppCommon::PoolAllocator<OrderBook, CppCommon::DefaultMemoryManager> _order_book_pool;
246  OrderBooks _order_books;
247 
248  // Orders
249  CppCommon::PoolMemoryManager<CppCommon::DefaultMemoryManager> _order_memory_manager;
250  CppCommon::PoolAllocator<OrderNode, CppCommon::DefaultMemoryManager> _order_pool;
251  Orders _orders;
252 
253  ErrorCode AddMarketOrder(const Order& order, bool recursive);
254  ErrorCode AddLimitOrder(const Order& order, bool recursive);
255  ErrorCode AddStopOrder(const Order& order, bool recursive);
256  ErrorCode AddStopLimitOrder(const Order& order, bool recursive);
257  ErrorCode ReduceOrder(uint64_t id, uint64_t quantity, bool recursive);
258  ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity, bool mitigate, bool recursive);
259  ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity, bool recursive);
260  ErrorCode DeleteOrder(uint64_t id, bool recursive);
261 
262  // Matching
263  bool _matching;
264 
265  void Match(OrderBook* order_book_ptr);
266  void MatchMarket(OrderBook* order_book_ptr, Order* order_ptr);
267  void MatchLimit(OrderBook* order_book_ptr, Order* order_ptr);
268  void MatchOrder(OrderBook* order_book_ptr, Order* order_ptr);
269 
270  bool ActivateStopOrders(OrderBook* order_book_ptr);
271  bool ActivateStopOrders(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t stop_price);
272  bool ActivateStopOrder(OrderBook* order_book_ptr, OrderNode* order_ptr);
273  bool ActivateStopLimitOrder(OrderBook* order_book_ptr, OrderNode* order_ptr);
274 
275  uint64_t CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume);
276  uint64_t CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* bid_level_ptr, LevelNode* ask_level_ptr);
277  void ExecuteMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume);
278  void RecalculateTrailingStopPrice(OrderBook* order_book_ptr, LevelNode* level_ptr);
279 
280  void UpdateLevel(const OrderBook& order_book, const LevelUpdate& update) const;
281 };
282 
286 } // namespace Matching
287 } // namespace CppTrader
288 
289 #include "market_manager.inl"
290 
291 #endif // CPPTRADER_MATCHING_MARKET_MANAGER_H
void DisableMatching()
Disable automatic matching.
CppCommon::HashMap< uint64_t, OrderNode *, FastHash > Orders
Orders container.
ErrorCode ReduceOrder(uint64_t id, uint64_t quantity)
Reduce the order by the given quantity.
MarketManager & operator=(MarketManager &&)=delete
MarketManager & operator=(const MarketManager &)=delete
const OrderBook * GetOrderBook(uint32_t id) const noexcept
Get the order book for the given symbol Id.
ErrorCode MitigateOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity)
Mitigate the order.
bool IsMatchingEnabled() const noexcept
Is automatic matching enabled?
ErrorCode AddOrderBook(const Symbol &symbol)
Add a new order book.
const OrderBooks & order_books() const noexcept
Get the order books container.
ErrorCode ExecuteOrder(uint64_t id, uint64_t quantity)
Execute the order.
ErrorCode DeleteOrder(uint64_t id)
Delete the order.
void EnableMatching()
Enable automatic matching.
const Symbols & symbols() const noexcept
Get the symbols container.
ErrorCode AddSymbol(const Symbol &symbol)
Add a new symbol.
void Match()
Match crossed orders in all order books.
std::vector< OrderBook * > OrderBooks
Order books container.
MarketManager(const MarketManager &)=delete
ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity)
Modify the order.
ErrorCode AddOrder(const Order &order)
Add a new order.
const Order * GetOrder(uint64_t id) const noexcept
Get the order with the given Id.
const Orders & orders() const noexcept
Get the orders container.
ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity)
Replace the order with a similar order but different Id, price and quantity.
ErrorCode DeleteSymbol(uint32_t id)
Delete the symbol.
MarketManager(MarketManager &&)=delete
std::vector< Symbol * > Symbols
Symbols container.
ErrorCode DeleteOrderBook(uint32_t id)
Delete the order book.
const Symbol * GetSymbol(uint32_t id) const noexcept
Get the symbol with the given Id.
Fast hash helper definition.
Market handler definition.
Market manager inline implementation.
ErrorCode
Error code.
Definition: errors.h:21
C++ Trader project definitions.
Definition: errors.h:16
Price level node.
Definition: level.h:79
Price level update.
Definition: level.h:110