JayBeams  0.1
Another project to have fun coding.
order_book.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_order_book_hpp
2 #define jb_itch5_order_book_hpp
3 
9 #include <jb/config_object.hpp>
10 
11 #include <type_traits>
12 
13 namespace jb {
14 namespace itch5 {
15 
16 /// Number of prices on a side order book
17 using book_depth_t = unsigned long int;
18 
19 /**
20  * Maintain the ITCH-5.0 order book for a single security.
21  *
22  * ITCH-5.0, like other market data feeds provide order-by-order
23  * detail, that is, the feed includes a message for each order
24  * received by the exchange, as well as the changes to these
25  * orders, i.e. when the execute, when their quantity (and/or price)
26  * is modified, and when they are canceled.. Such feeds are sometimes
27  * called Level III feeds. Typically only orders that do not
28  * immediately execute in full are included in the feed.
29  *
30  * There is substantial variation in the format of the messages, and
31  * some variation as to whether executions are represented differently
32  * than a partial cancel, and whether changes in price are allowed or
33  * create new order ids.
34  *
35  * This class encapsulates the order book data structure as well as its
36  * configuration.
37  *
38  * This class receives a stream of (unnormalized) ITCH-5.0 messages
39  * for a single security, and organizes the orders in a
40  * book, i.e. a data structure where orders at the same price are
41  * grouped together, and one can quickly ask:
42  *
43  * - What is the best bid (highest price of BUY orders) and what is
44  * the total quantity available at that price?
45  * - What is the best offer (lowest price of SELL orders) and what is
46  * the total quantity available at that price?
47  *
48  * This is a template class.
49  * @tparam book_type defines data structure type of price book,
50  * both sides buy and sell.
51  * Must be compatible with jb::itch5::map_based_order_book
52  *
53  * @param cfg book_type config
54  */
55 
56 template <typename book_type>
57 class order_book {
58 public:
59  using book_type_config = typename book_type::config;
60 
61  /// Initialize an empty order book.
62  explicit order_book(book_type_config const& cfg)
63  : buy_(cfg)
64  , sell_(cfg) {
65  }
66 
67  //@{
68  /**
69  * @name Accessors
70  */
71 
72  /// @returns the best bid price and quantity
73  half_quote best_bid() const {
74  return buy_.best_quote();
75  }
76 
77  /// @returns the worst bid price and quantity
79  return buy_.worst_quote();
80  }
81 
82  /// @returns the best offer price and quantity
84  return sell_.best_quote();
85  }
86 
87  /// @returns the worst offer price and quantity
89  return sell_.worst_quote();
90  }
91 
93  return buy_.count();
94  }
96  return sell_.count();
97  }
98 
99  /// @returns the book depth, this is the number of price levels on
100  /// this order book.
102  return buy_count() + sell_count();
103  };
104 
105  //@}
106 
107  /**
108  * Handle a new order.
109  *
110  * Update the quantity at the right price level in the correct
111  * side of the book.
112  *
113  * @param side whether the order is a buy or a sell
114  * @param px the price of the order
115  * @param qty the quantity of the order
116  * @return true if the inside changed
117  */
119  if (side == buy_sell_indicator_t('B')) {
120  return buy_.add_order(px, qty);
121  }
122  return sell_.add_order(px, qty);
123  }
124 
125  /**
126  * Handle an order reduction, which includes executions,
127  * cancels and replaces.
128  *
129  * @param side whether the order is a buy or a sell
130  * @param px the price of the order
131  * @param reduced_qty the executed quantity of the order
132  * @returns true if the inside changed
133  */
135  buy_sell_indicator_t side, price4_t px, int reduced_qty) {
136  if (side == buy_sell_indicator_t('B')) {
137  return buy_.reduce_order(px, reduced_qty);
138  }
139  return sell_.reduce_order(px, reduced_qty);
140  }
141 
142 private:
143  typename book_type::buys_t buy_;
144  typename book_type::sells_t sell_;
145 };
146 
147 } // namespace itch5
148 } // namespace jb
149 
150 #endif // jb_itch5_order_book_hpp
book_type::buys_t buy_
Definition: order_book.hpp:143
bool handle_order_reduced(buy_sell_indicator_t side, price4_t px, int reduced_qty)
Handle an order reduction, which includes executions, cancels and replaces.
Definition: order_book.hpp:134
half_quote worst_bid() const
Definition: order_book.hpp:78
unsigned long int book_depth_t
Number of prices on a side order book.
Definition: order_book.hpp:17
char_list_field< u 'S', u 'B'> buy_sell_indicator_t
Represent the &#39;Buy/Sell Indicator&#39; field on several messages.
bool handle_add_order(buy_sell_indicator_t side, price4_t px, int qty)
Handle a new order.
Definition: order_book.hpp:118
typename book_type::config book_type_config
Definition: order_book.hpp:59
std::pair< price4_t, int > half_quote
A simple representation for price + quantity.
order_book(book_type_config const &cfg)
Initialize an empty order book.
Definition: order_book.hpp:62
book_depth_t sell_count() const
Definition: order_book.hpp:95
half_quote best_bid() const
Definition: order_book.hpp:73
book_depth_t buy_count() const
Definition: order_book.hpp:92
half_quote worst_offer() const
Definition: order_book.hpp:88
Maintain the ITCH-5.0 order book for a single security.
Definition: order_book.hpp:57
half_quote best_offer() const
Definition: order_book.hpp:83
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7
book_depth_t get_book_depth() const
Definition: order_book.hpp:101
book_type::sells_t sell_
Definition: order_book.hpp:144