CppTrader 1.0.5.0
C++ Trader
Loading...
Searching...
No Matches
order_book.h
Go to the documentation of this file.
1
9#ifndef CPPTRADER_MATCHING_ORDER_BOOK_H
10#define CPPTRADER_MATCHING_ORDER_BOOK_H
11
12#include "level.h"
13#include "symbol.h"
14
15#include "memory/allocator_pool.h"
16
17namespace CppTrader {
18namespace Matching {
19
20class MarketManager;
21
23
29{
30 friend class MarketManager;
31
32public:
34 typedef CppCommon::BinTreeAVL<LevelNode, std::less<LevelNode>> Levels;
35
36 OrderBook(MarketManager& manager, const Symbol& symbol);
37 OrderBook(const OrderBook&) = delete;
38 OrderBook(OrderBook&&) = delete;
39 ~OrderBook();
40
41 OrderBook& operator=(const OrderBook&) = delete;
43
45 explicit operator bool() const noexcept { return !empty(); }
46
48 bool empty() const noexcept { return size() == 0; }
49
51 size_t size() const noexcept { return _bids.size() + _asks.size() + _buy_stop.size() + _sell_stop.size() + _trailing_buy_stop.size() + _trailing_sell_stop.size(); }
52
54 const Symbol& symbol() const noexcept { return _symbol; }
55
57 const LevelNode* best_bid() const noexcept { return _best_bid; }
59 const LevelNode* best_ask() const noexcept { return _best_ask; }
60
62 const Levels& bids() const noexcept { return _bids; }
64 const Levels& asks() const noexcept { return _asks; }
65
67 const LevelNode* best_buy_stop() const noexcept { return _best_buy_stop; }
69 const LevelNode* best_sell_stop() const noexcept { return _best_sell_stop; }
70
72 const Levels& buy_stop() const noexcept { return _buy_stop; }
74 const Levels& sell_stop() const noexcept { return _sell_stop; }
75
77 const LevelNode* best_trailing_buy_stop() const noexcept { return _best_trailing_buy_stop; }
79 const LevelNode* best_trailing_sell_stop() const noexcept { return _best_trailing_sell_stop; }
80
82 const Levels& trailing_buy_stop() const noexcept { return _trailing_buy_stop; }
84 const Levels& trailing_sell_stop() const noexcept { return _trailing_sell_stop; }
85
86 template <class TOutputStream>
87 friend TOutputStream& operator<<(TOutputStream& stream, const OrderBook& order_book);
88
90
94 const LevelNode* GetBid(uint64_t price) const noexcept;
96
100 const LevelNode* GetAsk(uint64_t price) const noexcept;
101
103
107 const LevelNode* GetBuyStopLevel(uint64_t price) const noexcept;
109
113 const LevelNode* GetSellStopLevel(uint64_t price) const noexcept;
114
116
120 const LevelNode* GetTrailingBuyStopLevel(uint64_t price) const noexcept;
122
126 const LevelNode* GetTrailingSellStopLevel(uint64_t price) const noexcept;
127
128private:
129 // Market manager
130 MarketManager& _manager;
131
132 // Order book symbol
133 Symbol _symbol;
134
135 // Bid/Ask price levels
136 LevelNode* _best_bid;
137 LevelNode* _best_ask;
138 Levels _bids;
139 Levels _asks;
140
141 // Price level management
142 LevelNode* GetNextLevel(LevelNode* level) noexcept;
143 LevelNode* AddLevel(OrderNode* order_ptr);
144 LevelNode* DeleteLevel(OrderNode* order_ptr);
145
146 // Orders management
147 LevelUpdate AddOrder(OrderNode* order_ptr);
148 LevelUpdate ReduceOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible);
149 LevelUpdate DeleteOrder(OrderNode* order_ptr);
150
151 // Buy/Sell stop orders levels
152 LevelNode* _best_buy_stop;
153 LevelNode* _best_sell_stop;
154 Levels _buy_stop;
155 Levels _sell_stop;
156
157 // Stop orders price level management
158 LevelNode* GetNextStopLevel(LevelNode* level) noexcept;
159 LevelNode* AddStopLevel(OrderNode* order_ptr);
160 LevelNode* DeleteStopLevel(OrderNode* order_ptr);
161
162 // Stop orders management
163 void AddStopOrder(OrderNode* order_ptr);
164 void ReduceStopOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible);
165 void DeleteStopOrder(OrderNode* order_ptr);
166
167 // Buy/Sell trailing stop orders levels
168 LevelNode* _best_trailing_buy_stop;
169 LevelNode* _best_trailing_sell_stop;
170 Levels _trailing_buy_stop;
171 Levels _trailing_sell_stop;
172
173 // Trailing stop orders price level management
174 LevelNode* GetNextTrailingStopLevel(LevelNode* level) noexcept;
175 LevelNode* AddTrailingStopLevel(OrderNode* order_ptr);
176 LevelNode* DeleteTrailingStopLevel(OrderNode* order_ptr);
177
178 // Trailing stop orders management
179 void AddTrailingStopOrder(OrderNode* order_ptr);
180 void ReduceTrailingStopOrder(OrderNode* order_ptr, uint64_t quantity, uint64_t hidden, uint64_t visible);
181 void DeleteTrailingStopOrder(OrderNode* order_ptr);
182
183 // Trailing stop price calculation
184 uint64_t CalculateTrailingStopPrice(const Order& order) const noexcept;
185
186 // Market last and trailing prices
187 uint64_t _last_bid_price;
188 uint64_t _last_ask_price;
189 uint64_t _matching_bid_price;
190 uint64_t _matching_ask_price;
191 uint64_t _trailing_bid_price;
192 uint64_t _trailing_ask_price;
193
194 // Update market last prices
195 uint64_t GetMarketPriceBid() const noexcept;
196 uint64_t GetMarketPriceAsk() const noexcept;
197 uint64_t GetMarketTrailingStopPriceBid() const noexcept;
198 uint64_t GetMarketTrailingStopPriceAsk() const noexcept;
199 void UpdateLastPrice(const Order& order, uint64_t price) noexcept;
200 void UpdateMatchingPrice(const Order& order, uint64_t price) noexcept;
201 void ResetMatchingPrice() noexcept;
202};
203
204} // namespace Matching
205} // namespace CppTrader
206
207#include "order_book.inl"
208
209#endif // CPPTRADER_MATCHING_ORDER_BOOK_H
const LevelNode * best_trailing_buy_stop() const noexcept
Get the order book best trailing buy stop order price level.
Definition order_book.h:77
const LevelNode * best_trailing_sell_stop() const noexcept
Get the order book best trailing sell stop order price level.
Definition order_book.h:79
const LevelNode * GetAsk(uint64_t price) const noexcept
Get the order book ask price level with the given price.
const LevelNode * best_sell_stop() const noexcept
Get the order book best sell stop order price level.
Definition order_book.h:69
bool empty() const noexcept
Is the order book empty?
Definition order_book.h:48
const Levels & asks() const noexcept
Get the order book asks container.
Definition order_book.h:64
const LevelNode * best_ask() const noexcept
Get the order book best ask price level.
Definition order_book.h:59
size_t size() const noexcept
Get the order book size.
Definition order_book.h:51
const Levels & trailing_sell_stop() const noexcept
Get the order book trailing sell stop orders container.
Definition order_book.h:84
friend TOutputStream & operator<<(TOutputStream &stream, const OrderBook &order_book)
OrderBook & operator=(const OrderBook &)=delete
const LevelNode * GetTrailingBuyStopLevel(uint64_t price) const noexcept
Get the order book trailing buy stop level with the given price.
const Symbol & symbol() const noexcept
Get the order book symbol.
Definition order_book.h:54
const LevelNode * GetBid(uint64_t price) const noexcept
Get the order book bid price level with the given price.
const LevelNode * GetSellStopLevel(uint64_t price) const noexcept
Get the order book sell stop level with the given price.
const Levels & trailing_buy_stop() const noexcept
Get the order book trailing buy stop orders container.
Definition order_book.h:82
const LevelNode * GetTrailingSellStopLevel(uint64_t price) const noexcept
Get the order book trailing sell stop level with the given price.
const Levels & sell_stop() const noexcept
Get the order book sell stop orders container.
Definition order_book.h:74
const LevelNode * best_buy_stop() const noexcept
Get the order book best buy stop order price level.
Definition order_book.h:67
const LevelNode * best_bid() const noexcept
Get the order book best bid price level.
Definition order_book.h:57
OrderBook(const OrderBook &)=delete
const LevelNode * GetBuyStopLevel(uint64_t price) const noexcept
Get the order book buy stop level with the given price.
const Levels & bids() const noexcept
Get the order book bids container.
Definition order_book.h:62
OrderBook(OrderBook &&)=delete
CppCommon::BinTreeAVL< LevelNode, std::less< LevelNode > > Levels
Price level container.
Definition order_book.h:34
const Levels & buy_stop() const noexcept
Get the order book buy stop orders container.
Definition order_book.h:72
OrderBook & operator=(OrderBook &&)=delete
Price level definition.
C++ Trader project definitions.
Definition errors.h:16
Price level node.
Definition level.h:79
Price level update.
Definition level.h:110
Symbol definition.