diff --git a/src/lib/core/TimeStamp.h b/src/lib/core/TimeStamp.h new file mode 100644 index 000000000..c139c1646 --- /dev/null +++ b/src/lib/core/TimeStamp.h @@ -0,0 +1,27 @@ +#pragma once +#include + +namespace bts::core { + +// I can send this whole class later but its a nice wrapper around the linux +// posix time +struct TimeStamp { + public: + constexpr TimeStamp() = default; + explicit constexpr TimeStamp(std::uint64_t value); + constexpr std::uint64_t nanos() const; + + private: + // posix time + std::uint64_t value_{}; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr TimeStamp::TimeStamp(std::uint64_t value) : value_(value) {} + +constexpr std::uint64_t TimeStamp::nanos() const { return value_; } + +} // namespace bts::core diff --git a/src/lib/core/ValueWrapper.h b/src/lib/core/ValueWrapper.h new file mode 100644 index 000000000..ef776972b --- /dev/null +++ b/src/lib/core/ValueWrapper.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" + +namespace bts::core { + +template +using ValueWrapper = utils::ValueWrapper; + +} // namespace bts::core diff --git a/src/lib/domain/AccountID.h b/src/lib/domain/AccountID.h new file mode 100644 index 000000000..b5d412a77 --- /dev/null +++ b/src/lib/domain/AccountID.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +#include + +namespace bts::domain { + +using AccountID = utils::ValueWrapper; + +} // namespace bts::domain diff --git a/src/lib/domain/AggressorSide.cpp b/src/lib/domain/AggressorSide.cpp new file mode 100644 index 000000000..582d93f3b --- /dev/null +++ b/src/lib/domain/AggressorSide.cpp @@ -0,0 +1,11 @@ +#include "AggressorSide.h" + +namespace bts::domain { + +std::ostream &operator<<( + std::ostream &out, const AggressorSide &value) noexcept { + out << value.name(); + return out; +} + +} // namespace bts::domain diff --git a/src/lib/domain/AggressorSide.h b/src/lib/domain/AggressorSide.h new file mode 100644 index 000000000..daa7749b3 --- /dev/null +++ b/src/lib/domain/AggressorSide.h @@ -0,0 +1,34 @@ +#pragma once +#include + +namespace bts::domain { + +class AggressorSide { + public: + enum Value { None = 0, Buy = 1, Sell = 2 }; + Value value; + + constexpr const char *name() const noexcept; +}; + +std::ostream &operator<<( + std::ostream &out, const AggressorSide &value) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr const char *AggressorSide::name() const noexcept { + switch (value) { + case AggressorSide::None: + return "None"; + case AggressorSide::Buy: + return "Buy"; + case AggressorSide::Sell: + return "Sell"; + default: + return "Unknown"; + } +} + +} // namespace bts::domain diff --git a/src/lib/domain/Book.h b/src/lib/domain/Book.h new file mode 100644 index 000000000..2564e7bac --- /dev/null +++ b/src/lib/domain/Book.h @@ -0,0 +1,14 @@ +#pragma once +#include "BookSide.h" +#include "SecurityID.h" + +namespace bts::domain { + +class Book { + public: + SecurityID security_id; + BookSide bids; + BookSide asks; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/BookLevel.h b/src/lib/domain/BookLevel.h new file mode 100644 index 000000000..90fa3e863 --- /dev/null +++ b/src/lib/domain/BookLevel.h @@ -0,0 +1,42 @@ +#pragma once +#include "Price.h" +#include "Quantity.h" +#include + +namespace bts::domain { + +class BookLevel { + public: + const Price price = {}; + const Quantity total_quantity = {}; + const Quantity direct_quantity = {}; + std::size_t n_orders = {}; + + /** + * @brief Gets an empty book + * + * @details This method offers protection against changes to the default + * constructor. For example, the default constructor could be optimized + * one day so that members aren't initialized at all. No matter what + * changes the future brings, this method will always return an empty book. + * + * @return An empty BookLevel + */ + static constexpr BookLevel empty() noexcept; + + constexpr Quantity implied_quantity() const; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr BookLevel BookLevel::empty() noexcept { + return {Price::nan(), Quantity{0}, Quantity{0}, 0}; +} + +constexpr Quantity BookLevel::implied_quantity() const { + return total_quantity - direct_quantity; +} + +} // namespace bts::domain diff --git a/src/lib/domain/BookLevelComp.h b/src/lib/domain/BookLevelComp.h new file mode 100644 index 000000000..924d3d0c4 --- /dev/null +++ b/src/lib/domain/BookLevelComp.h @@ -0,0 +1,57 @@ +#pragma once +#include "BookLevel.h" +#include "Side.h" + +namespace bts::domain { + +class BookLevelComp { + public: + explicit constexpr BookLevelComp(Side side); + + constexpr bool operator()(const BookLevel &lhs, const BookLevel &rhs) const; + + constexpr bool operator()(const BookLevel &lhs, Price rhs) const; + constexpr bool operator()(Price lhs, const BookLevel &rhs) const; + + bool operator()(const BookLevel &lhs, double rhs) const; + bool operator()(double lhs, const BookLevel &rhs) const; + + private: + Side side_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr BookLevelComp::BookLevelComp(Side side) : side_(side) {} + +constexpr bool BookLevelComp::operator()( + const BookLevel &lhs, const BookLevel &rhs) const { + if (lhs.price == rhs.price) { + return lhs.total_quantity > rhs.total_quantity; + } + return lhs.price.is_better(side_, rhs.price); +} + +constexpr bool BookLevelComp::operator()( + const BookLevel &lhs, Price rhs) const { + return lhs.price.is_better(side_, rhs); +} + +constexpr bool BookLevelComp::operator()( + Price lhs, const BookLevel &rhs) const { + return lhs.is_better(side_, rhs.price); +} + +inline bool BookLevelComp::operator()( + const BookLevel &lhs, double rhs) const { + return operator()(lhs, Price::from_double(rhs)); +} + +inline bool BookLevelComp::operator()( + double lhs, const BookLevel &rhs) const { + return operator()(Price::from_double(lhs), rhs); +} + +} // namespace bts::domain diff --git a/src/lib/domain/BookSide.h b/src/lib/domain/BookSide.h new file mode 100644 index 000000000..e8b150e40 --- /dev/null +++ b/src/lib/domain/BookSide.h @@ -0,0 +1,13 @@ +#pragma once +#include "BookLevel.h" +#include + +namespace bts::domain { + +class BookSide { + public: + // compile time size for speed + std::array blevel; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/Currency.h b/src/lib/domain/Currency.h new file mode 100644 index 000000000..9add121e0 --- /dev/null +++ b/src/lib/domain/Currency.h @@ -0,0 +1,7 @@ +#pragma once + +namespace bts::domain { + +enum class Currency { BTC, EH, USD }; + +} // namespace bts::domain diff --git a/src/lib/domain/FutureSecurity.h b/src/lib/domain/FutureSecurity.h new file mode 100644 index 000000000..1d6cd8bbc --- /dev/null +++ b/src/lib/domain/FutureSecurity.h @@ -0,0 +1,11 @@ +#pragma once +#include "lib/core/TimeStamp.h" + +namespace bts::domain { + +class FutureSecurity { + public: + core::TimeStamp expiration; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/IndexSecurity.h b/src/lib/domain/IndexSecurity.h new file mode 100644 index 000000000..520954aa1 --- /dev/null +++ b/src/lib/domain/IndexSecurity.h @@ -0,0 +1,7 @@ +#pragma once + +namespace bts::domain { + +class IndexSecurity {}; + +} // namespace bts::domain diff --git a/src/lib/domain/LegDefinition.h b/src/lib/domain/LegDefinition.h new file mode 100644 index 000000000..0f26caa3b --- /dev/null +++ b/src/lib/domain/LegDefinition.h @@ -0,0 +1,14 @@ +#pragma once +#include "Quantity.h" +#include "SecurityID.h" +#include "Side.h" + +namespace bts::domain { + +struct LegDefinition { + SecurityID security_id; + Quantity leg_mult; + Side side; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/NewBookSide.h b/src/lib/domain/NewBookSide.h new file mode 100644 index 000000000..aaea8858d --- /dev/null +++ b/src/lib/domain/NewBookSide.h @@ -0,0 +1,20 @@ +#pragma once +#include "BookLevel.h" +#include + +namespace bts::domain { + +class BookSide { + public: + // compile time size for speed + using Container = std::array; + + std::uint32_t num_populated_levels() const noexcept; + bool is_empty() const noexcept; + + private: + // compile time size for speed + Container levels_; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/OptionSecurity.h b/src/lib/domain/OptionSecurity.h new file mode 100644 index 000000000..d3fd927c4 --- /dev/null +++ b/src/lib/domain/OptionSecurity.h @@ -0,0 +1,7 @@ +#pragma once + +namespace bts::domain { + +class OptionSecurity {}; + +} // namespace bts::domain diff --git a/src/lib/domain/OrderKey.h b/src/lib/domain/OrderKey.h new file mode 100644 index 000000000..e3e7c3269 --- /dev/null +++ b/src/lib/domain/OrderKey.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +#include + +namespace bts::domain { + +using OrderKey = utils::ValueWrapper; + +} // namespace bts::domain diff --git a/src/lib/domain/Price.h b/src/lib/domain/Price.h new file mode 100644 index 000000000..52b28fc95 --- /dev/null +++ b/src/lib/domain/Price.h @@ -0,0 +1,457 @@ +#pragma once +#include "Side.h" +#include "lib/utils/bts_assert.h" +#include +#include +#include +#include + +#if bts_assert_msg +#define _ASSERT_MSG(EXP, msg) bts_assert_msg(EXP, msg) +#else +#define _ASSERT_MSG(EXP, msg) \ + do { \ + if (!(EXP)) { \ + std::cerr << "Assert failure: " << std::string(#EXP) << " " << msg \ + << std::endl; \ + if (is_debugger_present()) \ + debug_break(); \ + std::abort(); \ + } \ + } while (0) +#endif + +#define _ASSERT(EXP) _ASSERT_MSG(EXP, "") + +namespace bts::domain { + +class Price { + public: + Price() = default; + explicit constexpr Price( + std::int64_t mantissa, + std::int8_t exponent = Price::exponent()) noexcept; + constexpr Price(Price const &) = default; + Price(Price &&) noexcept = default; + ~Price() = default; + + template + static Price from_float(float price) noexcept; + [[nodiscard]] constexpr float to_float() const noexcept; + + template + static Price from_double(double price) noexcept; + [[nodiscard]] constexpr double to_double() const noexcept; + + static constexpr Price zero() noexcept; + static constexpr Price nan() noexcept; + [[nodiscard]] constexpr Price abs() const noexcept; + [[nodiscard]] Price square() const noexcept; + + [[nodiscard]] constexpr Price improve(Side s, Price by) const noexcept; + [[nodiscard]] constexpr Price worsen(Side s, Price by) const noexcept; + + [[nodiscard]] constexpr bool is_nan() const noexcept; + + [[nodiscard]] constexpr bool is_better(Side s, Price p2) const noexcept; + + [[nodiscard]] constexpr bool is_worse(Side s, Price p2) const noexcept; + + [[nodiscard]] constexpr bool is_better_or_equal( + Side s, Price p2) const noexcept; + + [[nodiscard]] constexpr bool is_worse_or_equal( + Side s, Price p2) const noexcept; + + [[nodiscard]] constexpr Price repl_nan(Price repl) const noexcept; + + [[nodiscard]] constexpr Price get_min(Price other) const noexcept; + [[nodiscard]] constexpr Price get_max(Price other) const noexcept; + + [[nodiscard]] constexpr Price get_better( + Side side, Price other) const noexcept; + + [[nodiscard]] constexpr Price get_worse( + Side side, Price other) const noexcept; + + template + static double get_mean(Iter first, Iter last, bool skip_nan = false); + template + static double get_mean(Range range, bool skip_nan = false); + + Price &operator=(Price const &) = default; + Price &operator=(Price &&) noexcept = default; + + constexpr Price operator-() const noexcept; + + constexpr bool operator==(Price factor) const noexcept; + constexpr bool operator!=(Price factor) const noexcept; + constexpr bool operator<(Price factor) const noexcept; + constexpr bool operator<=(Price factor) const noexcept; + constexpr bool operator>(Price factor) const noexcept; + constexpr bool operator>=(Price factor) const noexcept; + + constexpr Price operator+(Price factor) const noexcept; + constexpr Price operator-(Price factor) const noexcept; + constexpr Price operator*(Price factor) const noexcept; + constexpr Price operator/(Price factor) const noexcept; + + constexpr Price &operator+=(Price factor) noexcept; + constexpr Price &operator-=(Price factor) noexcept; + constexpr Price &operator*=(Price factor) noexcept; + constexpr Price &operator/=(Price factor) noexcept; + + constexpr bool operator==(std::int64_t factor) const noexcept; + constexpr bool operator!=(std::int64_t factor) const noexcept; + constexpr bool operator<(std::int64_t factor) const noexcept; + constexpr bool operator<=(std::int64_t factor) const noexcept; + constexpr bool operator>(std::int64_t factor) const noexcept; + constexpr bool operator>=(std::int64_t factor) const noexcept; + + constexpr Price operator+(std::int64_t factor) const noexcept; + constexpr Price operator-(std::int64_t factor) const noexcept; + constexpr Price operator*(std::int64_t factor) const noexcept; + constexpr Price operator/(std::int64_t factor) const noexcept; + + constexpr Price &operator+=(std::int64_t factor) noexcept; + constexpr Price &operator-=(std::int64_t factor) noexcept; + constexpr Price &operator*=(std::int64_t factor) noexcept; + constexpr Price &operator/=(std::int64_t factor) noexcept; + + [[nodiscard]] constexpr std::int64_t mantissa() const noexcept; + static constexpr std::int8_t exponent() noexcept; + + private: + std::int64_t _mantissa; + + static double _pow10() noexcept; + static double __pow10() noexcept; // NOLINT +}; + +std::ostream &operator<<(std::ostream &out, Price price) noexcept; + +} // namespace bts::domain + +namespace std { + +template <> +struct hash { // NOLINT + constexpr size_t operator()(const bts::domain::Price &price) const noexcept; +}; + +} // namespace std + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +namespace bts::domain { + +// NOLINTNEXTLINE +#define NAN_SENTINEL std::numeric_limits::min() +// NOLINTNEXTLINE +#define IS_NAN(x) (x == std::numeric_limits::min()) + +constexpr Price::Price(std::int64_t mantissa, std::int8_t exponent) noexcept + : _mantissa(mantissa) { + _ASSERT(exponent == Price::exponent()); +} + +template +inline Price Price::from_float(float price) noexcept { + return from_double(price); +} + +constexpr float Price::to_float() const noexcept { + return static_cast(to_double()); +} + +template +inline Price Price::from_double(double price) noexcept { + static_assert(PRECISION == -exponent()); + if (std::isnan(price)) { + return nan(); + } + double temp; // NOLINT + if (fabs(modf(price, &temp)) != 0.0) { + const double rounding = 0.5 * _pow10(); + price += (price > 0) ? rounding : -rounding; + price *= __pow10(); + return Price{(std::int64_t)price}; + } + return Price{(std::int64_t)(price * __pow10())}; +} + +constexpr double Price::to_double() const noexcept { + return is_nan() ? std::numeric_limits::quiet_NaN() + : _mantissa * _pow10(); // NOLINT +} + +constexpr Price Price::zero() noexcept { return Price(0); } +constexpr Price Price::nan() noexcept { return Price(NAN_SENTINEL); } +constexpr std::int8_t Price::exponent() noexcept { return -9; } +constexpr std::int64_t Price::mantissa() const noexcept { return _mantissa; } + +constexpr Price Price::abs() const noexcept { + if (_mantissa >= 0) { + return *this; + } + return Price{-_mantissa}; +} + +inline Price Price::square() const noexcept { + auto d = to_double(); + return from_double(d * d); +} + +constexpr Price Price::improve(Side s, Price by) const noexcept { + if (s == Side::Buy) { + return *this + by; + } + return *this - by; +} + +constexpr Price Price::worsen(Side s, Price by) const noexcept { + if (s == Side::Buy) { + return *this - by; + } + return *this + by; +} + +constexpr bool Price::is_nan() const noexcept { return IS_NAN(_mantissa); } + +constexpr bool Price::is_better(Side s, Price p2) const noexcept { + if (is_nan()) { + return false; + } + if (p2.is_nan()) { + return true; + } + if (s == Side::Buy) { + return *this > p2; + } + return *this < p2; +} + +constexpr bool Price::is_worse(Side s, Price p2) const noexcept { + return p2.is_better(s, *this); +} + +constexpr bool Price::is_better_or_equal(Side s, Price p2) const noexcept { + if (is_nan()) { + // This case is always false (even if p2 is nan), because nan != nan. + return false; + } + if (p2.is_nan()) { + return true; + } + if (s == Side::Buy) { + return *this >= p2; + } + return *this <= p2; +} + +constexpr bool Price::is_worse_or_equal(Side s, Price p2) const noexcept { + return p2.is_better_or_equal(s, *this); +} + +constexpr Price Price::repl_nan(Price repl) const noexcept { + if (is_nan()) { + return repl; + } + return *this; +} + +constexpr Price Price::get_min(Price other) const noexcept { + if (is_nan()) { + return other; + } + if (other.is_nan()) { + return *this; + } + if (*this < other) { + return *this; + } + return other; +} + +constexpr Price Price::get_max(Price other) const noexcept { + if (is_nan()) { + return other; + } + if (other.is_nan()) { + return *this; + } + if (*this > other) { + return *this; + } + return other; +} + +constexpr Price Price::get_better(Side side, Price other) const noexcept { + switch (side.value) { + case Side::Buy: + return get_max(other); + case Side::Sell: + return get_min(other); + default: + return nan(); + } +} + +constexpr Price Price::get_worse(Side side, Price other) const noexcept { + return get_better(side.other(), other); +} + +template +inline double Price::get_mean(Iter first, Iter last, bool skip_nan) { + if (first == last) { + return std::numeric_limits::quiet_NaN(); + } + auto sum = zero(); + std::int32_t n = 0; + do { + if (first->is_nan()) { + if (skip_nan) { + continue; + } + return std::numeric_limits::quiet_NaN(); + } + sum += *first; + n++; + first++; + } while (first != last); + if (n == 0) { + return std::numeric_limits::quiet_NaN(); + } + auto dsum = sum.to_double(); + return dsum / n; +} + +template +inline double Price::get_mean(Range range, bool skip_nan) { + return get_mean(range.begin(), range.end(), skip_nan); +} + +inline double Price::_pow10() noexcept { return std::pow(10.0, exponent()); } +inline double Price::__pow10() noexcept { return std::pow(10.0, -exponent()); } +constexpr Price Price::operator-() const noexcept { + if (is_nan()) { + return *this; + } + return Price{-_mantissa}; +} + +// If a number is nan, all comparisons are false. This is well-established +// behavior; even nan == nan evaluates to false. +// NOLINTNEXTLINE +#define PRICE_PRICE_COMPARISON(op, nan_part) \ + constexpr bool Price::operator op(Price factor) const noexcept { \ + return nan_part _mantissa op factor._mantissa; \ + } + +// NOLINTNEXTLINE +#define PRICE_PRICE_BIN_OP(op) \ + constexpr Price Price::operator op(Price factor) const noexcept { \ + if (is_nan() || factor.is_nan()) { \ + return nan(); \ + } \ + return Price{_mantissa op factor._mantissa}; \ + } + +// NOLINTNEXTLINE +#define PRICE_PRICE_BIN_REF_OP(op) \ + constexpr Price &Price::operator op(Price factor) noexcept { \ + if (factor.is_nan()) { \ + _mantissa = nan()._mantissa; \ + } else if (!is_nan()) { \ + _mantissa op factor._mantissa; \ + } \ + return *this; \ + } + +// NOLINTNEXTLINE +#define PRICE_INT64_COMPARISON(op, nan_part) \ + constexpr bool Price::operator op(std::int64_t factor) const noexcept { \ + return nan_part _mantissa op factor; \ + } + +// NOLINTNEXTLINE +#define PRICE_INT64_BIN_OP(op) \ + constexpr Price Price::operator op(std::int64_t factor) const noexcept { \ + if (is_nan()) { \ + return *this; \ + } \ + return Price{_mantissa op factor}; \ + } + +// NOLINTNEXTLINE +#define PRICE_INT64_BIN_REF_OP(op) \ + constexpr Price &Price::operator op(std::int64_t factor) noexcept { \ + if (!is_nan()) { \ + _mantissa op factor; \ + } \ + return *this; \ + } + +PRICE_PRICE_COMPARISON(!=, is_nan() ||) +PRICE_PRICE_COMPARISON(==, !is_nan() &&) +PRICE_PRICE_COMPARISON(<, !is_nan() &&) +PRICE_PRICE_COMPARISON(<=, !is_nan() &&) +PRICE_PRICE_COMPARISON(>, !is_nan() &&) +PRICE_PRICE_COMPARISON(>=, !is_nan() &&) + +PRICE_PRICE_BIN_OP(+) +PRICE_PRICE_BIN_OP(-) +PRICE_PRICE_BIN_OP(*) +PRICE_PRICE_BIN_OP(/) + +PRICE_PRICE_BIN_REF_OP(+=) +PRICE_PRICE_BIN_REF_OP(-=) +PRICE_PRICE_BIN_REF_OP(*=) +PRICE_PRICE_BIN_REF_OP(/=) + +PRICE_INT64_COMPARISON(!=, is_nan() ||) +PRICE_INT64_COMPARISON(==, !is_nan() &&) +PRICE_INT64_COMPARISON(<, !is_nan() &&) +PRICE_INT64_COMPARISON(<=, !is_nan() &&) +PRICE_INT64_COMPARISON(>, !is_nan() &&) +PRICE_INT64_COMPARISON(>=, !is_nan() &&) + +PRICE_INT64_BIN_OP(+) +PRICE_INT64_BIN_OP(-) +PRICE_INT64_BIN_OP(*) +PRICE_INT64_BIN_OP(/) + +PRICE_INT64_BIN_REF_OP(+=) +PRICE_INT64_BIN_REF_OP(-=) +PRICE_INT64_BIN_REF_OP(*=) +PRICE_INT64_BIN_REF_OP(/=) + +inline std::ostream &operator<<(std::ostream &out, Price p) noexcept { + out << p.to_double(); + return out; +} + +#undef NAN_SENTINEL +#undef IS_NAN +#undef PRICE_PRICE_COMPARISON +#undef PRICE_PRICE_BIN_OP +#undef PRICE_PRICE_BIN_REF_OP +#undef PRICE_INT64_COMPARISON +#undef PRICE_INT64_BIN_OP +#undef PRICE_INT64_BIN_REF_OP + +} // namespace bts::domain + +namespace std { // NOLINT + +constexpr size_t hash::operator()( + const bts::domain::Price &price) const noexcept { + return price.mantissa(); +} + +} // namespace std + +#undef _ASSERT +#undef _ASSERT_MSG diff --git a/src/lib/domain/PriceComp.h b/src/lib/domain/PriceComp.h new file mode 100644 index 000000000..09f843b69 --- /dev/null +++ b/src/lib/domain/PriceComp.h @@ -0,0 +1,28 @@ +#pragma once +#include "Price.h" +#include "Side.h" + +namespace bts::domain { + +struct PriceComp { + constexpr PriceComp() = default; + constexpr PriceComp(Side side); + constexpr PriceComp(const PriceComp &other) = default; + + constexpr bool operator()(const Price &lhs, const Price &rhs); + + private: + Side _side = Side::Buy; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr PriceComp::PriceComp(Side side) : _side(side) {} + +constexpr bool PriceComp::operator()(const Price &lhs, const Price &rhs) { + return lhs.is_better(_side, rhs); +} + +} // namespace bts::domain diff --git a/src/lib/domain/Quantity.h b/src/lib/domain/Quantity.h new file mode 100644 index 000000000..c45426571 --- /dev/null +++ b/src/lib/domain/Quantity.h @@ -0,0 +1,116 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +#include + +namespace bts::domain { + +using Quantity = utils::ValueWrapper; + +constexpr Quantity operator-(Quantity); + +constexpr Quantity operator+(Quantity, Quantity); +constexpr Quantity operator-(Quantity, Quantity); +constexpr Quantity operator*(Quantity, Quantity); +constexpr Quantity operator/(Quantity, Quantity); +constexpr Quantity operator%(Quantity, Quantity); + +constexpr Quantity &operator+=(Quantity &, Quantity); +constexpr Quantity &operator-=(Quantity &, Quantity); +constexpr Quantity &operator*=(Quantity &, Quantity); +constexpr Quantity &operator/=(Quantity &, Quantity); +constexpr Quantity &operator%=(Quantity &, Quantity); + +constexpr bool operator==(Quantity, std::uint32_t); +constexpr bool operator!=(Quantity, std::uint32_t); +constexpr bool operator<(Quantity, std::uint32_t); +constexpr bool operator<=(Quantity, std::uint32_t); +constexpr bool operator>(Quantity, std::uint32_t); +constexpr bool operator>=(Quantity, std::uint32_t); + +constexpr Quantity operator+(Quantity, std::uint32_t); +constexpr Quantity operator-(Quantity, std::uint32_t); +constexpr Quantity operator*(Quantity, std::uint32_t); +constexpr Quantity operator/(Quantity, std::uint32_t); + +Quantity &operator+=(Quantity &, std::uint32_t); +Quantity &operator-=(Quantity &, std::uint32_t); +Quantity &operator*=(Quantity &, std::uint32_t); +Quantity &operator/=(Quantity &, std::uint32_t); + +std::uint32_t &operator+=(std::uint32_t &, Quantity); +std::uint32_t &operator-=(std::uint32_t &, Quantity); +std::uint32_t &operator*=(std::uint32_t &, Quantity); +std::uint32_t &operator/=(std::uint32_t &, Quantity); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr Quantity operator-(Quantity qty) { return Quantity(-qty.value()); } + +#define QTY_QTY_BIN_OP(result_t, op) \ + constexpr result_t operator op(Quantity lhs, Quantity rhs) { \ + return result_t(lhs.value() op rhs.value()); \ + } + +#define QTY_QTY_BIN_REF_OP(op) \ + constexpr Quantity &operator op(Quantity &lhs, Quantity rhs) { \ + lhs.value() op rhs.value(); \ + return lhs; \ + } + +#define QTY_UINT32_BIN_OP(result_t, op) \ + constexpr result_t operator op(Quantity lhs, std::uint32_t rhs) { \ + return result_t(lhs.value() op rhs); /*NOLINT*/ \ + } \ + constexpr result_t operator op(std::uint32_t lhs, Quantity rhs) { \ + return result_t(lhs op rhs.value()); \ + } + +#define QTY_UINT32_BIN_REF_OP(op) \ + inline Quantity &operator op(Quantity &lhs, std::uint32_t rhs) { \ + lhs.value() op rhs; \ + return lhs; \ + } \ + inline std::uint32_t &operator op(std::uint32_t &lhs, Quantity rhs) { \ + lhs op rhs.value(); \ + return lhs; \ + } + +QTY_QTY_BIN_OP(Quantity, +) +QTY_QTY_BIN_OP(Quantity, -) +QTY_QTY_BIN_OP(Quantity, *) +QTY_QTY_BIN_OP(Quantity, /) +QTY_QTY_BIN_OP(Quantity, %) + +QTY_QTY_BIN_REF_OP(+=) +QTY_QTY_BIN_REF_OP(-=) +QTY_QTY_BIN_REF_OP(*=) +QTY_QTY_BIN_REF_OP(/=) +QTY_QTY_BIN_REF_OP(%=) + +QTY_UINT32_BIN_OP(bool, !=) +QTY_UINT32_BIN_OP(bool, ==) +QTY_UINT32_BIN_OP(bool, <) +QTY_UINT32_BIN_OP(bool, <=) +QTY_UINT32_BIN_OP(bool, >) +QTY_UINT32_BIN_OP(bool, >=) + +QTY_UINT32_BIN_OP(Quantity, +) +QTY_UINT32_BIN_OP(Quantity, -) +QTY_UINT32_BIN_OP(Quantity, *) +QTY_UINT32_BIN_OP(Quantity, /) +QTY_UINT32_BIN_OP(Quantity, %) + +QTY_UINT32_BIN_REF_OP(+=) +QTY_UINT32_BIN_REF_OP(-=) +QTY_UINT32_BIN_REF_OP(*=) +QTY_UINT32_BIN_REF_OP(/=) +QTY_UINT32_BIN_REF_OP(%=) + +#undef QTY_QTY_BIN_OP +#undef QTY_QTY_BIN_REF_OP +#undef QTY_UINT32_BIN_OP +#undef QTY_UINT32_BIN_REF_OP + +} // namespace bts::domain diff --git a/src/lib/domain/RFQ.h b/src/lib/domain/RFQ.h new file mode 100644 index 000000000..128cb70db --- /dev/null +++ b/src/lib/domain/RFQ.h @@ -0,0 +1,7 @@ +#pragma once + +namespace bts::domain { + +class RFQ {}; + +} // namespace bts::domain diff --git a/src/lib/domain/Security.h b/src/lib/domain/Security.h new file mode 100644 index 000000000..7557ca5c9 --- /dev/null +++ b/src/lib/domain/Security.h @@ -0,0 +1,34 @@ +#pragma once +#include "FutureSecurity.h" +#include "IndexSecurity.h" +#include "OptionSecurity.h" +#include "SecurityCommon.h" +#include "SpreadSecurity.h" +#include "lib/utils/overloaded.h" +#include + +namespace bts::domain { + +class Security { + public: + SecurityCommon security_common; + std::variant + security; + + SecurityType security_type() const; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline SecurityType Security::security_type() const { + return std::visit( + overloaded{[](const IndexSecurity &) { return SecurityType::INDEX; }, + [](const FutureSecurity &) { return SecurityType::FUTURE; }, + [](const SpreadSecurity &) { return SecurityType::SPREAD; }, + [](const OptionSecurity &) { return SecurityType::OPTION; }}, + security); +} + +} // namespace bts::domain diff --git a/src/lib/domain/SecurityCommon.h b/src/lib/domain/SecurityCommon.h new file mode 100644 index 000000000..f1501ddc1 --- /dev/null +++ b/src/lib/domain/SecurityCommon.h @@ -0,0 +1,30 @@ +#pragma once +#include "Currency.h" +#include "Price.h" +#include "SecurityID.h" +#include "SecurityType.h" +#include "Venue.h" +#include + +namespace bts::domain { + +// All the security common params, ie regardless of Future, Spot, Spread etc +class SecurityCommon { + public: + Venue venue; + SecurityID security_id{}; + SecurityType security_type; + Currency currency; + double display_factor{}; + double cash_per_point{}; + std::string symbol; + std::string security_group; + std::string asset; + std::string security_desc; + Price min_price_increment{}; + double min_price_increment_ammount = 0; + bool implied_matching = false; + std::string cme_security_type; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/SecurityID.h b/src/lib/domain/SecurityID.h new file mode 100644 index 000000000..cae1f47ea --- /dev/null +++ b/src/lib/domain/SecurityID.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +#include + +namespace bts::domain { + +using SecurityID = utils::ValueWrapper; + +} // namespace bts::domain diff --git a/src/lib/domain/SecurityMarketDataStatistics.h b/src/lib/domain/SecurityMarketDataStatistics.h new file mode 100644 index 000000000..76ef2e38d --- /dev/null +++ b/src/lib/domain/SecurityMarketDataStatistics.h @@ -0,0 +1,18 @@ +#pragma once +#include "Price.h" +#include "Quantity.h" +#include "Security.h" + +namespace bts::domain { + +class SecurityMarketDataStatistics { + public: + SecurityID security_id; + Price low; + Price high; + Price open; + Price settle; + Quantity volume; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/SecurityState.h b/src/lib/domain/SecurityState.h new file mode 100644 index 000000000..5eb3148c0 --- /dev/null +++ b/src/lib/domain/SecurityState.h @@ -0,0 +1,13 @@ +#pragma once +#include "SecurityID.h" + +namespace bts::domain { + +class SecurityState { + public: + enum class State { PREOPEN, OPEN }; + State state = State::OPEN; + SecurityID security_id; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/SecurityType.h b/src/lib/domain/SecurityType.h new file mode 100644 index 000000000..205e1de42 --- /dev/null +++ b/src/lib/domain/SecurityType.h @@ -0,0 +1,8 @@ +#pragma once + +namespace bts::domain { + +// more types will be needed there like spots etc +enum class SecurityType { INDEX, FUTURE, SPREAD, OPTION }; + +} // namespace bts::domain diff --git a/src/lib/domain/Side.cpp b/src/lib/domain/Side.cpp new file mode 100644 index 000000000..79ba8ea9d --- /dev/null +++ b/src/lib/domain/Side.cpp @@ -0,0 +1,10 @@ +#include "Side.h" + +namespace bts::domain { + +std::ostream &operator<<(std::ostream &out, const Side &s) { + out << s.name(); + return out; +} + +} // namespace bts::domain diff --git a/src/lib/domain/Side.h b/src/lib/domain/Side.h new file mode 100644 index 000000000..66ea88e58 --- /dev/null +++ b/src/lib/domain/Side.h @@ -0,0 +1,155 @@ +#pragma once +#include + +namespace bts::domain { + +class Side { + public: + enum Value { Buy = 0, Sell = 1 }; + Value value; + + Side() = default; + constexpr Side(Side::Value value) noexcept; // NOLINT + + Side(const Side &) noexcept = default; + Side(Side &&) noexcept = default; + Side &operator=(const Side &) noexcept = default; + Side &operator=(Side &&) noexcept = default; + + ~Side() = default; + + static constexpr Side from_contract_flow(int flow); + + [[nodiscard]] constexpr size_t as_index() const noexcept; + [[nodiscard]] constexpr int as_contract_flow() const noexcept; + [[nodiscard]] constexpr int as_cash_flow() const noexcept; + [[nodiscard]] constexpr const char *name() const noexcept; + [[nodiscard]] constexpr Side other() const noexcept; + template + constexpr NumericT normalize(NumericT numeric) const noexcept; + + template + [[nodiscard]] T &pick(T &pick_if_buy, T &pick_if_sell) const; + + template + [[nodiscard]] const T &pick( + const T &pick_if_buy, const T &pick_if_sell) const; + + template + static void foreach (C c); + + template + static bool foreither(C c); + + template + static bool forboth(C c); +}; + +constexpr bool operator==(Side lhs, Side rhs) noexcept; +constexpr bool operator==(Side lhs, Side::Value rhs) noexcept; +constexpr bool operator!=(Side lhs, Side rhs) noexcept; +constexpr bool operator!=(Side lhs, Side::Value rhs) noexcept; +constexpr Side operator*(Side lhs, Side rhs) noexcept; +constexpr Side operator*(Side lhs, Side::Value rhs) noexcept; + +std::ostream &operator<<(std::ostream &out, const Side &s); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr Side Side::from_contract_flow(int flow) { + if (flow > 0) { + return Side::Buy; + } + return Side::Sell; +} + +constexpr Side::Side(Side::Value value) noexcept : value(value) {} + +constexpr std::size_t Side::as_index() const noexcept { return (int)value; } + +constexpr int Side::as_contract_flow() const noexcept { + if (value == Buy) { + return 1; + } + return -1; +} + +constexpr int Side::as_cash_flow() const noexcept { + if (value == Buy) { + return -1; + } + return 1; +} + +constexpr const char *Side::name() const noexcept { + if (value == Side::Buy) { + return "Buy"; + } + return "Sell"; +} + +constexpr Side Side::other() const noexcept { + if (value == Side::Buy) { + return {Side::Sell}; + } + return {Side::Buy}; +} + +template +constexpr NumericT Side::normalize(NumericT numeric) const noexcept { + return value == Side::Buy ? numeric : -numeric; +} + +template +T &Side::pick(T &pick_if_buy, T &pick_if_sell) const { + return value == Side::Buy ? pick_if_buy : pick_if_sell; +} + +template +const T &Side::pick(const T &pick_if_buy, const T &pick_if_sell) const { + return value == Side::Buy ? pick_if_buy : pick_if_sell; +} + +template +void Side::foreach (C c) { + c(Side(Side::Buy)); + c(Side(Side::Sell)); +} + +template +bool Side::foreither(C c) { + return c(Side(Side::Buy)) || c(Side(Side::Sell)); +} + +template +bool Side::forboth(C c) { + return c(Side(Side::Buy)) && c(Side(Side::Sell)); +} + +constexpr bool operator==(Side lhs, Side rhs) noexcept { + return lhs.value == rhs.value; +} + +constexpr bool operator==(Side lhs, Side::Value rhs) noexcept { + return lhs.value == rhs; +} + +constexpr bool operator!=(Side lhs, Side rhs) noexcept { + return lhs.value != rhs.value; +} + +constexpr bool operator!=(Side lhs, Side::Value rhs) noexcept { + return lhs.value != rhs; +} + +constexpr Side operator*(Side lhs, Side rhs) noexcept { + return rhs == Side::Buy ? lhs : lhs.other(); +} + +constexpr Side operator*(Side lhs, Side::Value rhs) noexcept { + return rhs == Side::Buy ? lhs : lhs.other(); +} + +} // namespace bts::domain diff --git a/src/lib/domain/SpreadSecurity.h b/src/lib/domain/SpreadSecurity.h new file mode 100644 index 000000000..e0cffd72f --- /dev/null +++ b/src/lib/domain/SpreadSecurity.h @@ -0,0 +1,14 @@ +#pragma once +#include "LegDefinition.h" +#include "SecurityCommon.h" +#include + +namespace bts::domain { + +class SpreadSecurity { + public: + SecurityCommon security_definition; + std::vector legs; +}; + +} // namespace bts::domain diff --git a/src/lib/domain/TradeRecord.h b/src/lib/domain/TradeRecord.h new file mode 100644 index 000000000..53ef83cf9 --- /dev/null +++ b/src/lib/domain/TradeRecord.h @@ -0,0 +1,20 @@ +#pragma once +#include "SecurityID.h" +#include "lib/core/TimeStamp.h" +#include "AggressorSide.h" +#include "Quantity.h" +#include "Price.h" + +namespace bts::domain { + +class TradeRecord { + public: + SecurityID sec_id = {}; + TimeStamp time_stamp = {}; + AggressorSide agg_side = {}; + Quantity trade_quantity; + Price trade_price; +}; + + +} // namespace bts::domain diff --git a/src/lib/domain/UserID.h b/src/lib/domain/UserID.h new file mode 100644 index 000000000..c7cd24ff6 --- /dev/null +++ b/src/lib/domain/UserID.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/utils/ValueWrapper.h" +#include + +namespace bts::domain { + +using UserID = utils::ValueWrapper; + +} // namespace bts::domain diff --git a/src/lib/domain/Venue.h b/src/lib/domain/Venue.h new file mode 100644 index 000000000..7ed5620e6 --- /dev/null +++ b/src/lib/domain/Venue.h @@ -0,0 +1,14 @@ +#pragma once + +namespace bts::domain { + +enum class Venue { + None = 0, + Glbx = 1, + Deribit = 2, + Huobi = 3, + Binance = 4, + FTX = 5 +}; + +} // namespace bts::domain diff --git a/src/lib/md/IMarketDataListener.h b/src/lib/md/IMarketDataListener.h new file mode 100644 index 000000000..5ca1203ca --- /dev/null +++ b/src/lib/md/IMarketDataListener.h @@ -0,0 +1,38 @@ +#pragma once +#include + +namespace bts::domain { +class Security; +class SecurityState; +class SecurityMarketDataStatistics; +class Book; +class TradeRecord; +class RFQ; +} // namespace bts::domain + +namespace bts::md { + +class MarketDataError; + +// The is the interface that registers itself to IMarketDataGateway and receives +// all market data via callbacks. These callbacks are always on the same thread. +class IMarketDataListener { + public: + // complete list of all securities. as securities are added subsequent calls + // on this callback are made. IMarketDataGateway guarantees that a security + // is seen via on_securities first, before any other data callbacks on that + // security are made. + virtual void on_securities( + const std::vector &complete_security_list) = 0; + virtual void on_security_state( + const domain::SecurityState &security_state) = 0; + virtual void on_security_statistics( + const domain::SecurityMarketDataStatistics &security_stats) = 0; + virtual void on_book(const domain::Book &book) = 0; + virtual void on_trades( + const std::vector &trades) = 0; + virtual void on_rfqs(const std::vector &rfqs) = 0; + virtual void on_error(const MarketDataError &error) = 0; +}; + +} // namespace bts::md diff --git a/src/lib/md/MarketDataError.cpp b/src/lib/md/MarketDataError.cpp new file mode 100644 index 000000000..2d441ee63 --- /dev/null +++ b/src/lib/md/MarketDataError.cpp @@ -0,0 +1,11 @@ +#include "MarketDataError.h" + +namespace bts::md { + +std::ostream &operator<<( + std::ostream &out, const MarketDataError &value) noexcept { + out << value.type_name() << ": " << value.message; + return out; +} + +} // namespace bts::md diff --git a/src/lib/md/MarketDataError.h b/src/lib/md/MarketDataError.h new file mode 100644 index 000000000..eb2f75811 --- /dev/null +++ b/src/lib/md/MarketDataError.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include + +namespace bts::md { + +class MarketDataError { + public: + enum Type { DISCONNECTION }; + Type type = Type::DISCONNECTION; + std::string message = {}; + + constexpr const char *type_name() const noexcept; +}; + +std::ostream &operator<<( + std::ostream &out, const MarketDataError &value) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr const char *MarketDataError::type_name() const noexcept { + switch (type) { + case MarketDataError::DISCONNECTION: + return "DISCONNECTION"; + default: + return "UNKNOWN"; + } +} + +} // namespace bts::md diff --git a/src/lib/md/MarketDataListenerID.h b/src/lib/md/MarketDataListenerID.h new file mode 100644 index 000000000..46a34dd98 --- /dev/null +++ b/src/lib/md/MarketDataListenerID.h @@ -0,0 +1,9 @@ +#pragma once +#include "lib/core/ValueWrapper.h" + +namespace bts::md { + +using MarketDataListenerID = + core::ValueWrapper; + +} // namespace bts::md diff --git a/src/lib/md/MarketDataSubscription.h b/src/lib/md/MarketDataSubscription.h new file mode 100644 index 000000000..a0e34e2e2 --- /dev/null +++ b/src/lib/md/MarketDataSubscription.h @@ -0,0 +1,34 @@ +#pragma once +#include "lib/domain/Venue.h" +#include "lib/md/binance/BinanceMarketDataSubscription.h" +#include "lib/md/cme/GlbxMarketDataSubscription.h" +#include "lib/md/deribit/DeribitMarketDataSubscription.h" +#include + +namespace bts::md { + +struct MarketDataSubscription { + domain::Venue venue() const; + const std::variant< + cme::GlbxMarketDataSubscription, + deribit::DeribitMarketDataSubscription, + binance::BinanceMarketDataSubscription> + subscription{}; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline domain::Venue MarketDataSubscription::venue() const { + if (std::holds_alternative(subscription)) { + return domain::Venue::Glbx; + } + if (std::holds_alternative( + subscription)) { + return domain::Venue::Deribit; + } + return domain::Venue::Glbx; +} + +} // namespace bts::md diff --git a/src/lib/md/SecurityMarketState.cpp b/src/lib/md/SecurityMarketState.cpp new file mode 100644 index 000000000..5ac068108 --- /dev/null +++ b/src/lib/md/SecurityMarketState.cpp @@ -0,0 +1,11 @@ +#include "SecurityMarketState.h" + +namespace bts::md { + +std::ostream &operator<<( + std::ostream &out, const SecurityMarketState &value) noexcept { + out << value.name(); + return out; +} + +} // namespace bts::md diff --git a/src/lib/md/SecurityMarketState.h b/src/lib/md/SecurityMarketState.h new file mode 100644 index 000000000..513753d26 --- /dev/null +++ b/src/lib/md/SecurityMarketState.h @@ -0,0 +1,32 @@ +#pragma once +#include + +namespace bts::md { + +class SecurityMarketState { + public: + enum Value { PREOPEN, OPEN }; + Value value = Value::OPEN; + + constexpr const char *name() const noexcept; +}; + +std::ostream &operator<<( + std::ostream &out, const SecurityMarketState &value) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr const char *SecurityMarketState::name() const noexcept { + switch (value) { + case SecurityMarketState::PREOPEN: + return "PREOPEN"; + case SecurityMarketState::OPEN: + return "OPEN"; + default: + return "UNKNOWN"; + } +} + +} // namespace bts::md diff --git a/src/lib/md/cme/GlbxMarketDataSubscription.h b/src/lib/md/cme/GlbxMarketDataSubscription.h new file mode 100644 index 000000000..b80a38321 --- /dev/null +++ b/src/lib/md/cme/GlbxMarketDataSubscription.h @@ -0,0 +1,20 @@ +#pragma once + +namespace bts::md::cme { + +// subscription parameters, ex: this is what we have for glbx +// feed_handler { +// name: "360" # COMEX Globex Futures +// incremental_a { group: "224.0.31.192" port: 14360} +// incremental_b { group: "224.0.32.192" port: 15360} +// instrument { group: "224.0.31.212" port: 14360} +// snapshot_mbo { group: "233.72.75.21" port: 23360} +// interface_a: "172.17.248.30" +// interface_b: "172.17.249.30" +// process_mbo: false +// process_trades: false +// } +// (I can give a complete implementation of GlbxMarketDataSubscription) +struct GlbxMarketDataSubscription {}; + +} // namespace bts::md::cme diff --git a/src/lib/md/deribit/DeribitMarketDataSubscription.h b/src/lib/md/deribit/DeribitMarketDataSubscription.h new file mode 100644 index 000000000..f765bc75e --- /dev/null +++ b/src/lib/md/deribit/DeribitMarketDataSubscription.h @@ -0,0 +1,7 @@ +#pragma once + +namespace bts::md::deribit { + +struct DeribitMarketDataSubscription {}; + +} // namespace bts::md::deribit diff --git a/src/lib/md/emumba/IMarketDataListener.h b/src/lib/md/emumba/IMarketDataListener.h deleted file mode 100644 index 3b1767cbc..000000000 --- a/src/lib/md/emumba/IMarketDataListener.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include - -namespace EmumbaMarketData { - -struct Security; -struct SecurityState; -struct SecurityMarketDataStatistics; -struct Book; -struct TradeRecord; -struct MarketDataError; -struct RFQ; - -// The is the interface that registers itself to IMarketDataGateway and receives -// all market data via callbacks. These callbacks are always on the same thread. -class IMarketDataListener { - // complete list of all securities. as securities are added subsequent calls - // on this callback are made. IMarketDataGateway guarantees that a security - // is seen via on_securities first, before any other data callbacks on that - // security are made. - virtual void on_securities( - const std::vector &complete_security_list) = 0; - virtual void on_security_state(const SecurityState &security_state) = 0; - virtual void on_security_statistics( - const SecurityMarketDataStatistics &security_stats) = 0; - virtual void on_book(const Book &book) = 0; - virtual void on_trades(const std::vector &trades) = 0; - virtual void on_rfqs(const std::vector &rfqs) = 0; - virtual void on_error(const MarketDataError &error) = 0; -}; - -} // namespace EmumbaMarketData \ No newline at end of file diff --git a/src/lib/md/emumba/MarketDataCommon.h b/src/lib/md/emumba/MarketDataCommon.h deleted file mode 100644 index 40cf66655..000000000 --- a/src/lib/md/emumba/MarketDataCommon.h +++ /dev/null @@ -1,235 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace EmumbaMarketData { - -// placed a bunch of common types here. Need to be exctracted out to their own -// classes/files - -// wrapper around primitive types, so that SecurityID, OrderID, Quantity etc -// are types instead of size_t, uint32_t etc -template -class ValueWrapper { - public: - constexpr ValueWrapper() = default; - ~ValueWrapper() = default; - constexpr explicit ValueWrapper(ValueT const &v) : _v(v) {} - constexpr explicit ValueWrapper(ValueT &&v) : _v(std::move(v)) {} - - constexpr ValueWrapper(ValueWrapper const &v) = default; - constexpr ValueWrapper(ValueWrapper &&v) noexcept = default; - ValueWrapper &operator=(ValueWrapper const &v) = default; - ValueWrapper &operator=(ValueWrapper &&v) noexcept = default; - - constexpr bool operator==(ValueWrapper const &v) const { return _v == v._v; } - constexpr bool operator!=(ValueWrapper const &v) const { return _v != v._v; } - constexpr bool operator<(ValueWrapper const &v) const { return _v < v._v; } - constexpr bool operator<=(ValueWrapper const &v) const { return _v <= v._v; } - constexpr bool operator>(ValueWrapper const &v) const { return _v > v._v; } - constexpr bool operator>=(ValueWrapper const &v) const { return _v >= v._v; } - constexpr ValueT const &value() const { return _v; } - constexpr ValueT &value() { return _v; } - - private: - ValueT _v; -}; - -using Quantity = ValueWrapper; -using SecurityID = ValueWrapper; -using AccountID = ValueWrapper; -using UserID = ValueWrapper; -using MarketDataListenerID = - ValueWrapper; -using OrderKey = ValueWrapper; - -// more types will be needed there like spots etc -enum class SecurityType { INDEX, FUTURE, SPREAD, OPTION }; - -enum class Currency { BTC, EH, USD }; - -// Usually exchanges give price's as mantissa and exponent. The fastest way is -// to keep it like that and do the math in integers vs doubles (I can give you a -// complete implementation of this - this is just a placeholder for now) -class Price { - public: - constexpr Price() = default; - constexpr Price(std::int64_t mantissa) noexcept : _mantissa(mantissa) {} - constexpr Price(Price const &) = default; - constexpr bool is_nan() const { - return _mantissa == std::numeric_limits::min(); - } - inline constexpr double to_double() const noexcept { - return is_nan() ? std::numeric_limits::quiet_NaN() - : _mantissa * pow10(); - } - inline constexpr double pow10() const noexcept { - return std::pow(10.0, exp); - } - // all math operators should be overloaded here (I can send the whole thing) - constexpr bool operator==(const Price &o) const noexcept { - return _mantissa == o._mantissa; - } - constexpr bool operator<(const Price &o) const noexcept { - return _mantissa < o._mantissa; - } - constexpr Price &operator+=(const Price &o) noexcept { - _mantissa += o._mantissa; - return *this; - } - - private: - // 9 decimal places is the max we have seen. cryptos might be different - const std::int8_t exp{-9}; - std::int64_t _mantissa{0}; -}; - -enum class Venue { Glbx = 0, Deribit = 1, Huobi = 2, Binance = 3, FTX }; - -// subscription parameters, ex: this is what we have for glbx -// feed_handler { -// name: "360" # COMEX Globex Futures -// incremental_a { group: "224.0.31.192" port: 14360} -// incremental_b { group: "224.0.32.192" port: 15360} -// instrument { group: "224.0.31.212" port: 14360} -// snapshot_mbo { group: "233.72.75.21" port: 23360} -// interface_a: "172.17.248.30" -// interface_b: "172.17.249.30" -// process_mbo: false -// process_trades: false -// } -// (I can give a complete implementation of GlbxMarketDataSubscription) -struct GlbxMarketDataSubscription {}; - -struct DeribitMarketDataSubscription {}; - -struct MarketDataGatewaySubscription { - const Venue venue; - const std::variant - subscription = {DeribitMarketDataSubscription{}}; -}; - -// I can send this whole class later but its a nice wrapper around the linux -// posix time -struct TimeStamp { - private: - // posix time - uint64_t _v{}; - - public: - uint64_t value() const { return _v; } -}; - -enum class Side { BUY, SIDE }; - -// All the security common params, ie regardless of Future, Spot, Spread etc -class SecurityCommon { - public: - Venue venue; - SecurityID security_id{}; - SecurityType security_type; - Currency currency; - double display_factor{}; - double cash_per_point{}; - std::string symbol; - std::string security_group; - std::string asset; - std::string security_desc; - Price min_price_increment{}; - double min_price_increment_ammount = 0; - bool implied_matching = false; -}; - -class IndexSecurity {}; - -class FutureSecurity { - public: - TimeStamp expiration; -}; - -struct LegDefinition { - SecurityID security_id; - Quantity leg_mult; - Side side; -}; - -class SpreadSecurity { - public: - std::vector legs; -}; - -struct Security { - SecurityCommon security_common; - std::variant security; - SecurityType security_type() const { - if (std::holds_alternative(security)) { - return SecurityType::INDEX; - } else if (std::holds_alternative(security)) { - return SecurityType::FUTURE; - } else { - return SecurityType::SPREAD; - } - } -}; - -struct BookLevel { - const Price price = {}; - const Quantity total_qty = {}; - const Quantity direct_qty = {}; - size_t n_orders = {}; - Quantity implied_qty() const { return Quantity{}; } -}; - -struct BookSide { - // compile time size for speed - std::array blevel; -}; - -struct Book { - SecurityID security_id; - BookSide bids; - BookSide asks; -}; - -enum class AggressorSide { Buy = 0, Sell = 1, None = 2 }; - -struct TradeRecord { - SecurityID sec_id = {}; - TimeStamp time_stamp = {}; - AggressorSide agg_side = {}; - Quantity trade_quantity; - Price trade_price; -}; - -struct SecurityState { - public: - enum class State { PREOPEN, OPEN }; - State state = State::OPEN; - SecurityID security_id; -}; - -struct SecurityMarketDataStatistics { - SecurityID security_id; - Price low; - Price high; - Price open; - Price settle; - Quantity volume; -}; - -struct MarketDataError { - enum class ErrorType { DISCONNECTION }; - ErrorType error_type = ErrorType::DISCONNECTION; - std::string error_text = {}; -}; - -struct RFQ {}; - -} // namespace EmumbaMarketData \ No newline at end of file diff --git a/src/lib/md/emumba/MarketDataGateway.h b/src/lib/md/emumba/MarketDataGateway.h index 47b669b0b..6f59d0ae1 100644 --- a/src/lib/md/emumba/MarketDataGateway.h +++ b/src/lib/md/emumba/MarketDataGateway.h @@ -1,10 +1,14 @@ #pragma once - -#include "MarketDataCommon.h" +#include "lib/domain/SecurityID.h" +#include "lib/md/MarketDataListenerID.h" +#include "lib/md/MarketDataSubscription.h" #include #include #include + +namespace bts::md { class IMarketDataListener; +} namespace EmumbaMarketData { @@ -15,7 +19,7 @@ struct MarketDataRegistration { std::string error_text{}; }; // will have a value when there is no error - std::optional listener_id; + std::optional listener_id; RegstrationError error = {}; }; @@ -25,10 +29,12 @@ class MarketDataGateway { // listener is assigned an ID if the registration is successful. Subsequent // subscription of securities and unregister_listener is is via this id. MarketDataRegistration register_listener( - const MarketDataGatewaySubscription &, IMarketDataListener *) noexcept; + const bts::md::MarketDataSubscription &, + bts::md::IMarketDataListener *) noexcept; void subscribe( - MarketDataListenerID id, const std::vector &securities); - void unregister_listener(MarketDataListenerID id); + bts::md::MarketDataListenerID id, + const std::vector &securities); + void unregister_listener(bts::md::MarketDataListenerID id); }; -} // namespace EmumbaMarketData \ No newline at end of file +} // namespace EmumbaMarketData diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index 8f3fa5c4a..8796f8513 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -1,5 +1,4 @@ -#include "lib/md/emumba/IMarketDataListener.h" -#include "lib/md/emumba/MarketDataCommon.h" +#include "lib/md/IMarketDataListener.h" #include "lib/md/emumba/MarketDataGateway.h" #include "lib/md/emumba/OrderGateway.h" #include "lib/orderman/orders/spreader_orders/tests/tests_common.h" diff --git a/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.cpp b/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.cpp index f5bce2356..99d74f4d4 100644 --- a/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.cpp +++ b/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.cpp @@ -1,8 +1,22 @@ #include "EmumbaMDListener.h" +#include "lib/domain/AggressorSide.h" +#include "lib/domain/Book.h" +#include "lib/domain/FutureSecurity.h" +#include "lib/domain/IndexSecurity.h" +#include "lib/domain/OptionSecurity.h" +#include "lib/domain/RFQ.h" +#include "lib/domain/Security.h" +#include "lib/domain/SecurityMarketDataStatistics.h" +#include "lib/domain/SecurityState.h" +#include "lib/domain/Side.h" +#include "lib/domain/SpreadSecurity.h" +#include "lib/domain/TradeRecord.h" +#include "lib/domain/Venue.h" #include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" #include "lib/md/IntPrice.h" #include "lib/md/MDProducer.h" +#include "lib/md/MarketDataError.h" #include "lib/md/Shm/FixedSecurity.h" #include "lib/md/protos/co2.pb.h" #include "lib/utils/TimeStamp.h" @@ -27,58 +41,55 @@ void clear_books(MdArray &md_side) { } } -void copy_books( - const EmumbaMarketData::BookSide &from, MdArray &to, bool clear = true) { +void copy_books(const domain::BookSide &from, MdArray &to, bool clear = true) { if (clear) { clear_books(to); } size_t i = 0; - for (const EmumbaMarketData::BookLevel &l : from.blevel) { + for (const domain::BookLevel &l : from) { if (i < COCONUT_MAX_DEPTH) { to[i++] = PriceSize{// TODO IntPrice_from_double<9>(l.price.to_double()), - l.direct_qty.value()}; + l.direct_quantity.value()}; } } } } // namespace helpers -inline api::Venue convert_venue(EmumbaMarketData::Venue /*unused*/) { +inline api::Venue convert_venue(domain::Venue /*unused*/) { // TODO return api::Venue::VENUE_BTS; } -inline co2::TradingStatus convert_status( - EmumbaMarketData::SecurityState::State state) { +inline co2::TradingStatus convert_status(domain::SecurityState::State state) { switch (state) { - case EmumbaMarketData::SecurityState::State::OPEN: + case domain::SecurityState::State::OPEN: return co2::TRADINGSTATUS_NORMAL; - case EmumbaMarketData::SecurityState::State::PREOPEN: + case domain::SecurityState::State::PREOPEN: return co2::TRADINGSTATUS_PRE_OPEN; } return co2::TRADINGSTATUS_UNKNOWN; } -inline co2::AggressorSide convert_agg_side( - EmumbaMarketData::AggressorSide agg_side) { - switch (agg_side) { - case EmumbaMarketData::AggressorSide::Buy: +inline co2::AggressorSide convert_agg_side(domain::AggressorSide agg_side) { + switch (agg_side.value) { + case domain::AggressorSide::Buy: return co2::AggressorSide::AGGRESSOR_SIDE_BUY; - case EmumbaMarketData::AggressorSide::Sell: + case domain::AggressorSide::Sell: return co2::AggressorSide::AGGRESSOR_SIDE_SELL; - case EmumbaMarketData::AggressorSide::None: + case domain::AggressorSide::None: return co2::AggressorSide::AGGRESSOR_SIDE_NONE; } return co2::AggressorSide::AGGRESSOR_SIDE_NONE; } -bts::md::IntPrice convert_to_intprice(EmumbaMarketData::Price price) { +bts::md::IntPrice convert_to_intprice(domain::Price price) { return IntPrice_from_double<9>(price.to_double()); } inline EmumbaSecurityData *get_security( - EmumbaSecurityIndex &sec_map, EmumbaMarketData::SecurityID id) { + EmumbaSecurityIndex &sec_map, domain::SecurityID id) { auto int_id = static_cast(id.value()); EmumbaSecurityData *rv = sec_map.get_security(int_id); bts_assert(rv != nullptr); @@ -99,20 +110,16 @@ EmumbaMDListener::EmumbaMDListener( : me(std::make_unique(index, md_producer)) {} void EmumbaMDListener::on_securities( - const std::vector - &complete_security_list) { - for (EmumbaMarketData::Security const *s : complete_security_list) { - if (std::holds_alternative( - s->security)) { + const std::vector &complete_security_list) { + for (domain::Security const *s : complete_security_list) { + if (std::holds_alternative(s->security)) { _add_index(*s); - } else if (std::holds_alternative( - s->security)) { + } else if (std::holds_alternative(s->security)) { _add_future(*s); } } } -void EmumbaMDListener::on_security_state( - const EmumbaMarketData::SecurityState &state) { +void EmumbaMDListener::on_security_state(const domain::SecurityState &state) { EmumbaSecurityData *sec_data = get_security(me->index, state.security_id); if (sec_data == nullptr) { LOG(md, Error, @@ -125,7 +132,7 @@ void EmumbaMDListener::on_security_state( } void EmumbaMDListener::on_security_statistics( - const EmumbaMarketData::SecurityMarketDataStatistics &security_stats) { + const domain::SecurityMarketDataStatistics &security_stats) { EmumbaSecurityData *sec_data = get_security(me->index, security_stats.security_id); if (sec_data == nullptr) { @@ -145,7 +152,7 @@ void EmumbaMDListener::on_security_statistics( date_temp); } -void EmumbaMDListener::on_book(const EmumbaMarketData::Book &book) { +void EmumbaMDListener::on_book(const domain::Book &book) { EmumbaSecurityData *sec_data = get_security(me->index, book.security_id); if (sec_data == nullptr) { LOG(md, Error, "EmumbaMDListener::on_book could not find id: {}", @@ -159,8 +166,8 @@ void EmumbaMDListener::on_book(const EmumbaMarketData::Book &book) { } void EmumbaMDListener::on_trades( - const std::vector &latest_trades) { - for (EmumbaMarketData::TradeRecord const *t : latest_trades) { + const std::vector &latest_trades) { + for (domain::TradeRecord const *t : latest_trades) { EmumbaSecurityData *sec_data = get_security(me->index, t->sec_id); auto now = utils::TimeStamp::now(); if (sec_data != nullptr) { @@ -174,41 +181,40 @@ void EmumbaMDListener::on_trades( } } } + void EmumbaMDListener::on_rfqs( - const std::vector & /*unused*/) { + const std::vector & /*unused*/) { // TODO } -void EmumbaMDListener::on_error( - const EmumbaMarketData::MarketDataError &error) { - LOG(md, Error, "EmumbaMDListener::on_error error: {}", error.error_text); +void EmumbaMDListener::on_error(const MarketDataError &error) { + LOG(md, Error, "EmumbaMDListener::on_error error: {}", error); // TODO: clear out books? } EmumbaMDListener::~EmumbaMDListener() = default; // private functions -void EmumbaMDListener::_add_index(const EmumbaMarketData::Security &sec) { +void EmumbaMDListener::_add_index(const domain::Security &sec) { FixedStockSecurity fsec = {}; _copy_security_common(sec, fsec); _add_security(fsec); } -void EmumbaMDListener::_add_future(const EmumbaMarketData::Security &sec) { +void EmumbaMDListener::_add_future(const domain::Security &sec) { FixedFutureSecurity fsec = {}; _copy_security_common(sec, fsec); _add_security(fsec); - const auto &future_sec = - std::get(sec.security); + const auto &future_sec = std::get(sec.security); auto boost_ts = - boost::posix_time::from_time_t(future_sec.expiration.value()); + boost::posix_time::from_time_t(future_sec.expiration.nanos()); fsec.expiration_year = boost_ts.date().year(); fsec.expiration_month = boost_ts.date().month().as_number(); fsec.expiration_day = boost_ts.date().day().as_number(); } void EmumbaMDListener::_copy_security_common( - const EmumbaMarketData::Security &from, FixedSecurity &to) const { + const domain::Security &from, FixedSecurity &to) const { to.numeric_id = from.security_common.security_id.value(); to.symbol(from.security_common.symbol); to.security_desc(from.security_common.security_desc); diff --git a/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.h b/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.h index f5b43e5db..57ace141a 100644 --- a/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.h +++ b/src/server/sparkd/md/platforms/emumba/EmumbaMDListener.h @@ -1,7 +1,6 @@ #include "EmumbaSecurityIndex.h" +#include "lib/md/IMarketDataListener.h" #include "lib/md/MDProducer.h" -#include "lib/md/emumba/IMarketDataListener.h" -#include "lib/md/emumba/MarketDataCommon.h" namespace bts::md { @@ -9,34 +8,31 @@ class MDProducer; class FixedSecurity; // Listens to the emumba market data source and -class EmumbaMDListener : public EmumbaMarketData::IMarketDataListener { +class EmumbaMDListener : public IMarketDataListener { private: struct PImpl; std::unique_ptr me; public: EmumbaMDListener(EmumbaSecurityIndex &index, MDProducer &md_producer); - void on_securities(const std::vector + void on_securities(const std::vector &complete_security_list) override; - void on_security_state( - const EmumbaMarketData::SecurityState &security_state) override; + void on_security_state(const domain::SecurityState &security_state) override; void on_security_statistics( - const EmumbaMarketData::SecurityMarketDataStatistics &security_stats) - override; - void on_book(const EmumbaMarketData::Book &book) override; - void on_trades(const std::vector - &trades) override; - void on_rfqs( - const std::vector &rfqs) override; - void on_error(const EmumbaMarketData::MarketDataError &error) override; + const domain::SecurityMarketDataStatistics &security_stats) override; + void on_book(const domain::Book &book) override; + void on_trades( + const std::vector &trades) override; + void on_rfqs(const std::vector &rfqs) override; + void on_error(const MarketDataError &error) override; ~EmumbaMDListener(); private: - void _add_index(const EmumbaMarketData::Security &sec); - void _add_future(const EmumbaMarketData::Security &sec); + void _add_index(const domain::Security &sec); + void _add_future(const domain::Security &sec); void _copy_security_common( - const EmumbaMarketData::Security &from, FixedSecurity &to) const; + const domain::Security &from, FixedSecurity &to) const; void _add_security(const FixedSecurity &security); }; -} // namespace bts::md \ No newline at end of file +} // namespace bts::md