refactor: Move classes out of MarketDataCommon.h into own files

This commit is contained in:
John Colvin committed 2021-11-19 16:18:15 -05:00
1 parent a0ce1b361f
commit 4860d0cb3d
47 files changed
+1501 -334

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
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
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
namespace bts::core {
template <class Tag, class ValueT>
using ValueWrapper = utils::ValueWrapper<Tag, ValueT>;
} // namespace bts::core
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include <cstdint>
namespace bts::domain {
using AccountID = utils::ValueWrapper<struct AccountIDTag, std::uint64_t>;
} // namespace bts::domain
+11
View File
@@ -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
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <ostream>
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
+14
View File
@@ -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
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include "Price.h"
#include "Quantity.h"
#include <cstddef>
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
+57
View File
@@ -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
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include "BookLevel.h"
#include <array>
namespace bts::domain {
class BookSide {
public:
// compile time size for speed
std::array<BookLevel, 10> blevel;
};
} // namespace bts::domain
+7
View File
@@ -0,0 +1,7 @@
#pragma once
namespace bts::domain {
enum class Currency { BTC, EH, USD };
} // namespace bts::domain
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include "lib/core/TimeStamp.h"
namespace bts::domain {
class FutureSecurity {
public:
core::TimeStamp expiration;
};
} // namespace bts::domain
+7
View File
@@ -0,0 +1,7 @@
#pragma once
namespace bts::domain {
class IndexSecurity {};
} // namespace bts::domain
+14
View File
@@ -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
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include "BookLevel.h"
#include <array>
namespace bts::domain {
class BookSide {
public:
// compile time size for speed
using Container = std::array<BookLevel, 10>;
std::uint32_t num_populated_levels() const noexcept;
bool is_empty() const noexcept;
private:
// compile time size for speed
Container levels_;
};
} // namespace bts::domain
+7
View File
@@ -0,0 +1,7 @@
#pragma once
namespace bts::domain {
class OptionSecurity {};
} // namespace bts::domain
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include <cstdint>
namespace bts::domain {
using OrderKey = utils::ValueWrapper<struct OrderKeyTag, std::uint64_t>;
} // namespace bts::domain
+457
View File
@@ -0,0 +1,457 @@
#pragma once
#include "Side.h"
#include "lib/utils/bts_assert.h"
#include <cmath>
#include <cstdint>
#include <limits>
#include <ostream>
#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 <std::size_t PRECISION = 9>
static Price from_float(float price) noexcept;
[[nodiscard]] constexpr float to_float() const noexcept;
template <std::size_t PRECISION = 9>
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 <class Iter>
static double get_mean(Iter first, Iter last, bool skip_nan = false);
template <class Range>
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<bts::domain::Price> { // 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<std::int64_t>::min()
// NOLINTNEXTLINE
#define IS_NAN(x) (x == std::numeric_limits<std::int64_t>::min())
constexpr Price::Price(std::int64_t mantissa, std::int8_t exponent) noexcept
: _mantissa(mantissa) {
_ASSERT(exponent == Price::exponent());
}
template <std::size_t PRECISION>
inline Price Price::from_float(float price) noexcept {
return from_double(price);
}
constexpr float Price::to_float() const noexcept {
return static_cast<float>(to_double());
}
template <std::size_t PRECISION>
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<double>::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 <class Iter>
inline double Price::get_mean(Iter first, Iter last, bool skip_nan) {
if (first == last) {
return std::numeric_limits<double>::quiet_NaN();
}
auto sum = zero();
std::int32_t n = 0;
do {
if (first->is_nan()) {
if (skip_nan) {
continue;
}
return std::numeric_limits<double>::quiet_NaN();
}
sum += *first;
n++;
first++;
} while (first != last);
if (n == 0) {
return std::numeric_limits<double>::quiet_NaN();
}
auto dsum = sum.to_double();
return dsum / n;
}
template <class Range>
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<bts::domain::Price>::operator()(
const bts::domain::Price &price) const noexcept {
return price.mantissa();
}
} // namespace std
#undef _ASSERT
#undef _ASSERT_MSG
+28
View File
@@ -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
+116
View File
@@ -0,0 +1,116 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include <cstdint>
namespace bts::domain {
using Quantity = utils::ValueWrapper<struct QuantityTag, std::uint32_t>;
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
+7
View File
@@ -0,0 +1,7 @@
#pragma once
namespace bts::domain {
class RFQ {};
} // namespace bts::domain
+34
View File
@@ -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 <variant>
namespace bts::domain {
class Security {
public:
SecurityCommon security_common;
std::variant<IndexSecurity, FutureSecurity, SpreadSecurity, OptionSecurity>
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
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "Currency.h"
#include "Price.h"
#include "SecurityID.h"
#include "SecurityType.h"
#include "Venue.h"
#include <string>
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
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include <cstdint>
namespace bts::domain {
using SecurityID = utils::ValueWrapper<struct SecuirtyIDTag, std::uint32_t>;
} // namespace bts::domain
@@ -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
+13
View File
@@ -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
+8
View File
@@ -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
+10
View File
@@ -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
+155
View File
@@ -0,0 +1,155 @@
#pragma once
#include <ostream>
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 <class NumericT>
constexpr NumericT normalize(NumericT numeric) const noexcept;
template <class T>
[[nodiscard]] T &pick(T &pick_if_buy, T &pick_if_sell) const;
template <class T>
[[nodiscard]] const T &pick(
const T &pick_if_buy, const T &pick_if_sell) const;
template <class C>
static void foreach (C c);
template <class C>
static bool foreither(C c);
template <class C>
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 <class NumericT>
constexpr NumericT Side::normalize(NumericT numeric) const noexcept {
return value == Side::Buy ? numeric : -numeric;
}
template <class T>
T &Side::pick(T &pick_if_buy, T &pick_if_sell) const {
return value == Side::Buy ? pick_if_buy : pick_if_sell;
}
template <class T>
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 <class C>
void Side::foreach (C c) {
c(Side(Side::Buy));
c(Side(Side::Sell));
}
template <class C>
bool Side::foreither(C c) {
return c(Side(Side::Buy)) || c(Side(Side::Sell));
}
template <class C>
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
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include "LegDefinition.h"
#include "SecurityCommon.h"
#include <vector>
namespace bts::domain {
class SpreadSecurity {
public:
SecurityCommon security_definition;
std::vector<LegDefinition> legs;
};
} // namespace bts::domain
+20
View File
@@ -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
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include <cstdint>
namespace bts::domain {
using UserID = utils::ValueWrapper<struct UserIDTag, std::uint64_t>;
} // namespace bts::domain
+14
View File
@@ -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
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <vector>
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<domain::Security const *> &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<domain::TradeRecord const *> &trades) = 0;
virtual void on_rfqs(const std::vector<domain::RFQ const *> &rfqs) = 0;
virtual void on_error(const MarketDataError &error) = 0;
};
} // namespace bts::md
+11
View File
@@ -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
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <ostream>
#include <string>
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
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "lib/core/ValueWrapper.h"
namespace bts::md {
using MarketDataListenerID =
core::ValueWrapper<struct MarketDataListenedIDTag, std::uint32_t>;
} // namespace bts::md
+34
View File
@@ -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 <variant>
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<cme::GlbxMarketDataSubscription>(subscription)) {
return domain::Venue::Glbx;
}
if (std::holds_alternative<deribit::DeribitMarketDataSubscription>(
subscription)) {
return domain::Venue::Deribit;
}
return domain::Venue::Glbx;
}
} // namespace bts::md
+11
View File
@@ -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
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <ostream>
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
@@ -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
@@ -0,0 +1,7 @@
#pragma once
namespace bts::md::deribit {
struct DeribitMarketDataSubscription {};
} // namespace bts::md::deribit
-32
View File
@@ -1,32 +0,0 @@
#pragma once
#include <vector>
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<Security const *> &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<TradeRecord const *> &trades) = 0;
virtual void on_rfqs(const std::vector<RFQ const *> &rfqs) = 0;
virtual void on_error(const MarketDataError &error) = 0;
};
} // namespace EmumbaMarketData
-235
View File
@@ -1,235 +0,0 @@
#pragma once
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <ostream>
#include <string>
#include <variant>
#include <vector>
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 Tag, class ValueT>
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<struct QuantityTag, std::uint32_t>;
using SecurityID = ValueWrapper<struct SecuirtyIDTag, std::uint64_t>;
using AccountID = ValueWrapper<struct AccountIDTag, std::uint64_t>;
using UserID = ValueWrapper<struct UserIDTag, std::uint64_t>;
using MarketDataListenerID =
ValueWrapper<struct MarketDataListenedIDTag, std::uint32_t>;
using OrderKey = ValueWrapper<struct OrderKeyTag, std::uint64_t>;
// 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<std::int64_t>::min();
}
inline constexpr double to_double() const noexcept {
return is_nan() ? std::numeric_limits<double>::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<GlbxMarketDataSubscription, DeribitMarketDataSubscription>
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<LegDefinition> legs;
};
struct Security {
SecurityCommon security_common;
std::variant<IndexSecurity, FutureSecurity, SpreadSecurity> security;
SecurityType security_type() const {
if (std::holds_alternative<IndexSecurity>(security)) {
return SecurityType::INDEX;
} else if (std::holds_alternative<FutureSecurity>(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<BookLevel, 10> 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
+13 -7
View File
@@ -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 <optional>
#include <string>
#include <vector>
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<MarketDataListenerID> listener_id;
std::optional<bts::md::MarketDataListenerID> 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<SecurityID> &securities);
void unregister_listener(MarketDataListenerID id);
bts::md::MarketDataListenerID id,
const std::vector<bts::domain::SecurityID> &securities);
void unregister_listener(bts::md::MarketDataListenerID id);
};
} // namespace EmumbaMarketData
} // namespace EmumbaMarketData
@@ -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"
@@ -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<int>(id.value());
EmumbaSecurityData *rv = sec_map.get_security(int_id);
bts_assert(rv != nullptr);
@@ -99,20 +110,16 @@ EmumbaMDListener::EmumbaMDListener(
: me(std::make_unique<PImpl>(index, md_producer)) {}
void EmumbaMDListener::on_securities(
const std::vector<EmumbaMarketData::Security const *>
&complete_security_list) {
for (EmumbaMarketData::Security const *s : complete_security_list) {
if (std::holds_alternative<EmumbaMarketData::IndexSecurity>(
s->security)) {
const std::vector<domain::Security const *> &complete_security_list) {
for (domain::Security const *s : complete_security_list) {
if (std::holds_alternative<domain::IndexSecurity>(s->security)) {
_add_index(*s);
} else if (std::holds_alternative<EmumbaMarketData::FutureSecurity>(
s->security)) {
} else if (std::holds_alternative<domain::FutureSecurity>(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<EmumbaMarketData::TradeRecord const *> &latest_trades) {
for (EmumbaMarketData::TradeRecord const *t : latest_trades) {
const std::vector<domain::TradeRecord const *> &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<EmumbaMarketData::RFQ const *> & /*unused*/) {
const std::vector<domain::RFQ const *> & /*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<EmumbaMarketData::FutureSecurity>(sec.security);
const auto &future_sec = std::get<domain::FutureSecurity>(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);
@@ -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<PImpl> me;
public:
EmumbaMDListener(EmumbaSecurityIndex &index, MDProducer &md_producer);
void on_securities(const std::vector<EmumbaMarketData::Security const *>
void on_securities(const std::vector<domain::Security const *>
&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<EmumbaMarketData::TradeRecord const *>
&trades) override;
void on_rfqs(
const std::vector<EmumbaMarketData::RFQ const *> &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<domain::TradeRecord const *> &trades) override;
void on_rfqs(const std::vector<domain::RFQ const *> &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
} // namespace bts::md