ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
price.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <cstdint>
13#include <format>
14#include <string>
15#include <type_traits>
16
17namespace itch {
18
34template <typename RawType, unsigned int Decimals>
36 static_assert(std::is_unsigned_v<RawType>, "Price raw storage must be an unsigned integer");
37
38 public:
39 using raw_type = RawType;
40
42 static constexpr unsigned int decimals = Decimals;
43
45 constexpr BasicPrice() noexcept = default;
46
50 explicit constexpr BasicPrice(raw_type raw_value) noexcept : m_raw {raw_value} {}
51
55 [[nodiscard]] constexpr auto raw() const noexcept -> raw_type { return m_raw; }
56
61 [[nodiscard]] static constexpr auto divisor() noexcept -> raw_type {
62 raw_type result {1};
63 for (unsigned int exponent {0}; exponent < Decimals; ++exponent) {
64 result *= 10;
65 }
66 return result;
67 }
68
72 [[nodiscard]] constexpr auto to_double() const noexcept -> double {
73 return static_cast<double>(m_raw) / static_cast<double>(divisor());
74 }
75
79 [[nodiscard]] auto to_string() const -> std::string {
80 return std::format("{:.{}f}", to_double(), Decimals);
81 }
82
88 [[nodiscard]] friend constexpr auto operator==(BasicPrice, BasicPrice) noexcept -> bool =
89 default;
95 [[nodiscard]] friend constexpr auto operator<=>(BasicPrice, BasicPrice) noexcept = default;
96
97 private:
98 raw_type m_raw {0};
99};
100
103
106
111[[nodiscard]] constexpr auto make_price(std::uint32_t raw_value) noexcept -> StandardPrice {
112 return StandardPrice {raw_value};
113}
114
119[[nodiscard]] constexpr auto make_mwcb_price(std::uint64_t raw_value) noexcept -> MwcbPrice {
120 return MwcbPrice {raw_value};
121}
122
123} // namespace itch
124
126template <typename RawType, unsigned int Decimals>
127struct std::formatter<itch::BasicPrice<RawType, Decimals>> : std::formatter<double> {
134 auto format(itch::BasicPrice<RawType, Decimals> price, std::format_context& ctx) const {
135 return std::formatter<double>::format(price.to_double(), ctx);
136 }
137};
A strongly typed fixed-point price carrying its own decimal scale.
Definition price.hpp:35
static constexpr auto divisor() noexcept -> raw_type
The scale's divisor, i.e.
Definition price.hpp:61
RawType raw_type
Definition price.hpp:39
constexpr auto to_double() const noexcept -> double
The price as a floating-point value (raw / 10^Decimals).
Definition price.hpp:72
constexpr BasicPrice() noexcept=default
Constructs a zero-valued price at this scale.
constexpr auto raw() const noexcept -> raw_type
The underlying raw integer value, exactly as it appears on the wire.
Definition price.hpp:55
auto to_string() const -> std::string
The price as a fixed-precision decimal string.
Definition price.hpp:79
static constexpr unsigned int decimals
The number of implied decimal places for this price scale.
Definition price.hpp:42
friend constexpr auto operator<=>(BasicPrice, BasicPrice) noexcept=default
Orders two prices of the same scale by raw value.
friend constexpr auto operator==(BasicPrice, BasicPrice) noexcept -> bool=default
Compares two prices of the same scale for equality by raw value.
std::string price
Definition csv_sink.cpp:23
constexpr auto make_mwcb_price(std::uint64_t raw_value) noexcept -> MwcbPrice
Wraps a raw 8-decimal MWCB price value in the typed MwcbPrice.
Definition price.hpp:119
constexpr auto make_price(std::uint32_t raw_value) noexcept -> StandardPrice
Wraps a raw 4-decimal price value in the typed StandardPrice.
Definition price.hpp:111
auto format(itch::BasicPrice< RawType, Decimals > price, std::format_context &ctx) const
Formats price as a fixed-precision decimal string.
Definition price.hpp:134