Files
spark/src/lib/md/IntPrice.h
T
2019-04-18 14:39:15 -04:00

186 lines
6.1 KiB
C++

#pragma once
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_array_size.h"
#include "lib/domain_model/Security.pb.h"
namespace bts {
namespace md {
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:
IntPrice() : _mantissa(0), _exponent(127) {/*nan*/}
IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {}
IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {}
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) {
return *this == other || *this < other;
}
bool operator >=(const IntPrice& other) {
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& other) const {
return op(other, [](int64_t m1, int64_t m2) { return m1 * m2; });
}
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 (os::is_nan(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 md::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;
}
}}