18 #ifndef DATA_FEED_LIMIT_ORDER_BOOK_LIMIT_TREE_HPP
19 #define DATA_FEED_LIMIT_ORDER_BOOK_LIMIT_TREE_HPP
21 #include "structures.hpp"
22 #include "tsl/robin_map.h"
52 inline void set_best<Side::Buy>(
Limit** highest_buy,
Limit* limit) {
53 if (*highest_buy ==
nullptr) *highest_buy = limit;
54 else if (limit->key > (*highest_buy)->key) *highest_buy = limit;
63 inline void set_best<Side::Sell>(
Limit** lowest_sell,
Limit* limit) {
64 if (*lowest_sell ==
nullptr) *lowest_sell = limit;
65 else if (limit->key < (*lowest_sell)->key) *lowest_sell = limit;
85 inline void find_best<Side::Buy>(
Limit** highest_buy) {
86 if ((*highest_buy)->parent ==
nullptr)
88 *highest_buy =
static_cast<Limit*
>((*highest_buy)->left);
90 *highest_buy =
static_cast<Limit*
>((*highest_buy)->parent);
91 if (*highest_buy !=
nullptr)
92 *highest_buy =
static_cast<Limit*
>(BST::max(*highest_buy));
100 inline void find_best<Side::Sell>(
Limit** lowest_sell) {
101 if ((*lowest_sell)->parent ==
nullptr)
103 *lowest_sell =
static_cast<Limit*
>((*lowest_sell)->right);
105 *lowest_sell =
static_cast<Limit*
>((*lowest_sell)->parent);
106 if (*lowest_sell !=
nullptr)
107 *lowest_sell =
static_cast<Limit*
>(BST::min(*lowest_sell));
131 inline bool can_match<Side::Buy>(
Price limit,
Price market) {
135 return market == 0 || market <= limit;
145 inline bool can_match<Side::Sell>(
Price limit,
Price market) {
149 return market == 0 || market >= limit;
171 for(
auto item =
limits.begin(); item !=
limits.end(); item++)
192 reinterpret_cast<BST::Node<Price>**
>(&
root),
193 static_cast<BST::Node<Price>*
>(order->
limit)
209 static_cast<DLL::Node*
>(order)
226 auto limit_ = order->
limit;
227 if (order->prev ==
nullptr && order->next ==
nullptr) {
230 reinterpret_cast<BST::Node<Price>**
>(&
root),
231 static_cast<BST::Node<Price>*
>(limit_)
235 find_best<side>(&
best);
237 limits.erase(limit_->key);
244 reinterpret_cast<DLL::Node**
>(&limit_->order_head),
245 reinterpret_cast<DLL::Node**
>(&limit_->order_tail),
246 static_cast<DLL::Node*
>(order)
262 template<
typename Callback>
265 while (
best !=
nullptr && can_match<side>(
best->key, order->
price)) {
268 if (match->quantity >= order->
quantity) {
269 if (match->quantity == order->
quantity) {
272 did_fill(match->uid);
277 match->limit->volume -= order->
quantity;
289 did_fill(match->uid);
300 if (
limits.count(price))
return limits.at(price)->volume;
310 if (
limits.count(price))
return limits.at(price)->count;
319 #endif // DATA_FEED_LIMIT_ORDER_BOOK_LIMIT_TREE_HPP
Quantity quantity
the number of shares in the order
Definition: structures.hpp:60
void market(Order *order, Callback did_fill)
Perform a market order of given quantity on the given limit tree.
Definition: limit_tree.hpp:263
Count count
the number of orders at this limit price
Definition: structures.hpp:92
void limit(Order *order)
Place a limit order on the limit tree.
Definition: limit_tree.hpp:186
Volume volume
the total volume of orders for this tree
Definition: limit_tree.hpp:166
Count count_at(Price price) const
Return the number of orders at the given limit price.
Definition: limit_tree.hpp:309
uint64_t Volume
a type for limit total volume
Definition: structures.hpp:87
Limit * root
the sorted tree of orders in the book
Definition: limit_tree.hpp:156
Order * order_tail
the last order in the queue (last to execute)
Definition: structures.hpp:100
PriceLimitMap limits
a mapping of limit prices to limits
Definition: limit_tree.hpp:158
bool can_match(Price limit, Price market)
Return true if the market price can match with the limit price.
void set_best(Limit **best, Limit *limit)
Set the best to the given limit if the given limit is better.
Logic for sending and receiving messages on a financial data feed.
Definition: heartbeat.hpp:28
A single order in the LimitOrderBook.
Definition: structures.hpp:54
Order * order_head
the first order in the queue (next to execute)
Definition: structures.hpp:98
uint64_t Price
A type for order prices.
Definition: messages.hpp:103
Price last_best_price
the last best price
Definition: limit_tree.hpp:162
A price limit containing a FIFO queue of Order objects.
Definition: structures.hpp:90
A single side (buy/sell) of the LimitOrderBook.
Definition: limit_tree.hpp:154
void find_best(Limit **best)
Find the next best when removing the best from the tree.
tsl::robin_map< Price, Limit * > PriceLimitMap
a map of prices to limit pointers
Definition: limit_tree.hpp:31
uint64_t Price
a type for order prices
Definition: structures.hpp:48
const Price price
the limit price for the order (market price if market order)
Definition: structures.hpp:62
void cancel(Order *order)
Remove an order from the limit tree.
Definition: limit_tree.hpp:224
Count count
the total number of active orders for this tree
Definition: limit_tree.hpp:164
uint32_t Count
a type for limit price order counts
Definition: structures.hpp:85
Volume volume
the total amount of volume at this limit price (sum of order shares)
Definition: structures.hpp:96
void clear()
Clear all the limits in the tree.
Definition: limit_tree.hpp:169
Limit * limit
the limit this order falls under
Definition: structures.hpp:64
Limit * best
the best order
Definition: limit_tree.hpp:160
Volume volume_at(Price price) const
Return the volume of orders at the given limit price.
Definition: limit_tree.hpp:299