CppTrader 1.0.5.0
C++ Trader
Loading...
Searching...
No Matches
order_book.inl
Go to the documentation of this file.
1
9namespace CppTrader {
10namespace Matching {
11
12template <class TOutputStream>
13inline TOutputStream& operator<<(TOutputStream& stream, const OrderBook& order_book)
14{
15 stream << "OrderBook(Symbol=" << order_book._symbol
16 << "; Bids=" << order_book._bids.size()
17 << "; Asks=" << order_book._asks.size()
18 << "; BuyStop=" << order_book._buy_stop.size()
19 << "; SellStop=" << order_book._sell_stop.size()
20 << "; TrailingBuyStop=" << order_book._trailing_buy_stop.size()
21 << "; TrailingSellStop=" << order_book._trailing_sell_stop.size()
22 << ")";
23 return stream;
24}
25
26inline const LevelNode* OrderBook::GetBid(uint64_t price) const noexcept
27{
28 auto it = _bids.find(LevelNode(LevelType::BID, price));
29 return (it != _bids.end()) ? it.operator->() : nullptr;
30}
31
32inline const LevelNode* OrderBook::GetAsk(uint64_t price) const noexcept
33{
34 auto it = _asks.find(LevelNode(LevelType::ASK, price));
35 return (it != _asks.end()) ? it.operator->() : nullptr;
36}
37
38inline const LevelNode* OrderBook::GetBuyStopLevel(uint64_t price) const noexcept
39{
40 auto it = _buy_stop.find(LevelNode(LevelType::ASK, price));
41 return (it != _buy_stop.end()) ? it.operator->() : nullptr;
42}
43
44inline const LevelNode* OrderBook::GetSellStopLevel(uint64_t price) const noexcept
45{
46 auto it = _sell_stop.find(LevelNode(LevelType::BID, price));
47 return (it != _sell_stop.end()) ? it.operator->() : nullptr;
48}
49
50inline const LevelNode* OrderBook::GetTrailingBuyStopLevel(uint64_t price) const noexcept
51{
52 auto it = _trailing_buy_stop.find(LevelNode(LevelType::ASK, price));
53 return (it != _trailing_buy_stop.end()) ? it.operator->() : nullptr;
54}
55
56inline const LevelNode* OrderBook::GetTrailingSellStopLevel(uint64_t price) const noexcept
57{
58 auto it = _trailing_sell_stop.find(LevelNode(LevelType::BID, price));
59 return (it != _trailing_sell_stop.end()) ? it.operator->() : nullptr;
60}
61
62inline LevelNode* OrderBook::GetNextLevel(LevelNode* level) noexcept
63{
64 if (level->IsBid())
65 {
66 Levels::reverse_iterator it(&_bids, level);
67 ++it;
68 return it.operator->();
69 }
70 else
71 {
72 Levels::iterator it(&_asks, level);
73 ++it;
74 return it.operator->();
75 }
76}
77
78inline LevelNode* OrderBook::GetNextStopLevel(LevelNode* level) noexcept
79{
80 if (level->IsBid())
81 {
82 Levels::reverse_iterator it(&_sell_stop, level);
83 ++it;
84 return it.operator->();
85 }
86 else
87 {
88 Levels::iterator it(&_buy_stop, level);
89 ++it;
90 return it.operator->();
91 }
92}
93
94inline LevelNode* OrderBook::GetNextTrailingStopLevel(LevelNode* level) noexcept
95{
96 if (level->IsBid())
97 {
98 Levels::reverse_iterator it(&_trailing_sell_stop, level);
99 ++it;
100 return it.operator->();
101 }
102 else
103 {
104 Levels::iterator it(&_trailing_buy_stop, level);
105 ++it;
106 return it.operator->();
107 }
108}
109
110inline uint64_t OrderBook::GetMarketPriceBid() const noexcept
111{
112 uint64_t matching_price = _matching_bid_price;
113 uint64_t best_price = (_best_bid != nullptr) ? _best_bid->Price : 0;
114 return std::max(matching_price, best_price);
115}
116
117inline uint64_t OrderBook::GetMarketPriceAsk() const noexcept
118{
119 uint64_t matching_price = _matching_ask_price;
120 uint64_t best_price = (_best_ask != nullptr) ? _best_ask->Price : std::numeric_limits<uint64_t>::max();
121 return std::min(matching_price, best_price);
122}
123
124inline uint64_t OrderBook::GetMarketTrailingStopPriceBid() const noexcept
125{
126 uint64_t last_price = _last_bid_price;
127 uint64_t best_price = (_best_bid != nullptr) ? _best_bid->Price : 0;
128 return std::min(last_price, best_price);
129}
130
131inline uint64_t OrderBook::GetMarketTrailingStopPriceAsk() const noexcept
132{
133 uint64_t last_price = _last_ask_price;
134 uint64_t best_price = (_best_ask != nullptr) ? _best_ask->Price : std::numeric_limits<uint64_t>::max();
135 return std::max(last_price, best_price);
136}
137
138inline void OrderBook::UpdateLastPrice(const Order& order, uint64_t price) noexcept
139{
140 if (order.IsBuy())
141 _last_bid_price = price;
142 else
143 _last_ask_price = price;
144}
145
146inline void OrderBook::UpdateMatchingPrice(const Order& order, uint64_t price) noexcept
147{
148 if (order.IsBuy())
149 _matching_bid_price = price;
150 else
151 _matching_ask_price = price;
152}
153
154inline void OrderBook::ResetMatchingPrice() noexcept
155{
156 _matching_bid_price = 0;
157 _matching_ask_price = std::numeric_limits<uint64_t>::max();
158}
159
160} // namespace Matching
161} // namespace CppTrader
const LevelNode * GetAsk(uint64_t price) const noexcept
Get the order book ask price level with the given price.
const LevelNode * GetTrailingBuyStopLevel(uint64_t price) const noexcept
Get the order book trailing buy stop level with the given price.
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 LevelNode * GetTrailingSellStopLevel(uint64_t price) const noexcept
Get the order book trailing sell stop level with the given price.
const LevelNode * GetBuyStopLevel(uint64_t price) const noexcept
Get the order book buy stop level with the given price.
TOutputStream & operator<<(TOutputStream &stream, ErrorCode error)
Definition errors.inl:13
C++ Trader project definitions.
Definition errors.h:16
uint64_t Price
Level price.
Definition level.h:36
Price level node.
Definition level.h:79