JayBeams  0.1
Another project to have fun coding.
generate_inside.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_generate_inside_hpp
2 #define jb_itch5_generate_inside_hpp
3 
6 
7 #include <iostream>
8 
9 namespace jb {
10 namespace itch5 {
11 
12 /**
13  * Determine if this event changes the inside, if so, record the
14  * statistics.
15  *
16  * @tparam duration_t the type used to record the processing latency,
17  * must be compatible with a duration in the std::chrono sense.
18  * @tparam book_type the type used to define the book type,
19  * order_book<book_type>, must be compatible with jb::itch5::map_price.
20  *
21  * @param stats where to record the statistics
22  * @param header (unused) the header for the message that generated
23  * this book update
24  * @param book the book updated
25  * @param update a report of the changes to the book
26  * @param processing_latency the time it took to process the event
27  * (before the any output is generated).
28  * @returns true if the inside is affected by the change, false otherwise.
29  */
30 template <typename duration_t, typename book_type>
34  jb::itch5::book_update const& update, duration_t processing_latency) {
35  // ... we need to treat each side differently ...
36  if (update.buy_sell_indicator == u'B') {
37  // ... if the update price (or the old price for a cancel/replace
38  // is equal or better than the current best bid, that means the
39  // best bid changed. Notice that this works even when there is no
40  // best bid, because the book returns "0" as a best bid in that
41  // case ...
42  if ((not update.cxlreplx and update.px >= book.best_bid().first) or
43  (update.cxlreplx and update.oldpx >= book.best_bid().first)) {
44  stats.sample(header.timestamp.ts, processing_latency);
45  return true;
46  }
47  return false;
48  }
49  // ... do the analogous thing for the sell side ...
50  if ((not update.cxlreplx and update.px <= book.best_offer().first) or
51  (update.cxlreplx and update.oldpx <= book.best_offer().first)) {
52  stats.sample(header.timestamp.ts, processing_latency);
53  return true;
54  }
55  return false;
56 }
57 
58 /**
59  * Determine if this event changes the inside, if so, record the
60  * statistics and output the result.
61  *
62  * @tparam duration_t the type used to record the processing latency,
63  * must be compatible with a duration in the std::chrono sense.
64  * @tparam book_type the type used to define order_book<book_type>,
65  * must be compatible with jb::itch5::map_price
66  *
67  * @param stats where to record the statistics
68  * @param out where to send the new inside quote if needed
69  * @param header (unused) the header for the message that generated
70  * this book update
71  * @param book the book updated
72  * @param update a report of the changes to the book
73  * @param processing_latency the time it took to process the event
74  * (before the any output is generated).
75  * @returns true if the inside is affected by the change, false otherwise.
76  */
77 template <typename duration_t, typename book_type>
79  jb::offline_feed_statistics& stats, std::ostream& out,
80  jb::itch5::message_header const& header,
82  jb::itch5::book_update const& update, duration_t processing_latency) {
83  if (not record_latency_stats(
84  stats, header, book, update, processing_latency)) {
85  return false;
86  }
87  auto bid = book.best_bid();
88  auto offer = book.best_offer();
89  out << header.timestamp.ts.count() << " " << header.stock_locate << " "
90  << update.stock << " " << bid.first.as_integer() << " " << bid.second
91  << " " << offer.first.as_integer() << " " << offer.second << "\n";
92  return true;
93 }
94 
95 } // namespace itch5
96 } // namespace jb
97 
98 #endif // jb_itch5_generate_inside_hpp
Define the header common to all ITCH 5.0 messages.
bool record_latency_stats(jb::offline_feed_statistics &stats, jb::itch5::message_header const &header, jb::itch5::order_book< book_type > const &book, jb::itch5::book_update const &update, duration_t processing_latency)
Determine if this event changes the inside, if so, record the statistics.
Keep statistics about a feed and its offline processor.
void sample(event_timestamp_t ts, duration_t processing_latency)
Record a sample, that is process a message received at the given timestamp.
bool cxlreplx
If true, this was a cancel replace and and old order was modified too...
int stock_locate
The stock locate number.
half_quote best_bid() const
Definition: order_book.hpp:73
std::chrono::nanoseconds ts
Definition: timestamp.hpp:18
buy_sell_indicator_t buy_sell_indicator
What side of the book is being updated.
stock_t stock
The security updated by this order.
A flat struct to represent updates to an order book.
bool generate_inside(jb::offline_feed_statistics &stats, std::ostream &out, jb::itch5::message_header const &header, jb::itch5::order_book< book_type > const &book, jb::itch5::book_update const &update, duration_t processing_latency)
Determine if this event changes the inside, if so, record the statistics and output the result...
price4_t oldpx
Old price for the order.
Maintain the ITCH-5.0 order book for a single security.
Definition: order_book.hpp:57
jb::itch5::timestamp timestamp
The message timestamp, in nanoseconds since midnight.
half_quote best_offer() const
Definition: order_book.hpp:83
price4_t px
What price level is being updated.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7