JayBeams  0.1
Another project to have fun coding.
data.cpp
Go to the documentation of this file.
2 
4 #include <jb/itch5/timestamp.hpp>
5 
6 #include <sstream>
7 #include <stdexcept>
8 
9 namespace jb {
10 namespace itch5 {
11 namespace testing {
12 
13 /// A convenient sequence of bytes to test messages.
14 #define JB_ITCH5_TEST_HEADER \
15  "\x00\x00" /* Stock Locate (0) */ \
16  "\x00\x01" /* Tracking Number (1) */ \
17  "\x25\xCA\x5F\xF4\x23\x15" /* Timestamp (11:32:30.123456789) */
18 
19 std::chrono::nanoseconds expected_ts() {
20  using namespace std::chrono;
21  return duration_cast<nanoseconds>(
22  hours(11) + minutes(32) + seconds(31) + nanoseconds(123456789L));
23 }
24 
25 std::pair<char const*, std::size_t> message_header() {
26  static char const buf[] = u8" " // Message Type (space is invalid)
27  JB_ITCH5_TEST_HEADER // Common header body
28  ;
29  std::size_t const bufsize = sizeof(buf) - 1;
30  return std::make_pair(buf, bufsize);
31 }
32 
33 std::pair<char const*, std::size_t> add_order() {
34  static char const buf[] = u8"A" // Message Type
35  JB_ITCH5_TEST_HEADER // Common test header
36  "\x00\x00\x00\x00"
37  "\x00\x00\x00\x2A" // Order Reference Number (42)
38  "B" // Buy/Sell Indicator
39  "\x00\x00\x00\x64" // Shares (100)
40  "HSART " // Stock
41  "\x00\x12\xC6\xA4" // Price (123.0500)
42  ;
43  std::size_t const bufsize = sizeof(buf) - 1;
44  return std::make_pair(buf, bufsize);
45 }
46 
47 std::pair<char const*, std::size_t> add_order_mpid() {
48  static char const buf[] = u8"F" // Message Type
49  JB_ITCH5_TEST_HEADER // Common test header
50  "\x00\x00\x00\x00"
51  "\x00\x00\x00\x2A" // Order Reference Number (42)
52  "B" // Buy/Sell Indicator
53  "\x00\x00\x00\x64" // Shares (100)
54  "HSART " // Stock
55  "\x00\x12\xC6\xA4" // Price (123.0500)
56  "LOOF" // MPID
57  ;
58  std::size_t const bufsize = sizeof(buf) - 1;
59  return std::make_pair(buf, bufsize);
60 }
61 
62 std::pair<char const*, std::size_t> broken_trade() {
63  static char const buf[] = u8"B" // Message Type
64  JB_ITCH5_TEST_HEADER // Common test header
65  "\x00\x00\x00\x00"
66  "\x00\x23\xB6\xF8" // Match Number (2340600)
67  ;
68  std::size_t const bufsize = sizeof(buf) - 1;
69  return std::make_pair(buf, bufsize);
70 }
71 
72 std::pair<char const*, std::size_t> cross_trade() {
73  static char const buf[] = u8"Q" // Message Type
74  JB_ITCH5_TEST_HEADER // Common test header
75  "\x00\x00\x00\x00"
76  "\x00\x06\x79\x08" // Shares (424200)
77  "HSART " // Stock
78  "\x00\x12\xC6\xA4" // Cross Price (123.0500)
79  "\x00\x00\x00\x00"
80  "\x00\x23\xB6\xF8" // Match Number (2340600)
81  "H" // Cross Type (H)
82  ;
83  std::size_t const bufsize = sizeof(buf) - 1;
84  return std::make_pair(buf, bufsize);
85 }
86 
87 std::pair<char const*, std::size_t> ipo_quoting_period_update() {
88  static char const buf[] =
89  u8"K" // Message Type
90  JB_ITCH5_TEST_HEADER // Common test header
91  "HSART " // Stock
92  "\x00\x00\xC0\xFD" // IPO Quotation Release Time (13:43:25)
93  "A" // IPO Quotation Release Qualifier
94  "\x00\x12\xC6\xA4" // IPO Price (123.0500)
95  ;
96  std::size_t const bufsize = sizeof(buf) - 1;
97  return std::make_pair(buf, bufsize);
98 }
99 
100 std::pair<char const*, std::size_t> market_participant_position() {
101  static char const buf[] = u8"L" // Message Type
102  JB_ITCH5_TEST_HEADER // Common test header
103  "LOOF" // MPID
104  "HSART " // Stock
105  "N" // Primary Market Maker
106  "N" // Market Maker Mode
107  "A" // Market Participant State
108  ;
109  std::size_t const bufsize = sizeof(buf) - 1;
110  return std::make_pair(buf, bufsize);
111 }
112 
113 std::pair<char const*, std::size_t> mwcb_breach() {
114  static char const buf[] = u8"W" // Message Type
115  JB_ITCH5_TEST_HEADER // Common test header
116  "2" // Breached Level
117  ;
118  std::size_t const bufsize = sizeof(buf) - 1;
119  return std::make_pair(buf, bufsize);
120 }
121 
122 std::pair<char const*, std::size_t> mwcb_decline_level() {
123  static char const buf[] =
124  u8"V" // Message Type
125  JB_ITCH5_TEST_HEADER // Common test header
126  "\x00\x00\x00\x74\x6A\x61\xCA\x40" // Level 1 (5000.01)
127  "\x00\x00\x00\x5D\x21\xEB\x30\x60" // Level 2 (4000.0102)
128  "\x00\x00\x00\x45\xD9\x74\x49\x8C" // Level 3 (3000.010203)
129  ;
130  std::size_t const bufsize = sizeof(buf) - 1;
131  return std::make_pair(buf, bufsize);
132 }
133 
134 std::pair<char const*, std::size_t> net_order_imbalance_indicator() {
135  static char const buf[] =
136  u8"I" // Message Type
137  JB_ITCH5_TEST_HEADER // Common test header
138  "\x00\x00\x00\x00"
139  "\x02\x80\xDE\x80" // Paired Shares (42000000)
140  "\x00\x00\x00\x00"
141  "\x00\x06\x79\x08" // Imbalance Shares (424200)
142  "B" // Imbalance Direction (H)
143  "HSART " // Stock
144  "\x00\x23\xB6\xF8" // Far Price (234.0600)
145  "\x00\x12\xC6\xA4" // Near Price (123.0500)
146  "\x00\x0D\x94\xF4" // Current Reference Price (89.0100)
147  "O" // Cross Type (O)
148  "A" // Price Variation Indicator
149  ;
150  std::size_t const bufsize = sizeof(buf) - 1;
151  return std::make_pair(buf, bufsize);
152 }
153 
154 std::pair<char const*, std::size_t> order_cancel() {
155  static char const buf[] = u8"X" // Message Type
156  JB_ITCH5_TEST_HEADER // Common test header
157  "\x00\x00\x00\x00"
158  "\x00\x00\x00\x2A" // Order Reference Number (42)
159  "\x00\x00\x01\x2C" // Canceled Shares (300)
160  ;
161  std::size_t const bufsize = sizeof(buf) - 1;
162  return std::make_pair(buf, bufsize);
163 }
164 
165 std::pair<char const*, std::size_t> order_delete() {
166  static char const buf[] = u8"D" // Message Type
167  JB_ITCH5_TEST_HEADER // Common test header
168  "\x00\x00\x00\x00"
169  "\x00\x00\x00\x2A" // Order Reference Number (42)
170  ;
171  std::size_t const bufsize = sizeof(buf) - 1;
172  return std::make_pair(buf, bufsize);
173 }
174 
175 std::pair<char const*, std::size_t> order_executed() {
176  static char const buf[] = u8"E" // Message Type
177  JB_ITCH5_TEST_HEADER // Common test header
178  "\x00\x00\x00\x00"
179  "\x00\x00\x00\x2A" // Order Reference Number (42)
180  "\x00\x00\x01\x2C" // Executed Shares (300)
181  "\x00\x00\x00\x00"
182  "\x00\x00\x01\x3D" // Match Number (317)
183  ;
184  std::size_t const bufsize = sizeof(buf) - 1;
185  return std::make_pair(buf, bufsize);
186 }
187 
188 std::pair<char const*, std::size_t> order_executed_price() {
189  static char const buf[] = u8"C" // Message Type
190  JB_ITCH5_TEST_HEADER // Common test header
191  "\x00\x00\x00\x00"
192  "\x00\x00\x00\x2A" // Order Reference Number (42)
193  "\x00\x00\x01\x2C" // Executed Shares (300)
194  "\x00\x00\x00\x00"
195  "\x00\x00\x01\x3D" // Match Number (317)
196  "Y" // Printable (Y)
197  "\x00\x12\xC6\xA4" // Execution Price (123.0500)
198  ;
199  std::size_t const bufsize = sizeof(buf) - 1;
200  return std::make_pair(buf, bufsize);
201 }
202 
203 std::pair<char const*, std::size_t> order_replace() {
204  static char const buf[] =
205  u8"U" // Message Type
206  JB_ITCH5_TEST_HEADER // Common test header
207  "\x00\x00\x00\x00"
208  "\x00\x00\x00\x2A" // Original Order Reference Number (42)
209  "\x00\x00\x00\x00"
210  "\x00\x00\x10\x92" // New Order Reference Number (4242)
211  "\x00\x00\x00\x64" // Shares (100)
212  "\x00\x23\xB6\xF8" // Price (234.0600)
213  ;
214  std::size_t const bufsize = sizeof(buf) - 1;
215  return std::make_pair(buf, bufsize);
216 }
217 
218 std::pair<char const*, std::size_t> reg_sho_restriction() {
219  static char const buf[] = u8"Y" // Message Type
220  JB_ITCH5_TEST_HEADER // Common test header
221  "HSART " // Stock
222  "0" // Reg SHO Action
223  ;
224  std::size_t const bufsize = sizeof(buf) - 1;
225  return std::make_pair(buf, bufsize);
226 }
227 
228 std::pair<char const*, std::size_t> stock_directory() {
229  static char const buf[] = u8"R" // Message Type
230  JB_ITCH5_TEST_HEADER // Common test header
231  "HSART " // Stock
232  "G" // Market Category
233  "N" // Financial Status Indicator
234  "\x00\x00\x00\x64" // Round Lot Size
235  "N" // Round Lots Only
236  "O" // Issue Classification
237  "C " // Issue Sub-Type
238  "P" // Authenticity
239  "N" // Short Sale Threshold Indicator
240  "N" // IPO Flag
241  "1" // LULD Reference Price Tier
242  "N" // ETP Flag
243  "\x00\x00\x00\x00" // ETP Leverage Factor
244  "N" // Inverse Indicator
245  ;
246  std::size_t const bufsize = sizeof(buf) - 1;
247  return std::make_pair(buf, bufsize);
248 }
249 
250 std::pair<char const*, std::size_t> stock_trading_action() {
251  static char const buf[] = u8"H" // Message Type
252  JB_ITCH5_TEST_HEADER // Common test header
253  "HSART " // Stock
254  "T" // Trading State
255  "\x00" // Reserved
256  "MWC1" // Reason
257  ;
258  std::size_t const bufsize = sizeof(buf) - 1;
259  return std::make_pair(buf, bufsize);
260 }
261 
262 std::pair<char const*, std::size_t> system_event() {
263  static char buf[] = u8"S" // Message Type
264  JB_ITCH5_TEST_HEADER // Common test header
265  "O" // Event Code ("O" -> Start)
266  ;
267  std::size_t const size = sizeof(buf) / sizeof(buf[0]);
268  return std::make_pair(buf, size);
269 }
270 
271 std::pair<char const*, std::size_t> trade() {
272  static char const buf[] = u8"P" // Message Type
273  JB_ITCH5_TEST_HEADER // Common test header
274  "\x00\x00\x00\x00"
275  "\x00\x00\x10\x92" // Order Reference Number (4242)
276  "B" // Buy/Sell Indicator
277  "\x00\x00\x00\x64" // Shares (100)
278  "HSART " // Stock
279  "\x00\x12\xC6\xA4" // Price (123.0500)
280  "\x00\x00\x00\x00"
281  "\x00\x23\xB6\xF8" // Match Number (2340600)
282  ;
283  std::size_t const bufsize = sizeof(buf) - 1;
284  return std::make_pair(buf, bufsize);
285 }
286 
287 std::vector<char> create_message(
288  int message_type, jb::itch5::timestamp ts, std::size_t total_size) {
289  if (total_size < protocol::header_size or
290  total_size > protocol::max_message_size) {
291  std::ostringstream os;
292  os << "ITCH-5.x messages size in bytes must be in the ["
294  << "] range";
295  throw std::range_error(os.str());
296  }
297 
298  std::vector<char> msg(total_size);
299  if (message_type < 0 or message_type > 255) {
300  std::ostringstream os;
301  os << "out of range message type <" << message_type
302  << "> valid range is [0,255]";
303  throw std::range_error(os.str());
304  }
305  void* buf = &msg[0];
306  // message type
307  jb::itch5::encoder<true, std::uint8_t>::w(total_size, buf, 0, message_type);
308  // stock locate
309  jb::itch5::encoder<true, std::uint16_t>::w(total_size, buf, 1, 0);
310  // tracking number
311  jb::itch5::encoder<true, std::uint16_t>::w(total_size, buf, 3, 0);
312  // timestamp
314  return msg;
315 }
316 
317 } // namespace testing
318 } // namespace itch5
319 } // namespace jb
std::pair< char const *, std::size_t > order_replace()
Definition: data.cpp:203
std::pair< char const *, std::size_t > order_executed()
Definition: data.cpp:175
static void w(std::size_t size, void *msg, std::size_t offset, T const &x)
Write a single message or field to a buffer.
std::pair< char const *, std::size_t > add_order_mpid()
Definition: data.cpp:47
std::pair< char const *, std::size_t > add_order()
Definition: data.cpp:33
std::pair< char const *, std::size_t > mwcb_breach()
Definition: data.cpp:113
std::pair< char const *, std::size_t > system_event()
Definition: data.cpp:262
std::pair< char const *, std::size_t > order_cancel()
Definition: data.cpp:154
constexpr std::size_t max_message_size
The maximum size for a ITCH-5.x message (the length is a 16-bit integer)
std::pair< char const *, std::size_t > market_participant_position()
Definition: data.cpp:100
std::pair< char const *, std::size_t > mwcb_decline_level()
Definition: data.cpp:122
#define JB_ITCH5_TEST_HEADER
A convenient sequence of bytes to test messages.
Definition: data.cpp:14
std::pair< char const *, std::size_t > message_header()
Definition: data.cpp:25
std::pair< char const *, std::size_t > net_order_imbalance_indicator()
Definition: data.cpp:134
std::pair< char const *, std::size_t > broken_trade()
Definition: data.cpp:62
Represent a ITCH-5.0 timestamp.
Definition: timestamp.hpp:17
std::pair< char const *, std::size_t > stock_directory()
Definition: data.cpp:228
std::vector< char > create_message(int message_type, jb::itch5::timestamp ts, std::size_t total_size)
Generate test messages with a more-or-less valid header.
Definition: data.cpp:287
std::chrono::nanoseconds expected_ts()
Return the expected timestamp for all the test messages.
Definition: data.cpp:19
std::pair< char const *, std::size_t > order_executed_price()
Definition: data.cpp:188
std::pair< char const *, std::size_t > reg_sho_restriction()
Definition: data.cpp:218
std::pair< char const *, std::size_t > ipo_quoting_period_update()
Definition: data.cpp:87
std::pair< char const *, std::size_t > order_delete()
Definition: data.cpp:165
std::pair< char const *, std::size_t > stock_trading_action()
Definition: data.cpp:250
constexpr std::size_t header_size
The total size of the ITCH-5.x header.
std::pair< char const *, std::size_t > cross_trade()
Definition: data.cpp:72
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7
std::pair< char const *, std::size_t > trade()
Definition: data.cpp:271