'Moved' book and accompaying classes over
This commit is contained in:
1 parent
04629f28e8
commit
3f37777eb9
6 files changed
+534
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
//#include "lib/config/protos/market_data.pb.h"
|
||||
//#include "lib/domain_model/common.h"
|
||||
#include "PriceSize.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
using BookSide = std::array<PriceSize, 5>;
|
||||
|
||||
class Book {
|
||||
public:
|
||||
Book() : _security_key(), _book() {}
|
||||
|
||||
Book(SecurityKey key) : _security_key(key), _book() {}
|
||||
|
||||
Book(SecurityKey key, BookSide const &bid, BookSide const &ask)
|
||||
: _security_key(key), _book{bid, ask} {}
|
||||
|
||||
Book(Book const &rhs) : _security_key(rhs._security_key), _book(rhs._book) {}
|
||||
|
||||
Book &operator=(Book const &rhs) {
|
||||
_security_key = rhs._security_key;
|
||||
_book = rhs._book;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SecurityKey security_key() const { return _security_key; };
|
||||
|
||||
BookSide const &bid() const { return _book[0]; }
|
||||
BookSide const &ask() const { return _book[1]; }
|
||||
BookSide &bid() { return _book[0]; }
|
||||
BookSide &ask() { return _book[1]; }
|
||||
|
||||
size_t n_levels() const { return std::tuple_size<BookSide>(); }
|
||||
|
||||
BookSide const &side(api::Side bs) const {
|
||||
bts_assert_d(bs != api::SIDE_UNDEFINED);
|
||||
return _book[bs - api::SIDE_BUY];
|
||||
}
|
||||
|
||||
BookSide &side(api::Side bs) {
|
||||
bts_assert_d(bs != api::SIDE_UNDEFINED);
|
||||
return _book[bs - api::SIDE_BUY];
|
||||
}
|
||||
|
||||
private:
|
||||
// friend class MDSource;
|
||||
SecurityKey _security_key;
|
||||
std::array<BookSide, 2> _book;
|
||||
};
|
||||
|
||||
} // namespace spreader
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,207 @@
|
||||
#pragma once
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
const extern double NO_PRICE;
|
||||
|
||||
static const double powers_10[] = {
|
||||
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10,
|
||||
1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, 1E0, 1E1,
|
||||
1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10, 1E11, 1E12,
|
||||
1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20};
|
||||
static const int max_abs_power_10 = BTS_ARRAYSIZE(powers_10) / 2;
|
||||
inline double pow10(int power) {
|
||||
const int offset = power + max_abs_power_10;
|
||||
const int powers_10_sz = BTS_ARRAYSIZE(powers_10);
|
||||
bts_assert_d(offset >= 0 && offset < powers_10_sz);
|
||||
if (offset >= 0 && offset < powers_10_sz) {
|
||||
return powers_10[offset];
|
||||
}
|
||||
return std::numeric_limits<double>().quiet_NaN();
|
||||
}
|
||||
|
||||
class IntPrice {
|
||||
#ifdef _WIN32
|
||||
int64_t _mantissa : 56;
|
||||
int64_t _exponent : 8;
|
||||
#else
|
||||
int64_t _mantissa : 56;
|
||||
int8_t _exponent : 8;
|
||||
#endif
|
||||
|
||||
public:
|
||||
constexpr IntPrice() : _mantissa(0), _exponent(127) { /*nan*/
|
||||
}
|
||||
constexpr IntPrice(int64_t mantissa, int8_t exponent)
|
||||
: _mantissa(mantissa), _exponent(exponent) {}
|
||||
constexpr IntPrice(const IntPrice &other)
|
||||
: _mantissa(other._mantissa), _exponent(other._exponent) {}
|
||||
|
||||
constexpr IntPrice &operator=(const IntPrice &other) = default;
|
||||
|
||||
bool operator==(const IntPrice &other) const {
|
||||
if (_exponent == other._exponent) {
|
||||
return _mantissa == other._mantissa;
|
||||
} else {
|
||||
return price() == other.price();
|
||||
}
|
||||
}
|
||||
bool operator!=(const IntPrice &other) const { return !(*this == other); }
|
||||
bool operator<(const IntPrice &other) const {
|
||||
if (_exponent == other._exponent) {
|
||||
return _mantissa < other._mantissa;
|
||||
} else {
|
||||
// avoiding conversion to double is 4-5% faster
|
||||
// return price() < other.price();
|
||||
auto delta = *this - other;
|
||||
return delta.mantissa() < 0;
|
||||
}
|
||||
}
|
||||
bool operator<=(const IntPrice &other) const {
|
||||
return *this == other || *this < other;
|
||||
}
|
||||
bool operator>(const IntPrice &other) const { return other < *this; }
|
||||
bool operator>=(const IntPrice &other) const { return !(*this < other); }
|
||||
IntPrice operator-(const IntPrice &o) const {
|
||||
return op(o, [](int64_t m1, int64_t m2) { return m1 - m2; });
|
||||
}
|
||||
IntPrice operator+(const IntPrice &o) const {
|
||||
return op(o, [](int64_t m1, int64_t m2) { return m1 + m2; });
|
||||
}
|
||||
IntPrice &operator+=(const IntPrice &o) {
|
||||
auto temp = *this + o;
|
||||
_mantissa = temp._mantissa;
|
||||
_exponent = temp._exponent;
|
||||
return *this;
|
||||
}
|
||||
IntPrice &operator-=(const IntPrice &o) {
|
||||
auto temp = *this - o;
|
||||
_mantissa = temp._mantissa;
|
||||
_exponent = temp._exponent;
|
||||
return *this;
|
||||
}
|
||||
IntPrice operator-() { return IntPrice(-_mantissa, _exponent); }
|
||||
IntPrice operator*(const IntPrice &other) const {
|
||||
return op(other, [](int64_t m1, int64_t m2) { return m1 * m2; });
|
||||
}
|
||||
IntPrice operator*(const int &other) const {
|
||||
return IntPrice(_mantissa * other, _exponent);
|
||||
}
|
||||
IntPrice &operator*=(const int &other) {
|
||||
_mantissa *= other;
|
||||
return *this;
|
||||
}
|
||||
IntPrice operator/(double v) const {
|
||||
return {(int64_t)(_mantissa / v), exponent()};
|
||||
}
|
||||
|
||||
IntPrice sign_extend(int8_t new_exponent) const {
|
||||
int8_t delta = _exponent - new_exponent;
|
||||
int64_t m = _mantissa * pow10(delta);
|
||||
return {m, new_exponent};
|
||||
}
|
||||
|
||||
int64_t mantissa() const { return _mantissa; }
|
||||
int8_t exponent() const { return _exponent; }
|
||||
double price() const {
|
||||
if (is_nan())
|
||||
return NO_PRICE;
|
||||
return mantissa() * pow10(exponent());
|
||||
}
|
||||
bool is_nan() const { return _exponent == 127; }
|
||||
|
||||
private:
|
||||
template <class MantissaOp>
|
||||
IntPrice op(const IntPrice &o, MantissaOp operation) const {
|
||||
if (_exponent == o._exponent) {
|
||||
return IntPrice{operation(_mantissa, o.mantissa()), exponent()};
|
||||
} else {
|
||||
if (is_nan() || o.is_nan())
|
||||
return IntPrice();
|
||||
bts_assert_d(o.exponent() <= 0 && _exponent <= 0);
|
||||
if (o.exponent() < _exponent) {
|
||||
int8_t delta = _exponent - o.exponent();
|
||||
int64_t m = _mantissa * pow10(delta);
|
||||
return {operation(m, o.mantissa()), o.exponent()};
|
||||
} else {
|
||||
int8_t delta = o.exponent() - _exponent;
|
||||
int64_t m = o.mantissa() * pow10(delta);
|
||||
return {operation(_mantissa, m), exponent()};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(IntPrice) == 8, "IntPrice should be this big |--------|");
|
||||
extern IntPrice INT_NAN;
|
||||
extern IntPrice INT_ZERO;
|
||||
|
||||
template <uint8_t PRECISION>
|
||||
IntPrice IntPrice_from_double(double price) {
|
||||
if (std::isnan(price))
|
||||
return INT_NAN;
|
||||
double temp;
|
||||
if (fabs(modf(price, &temp)) != 0.0) {
|
||||
const double rounding = 0.5 * pow10(-PRECISION);
|
||||
price += (price > 0) ? rounding : -rounding;
|
||||
price *= pow10(PRECISION);
|
||||
return {(int64_t)price, -PRECISION};
|
||||
}
|
||||
return {(int64_t)price, 0};
|
||||
}
|
||||
// inline IntPrice IntPrice_from_double(double price, int8_t precision) {
|
||||
// if (os::is_nan(price)) return INT_NAN;
|
||||
// return { (int64_t)(price * pow10(-precision)), precision };
|
||||
// }
|
||||
|
||||
inline bts::spreader::IntPrice round_to_quote_price(
|
||||
const IntPrice &tick, const IntPrice &price, api::Side side) {
|
||||
bts_assert_d(tick.mantissa() != 0);
|
||||
if (price.is_nan())
|
||||
return price;
|
||||
bts_assert_d(price.exponent() <= 0 && tick.exponent() <= 0);
|
||||
if (price.exponent() == tick.exponent()) {
|
||||
auto mod = price.mantissa() % tick.mantissa();
|
||||
if (mod == 0)
|
||||
return price;
|
||||
else if (side == api::SIDE_BUY) {
|
||||
// buy, round down
|
||||
if (price.mantissa() > 0) {
|
||||
return {price.mantissa() - mod, price.exponent()};
|
||||
} else {
|
||||
return {price.mantissa() - mod - tick.mantissa(), price.exponent()};
|
||||
}
|
||||
} else if (side == api::SIDE_SELL) {
|
||||
// sell, round up
|
||||
if (price.mantissa() > 0) {
|
||||
return {price.mantissa() - mod + tick.mantissa(), price.exponent()};
|
||||
} else {
|
||||
return {price.mantissa() - mod, price.exponent()};
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Uncommon path
|
||||
// Math is reversed because exponents are negative
|
||||
if (tick.exponent() < price.exponent()) {
|
||||
return round_to_quote_price(
|
||||
tick, price.sign_extend(tick.exponent()), side);
|
||||
} else {
|
||||
// Extend tick to price's exponent
|
||||
return round_to_quote_price(
|
||||
tick.sign_extend(price.exponent()), price, side);
|
||||
}
|
||||
}
|
||||
|
||||
return price;
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &os, const IntPrice &p) {
|
||||
os << p.price();
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace spreader
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
|
||||
#include "IntPrice.h"
|
||||
#include "ValueWrapperT.h"
|
||||
|
||||
//#include "lib/os/os.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
typedef double Price;
|
||||
typedef uint32_t Size;
|
||||
|
||||
int to_exponent(double value);
|
||||
|
||||
class PriceSize {
|
||||
IntPrice _price;
|
||||
// merged size
|
||||
uint32_t _size;
|
||||
uint32_t _direct_size;
|
||||
|
||||
public:
|
||||
PriceSize(IntPrice price, Size size)
|
||||
: _price(size == 0 ? INT_NAN : price), _size(size), _direct_size(size) {}
|
||||
PriceSize(IntPrice price, Size direct_size, ImpliedSize implied_size)
|
||||
: _price(price), _size(direct_size + implied_size.value()),
|
||||
_direct_size(direct_size) {}
|
||||
PriceSize() : _price(INT_NAN), _size(0), _direct_size(0) {}
|
||||
|
||||
PriceSize(const PriceSize &other) = default;
|
||||
PriceSize &operator=(const PriceSize &other) = default;
|
||||
|
||||
bool operator==(const PriceSize &other) const {
|
||||
return (
|
||||
_size == other._size && _direct_size == other._direct_size &&
|
||||
int_price() == other.int_price());
|
||||
}
|
||||
bool operator!=(const PriceSize &other) const { return !(*this == other); }
|
||||
PriceSize operator-(IntPrice price) const {
|
||||
PriceSize res = *this;
|
||||
res._price = res._price - price;
|
||||
return res;
|
||||
}
|
||||
PriceSize get_divided_price(double divisor) const {
|
||||
PriceSize res = *this;
|
||||
res._price = res._price / divisor;
|
||||
return res;
|
||||
}
|
||||
//
|
||||
// Accessors
|
||||
|
||||
int64_t mantissa() const { return _price.mantissa(); }
|
||||
int8_t exponent() const { return _price.exponent(); }
|
||||
Size size() const { return _size; }
|
||||
Size direct_size() const { return _direct_size; }
|
||||
Size implied_size() const { return _size - _direct_size; }
|
||||
double price() const { return _price.price(); }
|
||||
IntPrice int_price() const { return _price; }
|
||||
bool is_nan() const { return _price.is_nan(); }
|
||||
};
|
||||
extern PriceSize NO_MARKET;
|
||||
static_assert(sizeof(PriceSize) == 16, "PriceSize should be small");
|
||||
|
||||
template <uint8_t PRECISION = 11>
|
||||
PriceSize PriceSize_from_double(double price, Size size) {
|
||||
if (size == 0 || std::isnan(price))
|
||||
return NO_MARKET;
|
||||
return {IntPrice_from_double<PRECISION>(price), size};
|
||||
}
|
||||
|
||||
//
|
||||
// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad
|
||||
// out to 16 bytes This structure can store the same data in 24 bytes so it's
|
||||
// more efficient for shared memory
|
||||
struct PriceLevel {
|
||||
PriceLevel() : _bid(), _ask(), _bid_size(0), _ask_size(0) {}
|
||||
PriceLevel(const PriceSize &bid, const PriceSize &ask)
|
||||
: _bid(bid.int_price()), _ask(ask.int_price()), _bid_size(bid.size()),
|
||||
_ask_size(ask.size()) {}
|
||||
|
||||
IntPrice _bid;
|
||||
IntPrice _ask;
|
||||
uint32_t _bid_size;
|
||||
uint32_t _ask_size;
|
||||
|
||||
const IntPrice &bid_price() const { return _bid; }
|
||||
const IntPrice &ask_price() const { return _ask; }
|
||||
uint32_t bid_size() const { return _bid_size; }
|
||||
uint32_t ask_size() const { return _ask_size; }
|
||||
|
||||
bool operator==(const PriceLevel &other) const {
|
||||
return _ask_size == other._ask_size && _bid_size == other._bid_size &&
|
||||
_bid == other._bid && _ask == other._ask;
|
||||
}
|
||||
bool operator!=(const PriceLevel &other) const {
|
||||
return _ask_size != other._ask_size || _bid_size != other._bid_size ||
|
||||
_bid != other._bid || _ask != other._ask;
|
||||
}
|
||||
|
||||
bool update(const PriceLevel &temp) {
|
||||
if (*this != temp) {
|
||||
*this = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
PriceSize bid() const { return PriceSize(_bid, _bid_size); }
|
||||
PriceSize ask() const { return PriceSize(_ask, _ask_size); }
|
||||
};
|
||||
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
|
||||
|
||||
struct ImpliedPriceLevel : private PriceLevel {
|
||||
ImpliedPriceLevel() : PriceLevel() {}
|
||||
ImpliedPriceLevel(const PriceSize &bid, const PriceSize &ask)
|
||||
: PriceLevel(bid, ask) {}
|
||||
using PriceLevel::ask_price;
|
||||
using PriceLevel::ask_size;
|
||||
using PriceLevel::bid_price;
|
||||
using PriceLevel::bid_size;
|
||||
using PriceLevel::operator==;
|
||||
using PriceLevel::operator!=;
|
||||
bool operator=(const PriceLevel &other) { return update(other); }
|
||||
PriceSize bid() const { return PriceSize(_bid, 0, ImpliedSize(_bid_size)); }
|
||||
PriceSize ask() const { return PriceSize(_ask, 0, ImpliedSize(_ask_size)); }
|
||||
};
|
||||
static_assert(
|
||||
sizeof(ImpliedPriceLevel) == sizeof(PriceLevel),
|
||||
"ImpliedPriceLevel should not be bigger than PriceLevel");
|
||||
|
||||
} // namespace spreader
|
||||
} // namespace bts
|
||||
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
class ValueWrapper {
|
||||
public:
|
||||
typedef ValueT Value;
|
||||
|
||||
constexpr ValueWrapper() = default;
|
||||
constexpr explicit ValueWrapper(const ValueT &v);
|
||||
constexpr explicit ValueWrapper(ValueT &&v);
|
||||
|
||||
constexpr ValueWrapper(const ValueWrapper &v) = default;
|
||||
constexpr ValueWrapper(ValueWrapper &&v) = default;
|
||||
|
||||
ValueWrapper &operator=(ValueWrapper const &v) = default;
|
||||
ValueWrapper &operator=(ValueWrapper &&v) = default;
|
||||
|
||||
constexpr const ValueT &value() const;
|
||||
constexpr ValueT &value();
|
||||
|
||||
private:
|
||||
ValueT v_;
|
||||
};
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator==(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator!=(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator>(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator>=(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator<(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr bool operator<=(
|
||||
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
|
||||
|
||||
template <class Tag, class V>
|
||||
std::ostream &operator<<(std::ostream &s, ValueWrapper<Tag, V> const &v);
|
||||
|
||||
using SecurityKey = ValueWrapper<struct SecurityKeyTag, uint32_t>;
|
||||
using ImpliedSize = ValueWrapper<struct ImpliedSizeTag, uint32_t>;
|
||||
|
||||
} // namespace spreader
|
||||
} // namespace bts
|
||||
|
||||
namespace std {
|
||||
|
||||
template <typename Tag, typename Value>
|
||||
struct hash<bts::spreader::ValueWrapper<Tag, Value>> {
|
||||
std::size_t operator()(
|
||||
const bts::spreader::ValueWrapper<Tag, Value> &k) const;
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr ValueWrapper<Tag, ValueT>::ValueWrapper(const ValueT &v) : v_(v) {}
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr ValueWrapper<Tag, ValueT>::ValueWrapper(ValueT &&v)
|
||||
: v_(std::move(v)) {}
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr ValueT const &ValueWrapper<Tag, ValueT>::value() const {
|
||||
return v_;
|
||||
}
|
||||
|
||||
template <class Tag, class ValueT>
|
||||
constexpr ValueT &ValueWrapper<Tag, ValueT>::value() {
|
||||
return v_;
|
||||
}
|
||||
|
||||
#define BIN_OP(result_t, op) \
|
||||
template <class Tag, class ValueT> \
|
||||
constexpr result_t operator op( \
|
||||
const ValueWrapper<Tag, ValueT> &lhs, \
|
||||
const ValueWrapper<Tag, ValueT> &rhs) { \
|
||||
return result_t(lhs.value() op rhs.value()); \
|
||||
}
|
||||
|
||||
BIN_OP(bool, ==)
|
||||
BIN_OP(bool, !=)
|
||||
BIN_OP(bool, >)
|
||||
BIN_OP(bool, >=)
|
||||
BIN_OP(bool, <)
|
||||
BIN_OP(bool, <=)
|
||||
|
||||
#undef BIN_OP
|
||||
|
||||
template <class Tag, class V>
|
||||
std::ostream &operator<<(std::ostream &s, ValueWrapper<Tag, V> const &v) {
|
||||
s << v.value();
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace spreader
|
||||
} // namespace bts
|
||||
|
||||
namespace std {
|
||||
|
||||
template <typename Tag, typename Value>
|
||||
std::size_t hash<bts::spreader::ValueWrapper<Tag, Value>>::operator()(
|
||||
const bts::spreader::ValueWrapper<Tag, Value> &k) const {
|
||||
return hash<Value>()(k.value());
|
||||
}
|
||||
|
||||
} // namespace std
|
||||
+4
@@ -1,6 +1,10 @@
|
||||
#include "LegOrderRouter.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/orders/spreader_orders/market_data/Book.h"
|
||||
#include "lib/orderman/orders/spreader_orders/market_data/IntPrice.h"
|
||||
#include "lib/orderman/orders/spreader_orders/market_data/PriceSize.h"
|
||||
#include "lib/orderman/orders/spreader_orders/market_data/ValueWrapperT.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/IOrderGateway.h"
|
||||
|
||||
namespace bts::orderman {} // namespace bts::orderman
|
||||
Reference in new issue
Block a user