JayBeams  0.1
Another project to have fun coding.
itch5trades.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * This program reads a raw ITCH-5.0 file and prints out the trade
5  * messages into an ASCII (though potentially compressed) file.
6  */
8 #include <jb/fileio.hpp>
9 #include <jb/log.hpp>
10 
11 #include <stdexcept>
12 
13 /**
14  * Define types and functions used in this program.
15  */
16 namespace {
17 
18 /// Configuration parameters for itch5inside
19 class config : public jb::config_object {
20 public:
21  config();
23 
24  void validate() const override;
25 
29 };
30 
31 /**
32  * Filter the jb::itch5::trade_message and print them to a std::ostream.
33  */
34 class trades_handler {
35 public:
36  //@{
37  /**
38  * @name Type traits
39  */
40  /// Define the clock used to measure processing delays
41  typedef std::chrono::steady_clock clock_type;
42 
43  /// A convenience typedef for clock_type::time_point
44  typedef typename clock_type::time_point time_point;
45  //@}
46 
47  /// Constructor, capture the output stream
48  explicit trades_handler(std::ostream& out)
49  : out_(out) {
50  }
51 
52  /**
53  * Handle a trade message, print it out to the output stream.
54  *
55  * @param header the header of the raw ITCH-5.0 message
56  * @param update a representation of the update just applied to the
57  * book
58  * @param msg the trade message
59  */
60  void handle_message(
61  time_point recvts, long msgcnt, std::size_t msgoffset,
62  jb::itch5::trade_message const& msg);
63 
64  /**
65  * Ignore all other message types.
66  *
67  * We are only interested in a handful of message types, anything
68  * else is captured by this template function and ignored.
69  *
70  * @tparam message_type the type of message to ignore
71  */
72  template <typename message_type>
73  void handle_message(time_point, long, std::size_t, message_type const&) {
74  }
75 
76  /**
77  * Log any unknown message types.
78  *
79  * @param recvts the timestamp when the message was received
80  * @param msg the unknown message location and contents
81  */
82  void handle_unknown(time_point recvts, jb::itch5::unknown_message const& msg);
83 
84  /// Return the current timestamp for delay measurements
85  time_point now() const {
86  return std::chrono::steady_clock::now();
87  }
88 
89 private:
90  std::ostream& out_;
91 };
92 
93 } // anonymous namespace
94 
95 int main(int argc, char* argv[]) try {
96  config cfg;
97  cfg.load_overrides(argc, argv, std::string("itch5trades.yaml"), "JB_ROOT");
98  jb::log::init(cfg.log());
99 
100  boost::iostreams::filtering_istream in;
101  jb::open_input_file(in, cfg.input_file());
102 
103  boost::iostreams::filtering_ostream out;
104  jb::open_output_file(out, cfg.output_file());
105 
106  trades_handler handler(out);
107  jb::itch5::process_iostream(in, handler);
108 
109  return 0;
110 } catch (jb::usage const& u) {
111  std::cerr << u.what() << std::endl;
112  return u.exit_status();
113 } catch (std::exception const& ex) {
114  std::cerr << "Standard exception raised: " << ex.what() << std::endl;
115  return 1;
116 } catch (...) {
117  std::cerr << "Unknown exception raised" << std::endl;
118  return 1;
119 }
120 
121 namespace {
122 
123 config::config()
124  : input_file(
125  desc("input-file").help("An input file with ITCH-5.0 messages."),
126  this)
127  , output_file(
128  desc("output-file")
129  .help("The name of the file where to store the inside data."
130  " Files ending in .gz are automatically compressed."),
131  this, "stdout")
132  , log(desc("log", "logging"), this) {
133 }
134 
135 void config::validate() const {
136  if (input_file() == "") {
137  throw jb::usage(
138  "Missing input-file setting."
139  " You must specify an input file.",
140  1);
141  }
142  if (output_file() == "") {
143  throw jb::usage(
144  "Missing output-file setting."
145  " You must specify an output file.",
146  1);
147  }
148  log().validate();
149 }
150 
151 void trades_handler::handle_message(
152  time_point, long, std::size_t, jb::itch5::trade_message const& msg) {
153  out_ << msg.header.timestamp.ts.count() << " " << msg.order_reference_number
154  << " " << static_cast<char>(msg.buy_sell_indicator.as_int()) << " "
155  << msg.shares << " " << msg.stock << " " << msg.price << " "
156  << msg.match_number << "\n";
157 }
158 
159 void trades_handler::handle_unknown(
160  time_point recvts, jb::itch5::unknown_message const& msg) {
161  char msgtype = *static_cast<char const*>(msg.buf());
162  JB_LOG(error) << "Unknown message type '" << msgtype << "'(" << int(msgtype)
163  << ") in msgcnt=" << msg.count()
164  << ", msgoffset=" << msg.offset();
165 }
166 
167 } // anonymous namespace
clock_type::time_point time_point
A convenience alias for clock_type::time_point.
Base class for all configuration objects.
virtual void validate() const
Validate the settings.
int exit_status() const
Definition: usage.hpp:21
void open_output_file(boost::iostreams::filtering_ostream &out, std::string const &filename)
Open a file for writing.
Definition: fileio.cpp:12
void process_iostream(std::istream &in, message_handler &handler)
Process an iostream of ITCH-5.0 messages.
std::chrono::steady_clock clock_type
A convenience alias for clock_type.
void init(config const &cfg)
Initialize the logging functions using the configuration provided.
Definition: log.cpp:190
#define config_object_constructors(NAME)
Helper class to easily define configuration attributes.
std::uint64_t order_reference_number
Represent an &#39;Trade (non-Cross)&#39; message in the ITCH-5.0 protocol.
std::chrono::nanoseconds ts
Definition: timestamp.hpp:18
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
void open_input_file(boost::iostreams::filtering_istream &in, std::string const &filename)
Open a file for reading.
Definition: fileio.cpp:27
buy_sell_indicator_t buy_sell_indicator
#define JB_LOG(lvl)
Definition: log.hpp:70
int main(int argc, char *argv[])
Definition: itch5trades.cpp:95
jb::itch5::timestamp timestamp
The message timestamp, in nanoseconds since midnight.
void const * buf() const
int as_int() const
Return the integer value.
std::uint32_t count() const
std::uint64_t offset() const