JayBeams  0.1
Another project to have fun coding.
map_based_order_book.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_map_based_order_book_hpp
2 #define jb_itch5_map_based_order_book_hpp
3 
6 #include <jb/feed_error.hpp>
7 #include <jb/log.hpp>
8 
9 #include <functional>
10 #include <map>
11 #include <utility>
12 
13 namespace jb {
14 namespace itch5 {
15 
16 template <typename compare_t>
18 
19 /**
20  * Define the types of buy and sell sides data structure.
21  *
22  * It is used as template parameter book_type of the
23  * template class order_book:
24  * - usage: jb::itch5::order_book<jb::itch5::map_based_order_book>
25  */
29  class config;
30 };
31 
32 /**
33  * Configure an map_based_order_book config object
34  */
36 public:
37  config() {
38  }
40  /// empty
41  void validate() const override {
42  }
43  /* no members */
44 };
45 
46 /**
47  * Represent one side of the book. Class implementation of struct
48  * map_based_order_book buy and side types.
49  * @tparam compare_t function object class type to sort the side
50  */
51 template <typename compare_t>
52 class map_based_book_side {
53 public:
54  /// Initializes an empty side order book
56  }
57 
58  /// @returns the best side price and quantity
60  if (levels_.empty()) {
62  }
63  auto i = levels_.begin();
64  return half_quote(i->first, i->second);
65  }
66 
67  /// @returns the worst side price and quantity
69  if (levels_.empty()) {
71  }
72  auto i = levels_.rbegin();
73  return half_quote(i->first, i->second);
74  }
75 
76  /// @returns the number of levels with non-zero quantity for the order side.
77  std::size_t count() const {
78  return levels_.size();
79  }
80 
81  /**
82  * Add a price and quantity to the side order book.
83  *
84  * @param px the price of the new order
85  * @param qty the quantity of the new order
86  * @returns true if the inside changed
87  */
88  bool add_order(price4_t px, int qty) {
89  auto emp_tup = levels_.emplace(px, 0);
90  emp_tup.first->second += qty;
91  return emp_tup.first == levels_.begin();
92  }
93 
94  /**
95  * Reduce the quantity for a given price
96  *
97  * @param px the price of the order that was reduced
98  * @param reduced_qty the quantity reduced in the order
99  * @returns true if the inside changed
100  */
101  bool reduce_order(price4_t px, int reduced_qty) {
102  auto price_it = levels_.find(px);
103  if (price_it == levels_.end()) {
104  throw jb::feed_error("trying to reduce a non-existing price level");
105  }
106  // ... reduce the quantity ...
107  price_it->second -= reduced_qty;
108  if (price_it->second < 0) {
109  // ... this is "Not Good[tm]", somehow we missed an order or
110  // processed a delete twice ...
111  JB_LOG(warning) << "negative quantity in order book";
112  }
113  // now we can erase this element (pf.first) if qty <=0
114  bool inside_change = (price_it == levels_.begin());
115  if (price_it->second <= 0) {
116  levels_.erase(price_it);
117  }
118  return inside_change;
119  }
120 
121  /**
122  * Testing hook.
123  * @returns true if side is in ascending order (BUY side)
124  * To discriminate different implementations for buy and sell sides
125  * during testing.
126  */
127  bool is_ascending() const {
129  }
130 
131  /// template specialization struct to handle differences between BUY and SELL
132  /// version SELL side
133  template <typename ordering, class DUMMY = void>
134  struct side {
135  static bool constexpr ascending = false;
136 
137  /// @returns an empty offer
139  return empty_offer();
140  }
141  };
142 
143  /// version BUY side
144  template <class DUMMY>
145  struct side<std::greater<price4_t>, DUMMY> {
146  static bool constexpr ascending = true;
147 
148  /// @returns an empty bid
150  return empty_bid();
151  }
152  };
153 
154 private:
155  std::map<price4_t, int, compare_t> levels_;
156 };
157 
158 } // namespace itch5
159 } // namespace jb
160 
161 #endif // jb_itch5_map_based_order_book_hpp
half_quote empty_bid()
The value used to represent an empty bid.
Base class for all configuration objects.
Communicate feed error exceptions.
Definition: feed_error.hpp:11
map_based_book_side(map_based_order_book::config const &cfg)
Initializes an empty side order book.
STL namespace.
Define the types of buy and sell sides data structure.
template specialization struct to handle differences between BUY and SELL version SELL side ...
bool add_order(price4_t px, int qty)
Add a price and quantity to the side order book.
bool is_ascending() const
Testing hook.
std::pair< price4_t, int > half_quote
A simple representation for price + quantity.
#define config_object_constructors(NAME)
Represent one side of the book.
bool reduce_order(price4_t px, int reduced_qty)
Reduce the quantity for a given price.
half_quote empty_offer()
The value used to represent an empty offer.
Configure an map_based_order_book config object.
#define JB_LOG(lvl)
Definition: log.hpp:70
std::map< price4_t, int, compare_t > levels_
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7