JayBeams  0.1
Another project to have fun coding.
ut_generate_inside.cpp
Go to the documentation of this file.
2 
3 #include <boost/test/unit_test.hpp>
4 
5 #include <sstream>
6 
7 using namespace jb::itch5;
8 
9 /**
10  * Helper functions and constants to test jb::itch5::generate_inside.
11  */
12 namespace {
13 buy_sell_indicator_t const BUY(u'B');
14 buy_sell_indicator_t const SELL(u'S');
15 
16 jb::itch5::message_header create_header(std::chrono::nanoseconds ns) {
19 }
20 } // anonymous namespace
21 
22 namespace jb {
23 namespace itch5 {
24 namespace testing {
25 /**
26  * Test generate_inside.
27  * @tparam based_order_book Type used by compute_book and order_book
28  */
29 template <typename based_order_book>
30 void test_generate_inside_basic(based_order_book& base) {
31  using book_type = order_book<based_order_book>;
32  using compute_type = compute_book<based_order_book>;
33  // ... create the preconditions to run a test, first some place to
34  // collect the statistics ...
36  // ... then a book with 3 orders on each side, widely spaced
37  typename based_order_book::config cfg;
38  book_type book(cfg);
39  book.handle_add_order(BUY, price4_t(10 * 10000), 300);
40  book.handle_add_order(BUY, price4_t(11 * 10000), 200);
41  book.handle_add_order(BUY, price4_t(12 * 10000), 100);
42  book.handle_add_order(SELL, price4_t(15 * 10000), 100);
43  book.handle_add_order(SELL, price4_t(16 * 10000), 200);
44  book.handle_add_order(SELL, price4_t(17 * 10000), 300);
45 
46  stock_t stock("HSART");
47  std::ostringstream out;
48 
49  // ... an update at the BBO produces no output ...
50  auto now = compute_type::clock_type::now();
51  typename compute_type::clock_type::duration pl(std::chrono::nanoseconds(525));
52  BOOST_CHECK_EQUAL(
53  false, generate_inside(
54  stats, out, create_header(std::chrono::nanoseconds(0)), book,
55  book_update{now, stock, BUY, price4_t(10 * 10000), 100}, pl));
56  BOOST_CHECK_EQUAL(std::string(""), out.str());
57  BOOST_CHECK_EQUAL(
58  false, generate_inside(
59  stats, out, create_header(std::chrono::nanoseconds(0)), book,
60  book_update{now, stock, SELL, price4_t(17 * 10000), 100}, pl));
61  BOOST_CHECK_EQUAL(std::string(""), out.str());
62 
63  // ... an order better than the BBO produces some output ...
64  out.str("");
65  now = compute_type::clock_type::now();
66  BOOST_CHECK_EQUAL(
67  true,
69  stats, out, create_header(std::chrono::nanoseconds(0)), book,
70  book_update{now, stock, BUY, price4_t(12 * 10000 + 5000), 100}, pl));
71  BOOST_CHECK_EQUAL(
72  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
73  out.str("");
74  BOOST_CHECK_EQUAL(
75  true,
77  stats, out, create_header(std::chrono::nanoseconds(0)), book,
78  book_update{now, stock, SELL, price4_t(15 * 10000 - 5000), 100}, pl));
79  BOOST_CHECK_EQUAL(
80  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
81 
82  // ... an order at the BBO produces some output ...
83  out.str("");
84  now = compute_type::clock_type::now();
85  BOOST_CHECK_EQUAL(
86  true, generate_inside(
87  stats, out, create_header(std::chrono::nanoseconds(0)), book,
88  book_update{now, stock, BUY, price4_t(12 * 10000), 100}, pl));
89  BOOST_CHECK_EQUAL(
90  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
91  out.str("");
92  BOOST_CHECK_EQUAL(
93  true, generate_inside(
94  stats, out, create_header(std::chrono::nanoseconds(0)), book,
95  book_update{now, stock, SELL, price4_t(15 * 10000), 100}, pl));
96  BOOST_CHECK_EQUAL(
97  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
98 
99  // ... an order moving from the BBO to outside the BBO produces some
100  // output ...
101  out.str("");
102  now = compute_type::clock_type::now();
103  BOOST_CHECK_EQUAL(
104  true, generate_inside(
105  stats, out, create_header(std::chrono::nanoseconds(0)), book,
106  book_update{now, stock, BUY, price4_t(11 * 10000), 100, true,
107  price4_t(12 * 10000), -100},
108  pl));
109  BOOST_CHECK_EQUAL(
110  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
111  out.str("");
112  BOOST_CHECK_EQUAL(
113  true, generate_inside(
114  stats, out, create_header(std::chrono::nanoseconds(0)), book,
115  book_update{now, stock, SELL, price4_t(16 * 10000), 100, true,
116  price4_t(15 * 10000), -100},
117  pl));
118  BOOST_CHECK_EQUAL(
119  std::string("0 0 HSART 120000 100 150000 100\n"), out.str());
120 }
121 
122 } // namespace testing
123 } // namespace itch5
124 } // namespace jb
125 
126 /**
127  * @test Verify that jb::itch5::generate_inside works as expected.
128  */
129 BOOST_AUTO_TEST_CASE(generate_inside_basic) {
132 }
static constexpr int message_type
Compute the book and call a user-defined callback on each change.
Define the header common to all ITCH 5.0 messages.
Contains classes and functions to parse NASDAQ ITCH-5.0 messages, more information about ITCH-5...
void test_generate_inside_basic(based_order_book &base)
Test generate_inside.
buy_sell_indicator_t const SELL(u 'S')
Keep statistics about a feed and its offline processor.
Define the types of buy and sell sides data structure.
Represent a ITCH-5.0 timestamp.
Definition: timestamp.hpp:17
A flat struct to represent updates to an order book.
buy_sell_indicator_t const BUY(u 'B')
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...
price_field< std::uint32_t, 10000 > price4_t
Convenience definition for Price(4) fields.
Maintain the ITCH-5.0 order book for a single security.
Definition: order_book.hpp:57
Configure an offline_feed_statistics object.
BOOST_AUTO_TEST_CASE(generate_inside_basic)
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7