JayBeams  0.1
Another project to have fun coding.
price_field.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_price_field_hpp
2 #define jb_itch5_price_field_hpp
3 
6 
7 #include <boost/io/ios_state.hpp>
8 #include <boost/operators.hpp>
9 
10 #include <iomanip>
11 #include <iostream>
12 #include <limits>
13 #include <ratio>
14 
15 namespace jb {
16 namespace itch5 {
17 
18 /**
19  * Define a class used to represent prices in the ITCH-5.0 feed.
20  *
21  * The ITCH-5.0 protocol represent prices as fixed-point values. The
22  * number of decimal digits is specified in the field definition. For
23  * example, a Price(4) field represents prices with 4 decimal digits,
24  * i.e., of the 99999.9999 form. On the wire the number is actually
25  * stored as an integer. For example, the price 150.0100 would be
26  * stored on the wire as 1500100.
27  *
28  * @tparam wire_type an integer type, typically std::uint32_t for
29  * 4-byte wide fields, or std::uint64_t for 8-byte wide fields.
30  * @tparam denom the denominator for this field, for example Price(4)
31  * would use denom==10000, while Price(8) would use 100000000.
32  */
33 template <typename wire_type_t, std::intmax_t denom_v>
35  : public boost::equality_comparable<price_field<wire_type_t, denom_v>>,
36  public boost::less_than_comparable<price_field<wire_type_t, denom_v>> {
37 public:
38  /// The wire type
39  typedef wire_type_t wire_type;
40 
41  /// The denominator
42  constexpr static std::intmax_t denom = denom_v;
43 
44  /// The number of digits in the denominator
45  constexpr static int denom_digits = static_digits(denom);
46 
47  /// The minimum quotation tick for this price
48  typedef std::ratio<1, denom> tick;
49 
50  /// Default constructor
52  : value_() {
53  }
54 
55  /// Constructor from a price
56  explicit price_field(wire_type rhs)
57  : value_(rhs) {
58  }
59 
60  /// Assignment from
61  price_field& operator=(wire_type rhs) {
62  value_ = rhs;
63  return *this;
64  }
65 
66  /// Addition assignment operator
68  value_ += rhs.value_;
69  return *this;
70  }
71 
72  //@{
73  /**
74  * @name Accessors
75  */
76  double as_double() const {
77  return value_ / double(denom);
78  }
79 
80  wire_type as_integer() const {
81  return value_;
82  }
83  //@}
84 
85  //@{
86  /**
87  * Base comparison operators
88  */
89  bool operator==(price_field const& rhs) const {
90  return value_ == rhs.value_;
91  }
92 
93  bool operator<(price_field const& rhs) const {
94  return value_ < rhs.value_;
95  }
96  //@}
97 
98  // Simple representation of $1.00
101  }
102 
103 private:
104  /// The value as an integer
105  wire_type value_;
106 };
107 
108 /// Specialize jb::itch5::decoder for jb::itch5::price_field
109 template <bool validate, typename wire_type_t, std::intmax_t denom_v>
110 struct decoder<validate, price_field<wire_type_t, denom_v>> {
111  /// Please see the generic documentation for jb::itch5::decoder<>::r()
113  r(std::size_t size, void const* buf, std::size_t offset) {
115  decoder<validate, wire_type_t>::r(size, buf, offset));
116  return tmp;
117  }
118 };
119 
120 /// Streaming operator for jb::itch5::price_field<>
121 template <typename wire_type_t, std::intmax_t denom_v>
122 std::ostream&
123 operator<<(std::ostream& os, price_field<wire_type_t, denom_v> const& x) {
124  boost::io::ios_flags_saver saver(os);
125  auto d = std::div(x.as_integer(), x.denom);
126  return os << d.quot << "." << std::setw(x.denom_digits - 1)
127  << std::setfill('0') << d.rem;
128 }
129 
130 /// Convenience definition for Price(4) fields.
132 
133 /// Convenience definition for Price(8) fields.
135 
136 template <typename price_field>
138  auto v = std::numeric_limits<typename price_field::wire_type>::max();
139  v /= price_field::denom;
140  return price_field(v * price_field::denom);
141 }
142 
143 template <>
144 inline price4_t max_price_field_value() {
145  // Per the ITCH-5.0 spec, the maximum value is 200,000.0000
146  return price4_t(std::uint32_t(200000) * std::uint32_t(price4_t::denom));
147 }
148 
149 /// non-member addition operator
150 template <typename price_field>
151 inline price_field operator+(price_field const& lhs, price_field const& rhs) {
152  price_field temp = lhs;
153  temp += rhs;
154  return temp;
155 }
156 
157 } // namespace itch5
158 } // namespace jb
159 
160 #endif // jb_itch5_price_field_hpp
price_field()
Default constructor.
Definition: price_field.hpp:51
constexpr int static_digits(std::intmax_t value)
Compute (at compile time if possible) the number of digits in a number.
Define a class used to represent prices in the ITCH-5.0 feed.
Definition: price_field.hpp:34
std::ratio< 1, denom > tick
The minimum quotation tick for this price.
Definition: price_field.hpp:48
price_field< std::uint64_t, 100000000 > price8_t
Convenience definition for Price(8) fields.
price_field(wire_type rhs)
Constructor from a price.
Definition: price_field.hpp:56
wire_type value_
The value as an integer.
static price_field dollar_price()
Definition: price_field.hpp:99
price_field operator+(price_field const &lhs, price_field const &rhs)
non-member addition operator
price_field max_price_field_value()
price_field & operator=(wire_type rhs)
Assignment from.
Definition: price_field.hpp:61
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24
wire_type as_integer() const
Definition: price_field.hpp:80
double as_double() const
Definition: price_field.hpp:76
price_field & operator+=(price_field const &rhs)
Addition assignment operator.
Definition: price_field.hpp:67
static constexpr int denom_digits
The number of digits in the denominator.
Definition: price_field.hpp:45
bool operator==(price_field const &rhs) const
Base comparison operators.
Definition: price_field.hpp:89
wire_type_t wire_type
The wire type.
Definition: price_field.hpp:39
static price_field< wire_type_t, denom_v > r(std::size_t size, void const *buf, std::size_t offset)
Please see the generic documentation for jb::itch5::decoder<>::r()
price_field< std::uint32_t, 10000 > price4_t
Convenience definition for Price(4) fields.
bool operator<(price_field const &rhs) const
Definition: price_field.hpp:93
static constexpr std::intmax_t denom
The denominator.
Definition: price_field.hpp:42
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7