Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <limits>
4#include <cstdint>
5#include <string>
6#include <string_view>
7#include <boost/multiprecision/cpp_int.hpp>
8
9// Bracket wire structs that end in a `T name[0]` payload. Zero-size arrays are an
10// extension: this quiets GCC/clang -Wpedantic for the block, MSVC's C4200 is off in CMake.
11#if defined(__GNUC__)
12#define SLICK_SIM_ZERO_SIZE_ARRAY_BEGIN _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wpedantic\"")
13#define SLICK_SIM_ZERO_SIZE_ARRAY_END _Pragma("GCC diagnostic pop")
14#else
15#define SLICK_SIM_ZERO_SIZE_ARRAY_BEGIN
16#define SLICK_SIM_ZERO_SIZE_ARRAY_END
17#endif
18
19namespace slick::sim {
20
21using price_t = int_fast64_t;
22using qty_t = int_fast64_t;
23using time_t = uint_fast64_t;
24using symid_t = uint16_t;
25using cum_value_t = boost::multiprecision::uint128_t;
26
27constexpr symid_t INVALID_SYMBOL_ID = std::numeric_limits<symid_t>::max();
28constexpr price_t NULL_PRICE = std::numeric_limits<price_t>::max();
29constexpr int32_t DOUBLE_MULTIPLIER = static_cast<int32_t>(1e8);
30constexpr double EPSILON = 1e-8;
31
42inline constexpr price_t to_price_t(double price) {
43 return static_cast<price_t>(price * DOUBLE_MULTIPLIER + (price < 0 ? -0.5 : 0.5));
44}
45
46inline constexpr double to_price_double(price_t price) {
47 return static_cast<double>(price) / DOUBLE_MULTIPLIER;
48}
49
50inline constexpr qty_t to_qty_t(double qty) {
51 return static_cast<qty_t>(qty * DOUBLE_MULTIPLIER + (qty < 0 ? -0.5 : 0.5));
52}
53
54inline constexpr double to_qty_double(qty_t qty) {
55 return static_cast<double>(qty) / DOUBLE_MULTIPLIER;
56}
57
72inline std::string to_fixed_string(int_fast64_t value, unsigned min_frac_digits = 0) {
73 const bool negative = value < 0;
74 // Negated through the unsigned domain so the most negative value cannot overflow.
75 const uint64_t magnitude = negative
76 ? (0ULL - static_cast<uint64_t>(value))
77 : static_cast<uint64_t>(value);
78
79 const uint64_t scale = static_cast<uint64_t>(DOUBLE_MULTIPLIER);
80 uint64_t fraction = magnitude % scale;
81
82 std::string out;
83 if (negative) {
84 out.push_back('-');
85 }
86 out += std::to_string(magnitude / scale);
87
88 if (fraction == 0 && min_frac_digits == 0) {
89 return out;
90 }
91
92 char digits[8];
93 for (int i = 7; i >= 0; --i) {
94 digits[i] = static_cast<char>('0' + fraction % 10);
95 fraction /= 10;
96 }
97 std::string_view significant(digits, 8);
98 while (significant.size() > min_frac_digits && significant.back() == '0') {
99 significant.remove_suffix(1);
100 }
101
102 out.push_back('.');
103 out.append(significant);
104 return out;
105}
106
107inline std::string to_price_string(price_t price) { return to_fixed_string(price); }
108inline std::string to_qty_string(qty_t qty) { return to_fixed_string(qty); }
109
110namespace detail {
111
117inline bool iequals(std::string_view a, std::string_view b) noexcept {
118 if (a.size() != b.size()) {
119 return false;
120 }
121 for (size_t i = 0; i < a.size(); ++i) {
122 char ca = a[i];
123 char cb = b[i];
124 if (ca >= 'a' && ca <= 'z') { ca = static_cast<char>(ca - ('a' - 'A')); }
125 if (cb >= 'a' && cb <= 'z') { cb = static_cast<char>(cb - ('a' - 'A')); }
126 if (ca != cb) {
127 return false;
128 }
129 }
130 return true;
131}
132
133} // end namespace detail
134
148#define SLICK_SIM_VENUE_LIST(X) \
149 X(CME, "CME") \
150 X(ICE, "ICE") \
151 X(COINBASE, "COINBASE") \
152 X(HYPERLIQUID, "HYPERLIQUID") \
153 X(STOCK, "STOCK")
154
155enum Venue : uint8_t {
157#define SLICK_SIM_VENUE_ENUMERATOR(name, text) name,
159#undef SLICK_SIM_VENUE_ENUMERATOR
160 __COUNT__, // for internal use only
161};
162
163inline const char* to_string(Venue venue) {
164 switch(venue) {
165#define SLICK_SIM_VENUE_CASE(name, text) case Venue::name: return text;
167#undef SLICK_SIM_VENUE_CASE
168 case Venue::__COUNT__:
170 break;
171 }
172 return "UNKNOWN";
173}
174
177inline Venue to_venue(std::string_view exch) {
178#define SLICK_SIM_VENUE_MATCH(name, text) \
179 if (detail::iequals(exch, text)) { return Venue::name; }
181#undef SLICK_SIM_VENUE_MATCH
183}
184
185} // end namespace slick::sim
bool iequals(std::string_view a, std::string_view b) noexcept
ASCII case-insensitive compare, used for every venue-name lookup.
Definition types.hpp:117
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
std::string to_fixed_string(int_fast64_t value, unsigned min_frac_digits=0)
Renders a fixed-point value as an exact decimal string.
Definition types.hpp:72
constexpr price_t NULL_PRICE
Definition types.hpp:28
uint_fast64_t time_t
Definition types.hpp:23
@ UNKNOWN_VENUE
Definition types.hpp:156
constexpr int32_t DOUBLE_MULTIPLIER
Definition types.hpp:29
constexpr symid_t INVALID_SYMBOL_ID
Definition types.hpp:27
constexpr double to_price_double(price_t price)
Definition types.hpp:46
Venue to_venue(std::string_view exch)
Matches venue names case-insensitively, including mixed-case names such as "Coinbase".
Definition types.hpp:177
constexpr price_t to_price_t(double price)
Scales a decimal into the fixed-point representation, rounding to the nearest tick rather than trunca...
Definition types.hpp:42
uint16_t symid_t
Definition types.hpp:24
boost::multiprecision::uint128_t cum_value_t
Definition types.hpp:25
constexpr const char * to_string(FeedState state) noexcept
std::string to_price_string(price_t price)
Definition types.hpp:107
int_fast64_t qty_t
Definition types.hpp:22
std::string to_qty_string(qty_t qty)
Definition types.hpp:108
constexpr qty_t to_qty_t(double qty)
Definition types.hpp:50
constexpr double to_qty_double(qty_t qty)
Definition types.hpp:54
constexpr double EPSILON
Definition types.hpp:30
int_fast64_t price_t
Definition types.hpp:21
#define SLICK_SIM_VENUE_ENUMERATOR(name, text)
Definition types.hpp:157
#define SLICK_SIM_VENUE_MATCH(name, text)
#define SLICK_SIM_VENUE_CASE(name, text)
#define SLICK_SIM_VENUE_LIST(X)
One line per venue: the enumerator, then the canonical name to_string returns and to_venue accepts.
Definition types.hpp:148