diff --git a/exchange/src/common/types/decimal.cpp b/exchange/src/common/types/decimal.cpp index 700a9a75..282fa96d 100644 --- a/exchange/src/common/types/decimal.cpp +++ b/exchange/src/common/types/decimal.cpp @@ -6,7 +6,7 @@ template Decimal Decimal::operator-() const { - return -value_; + return Decimal{-value_}; } template @@ -27,28 +27,28 @@ template Decimal Decimal::operator-(const Decimal& other) const { - return value_ - other.value_; + return Decimal{value_ - other.value_}; } template Decimal Decimal::operator+(const Decimal& other) const { - return value_ + other.value_; + return Decimal{value_ + other.value_}; } template Decimal Decimal::operator/(const Decimal& other) const { - return value_ * MULTIPLIER / other.value_; + return Decimal{value_ * MULTIPLIER / other.value_}; } template Decimal Decimal::operator*(const Decimal& other) const { - return (value_ * other.value_) / MULTIPLIER; + return Decimal{(value_ * other.value_) / MULTIPLIER}; } template @@ -101,9 +101,9 @@ Decimal Decimal::difference(const Decimal& other) const { if (value_ >= other.value_) { - return value_ - other.value_; + return Decimal{value_ - other.value_}; } - return other.value_ - value_; + return Decimal{other.value_ - value_}; } template class Decimal; @@ -111,4 +111,5 @@ template class Decimal; #if PRICE_DECIMAL_PLACES != QUANTITY_DECIMAL_PLACES template class Decimal; #endif + } // namespace nutc::common diff --git a/exchange/src/common/types/decimal.hpp b/exchange/src/common/types/decimal.hpp index 4c69a83c..ba738673 100644 --- a/exchange/src/common/types/decimal.hpp +++ b/exchange/src/common/types/decimal.hpp @@ -24,6 +24,7 @@ pow10(int pow) } } // namespace detail +// THIS CLASS PRIORITIZES PRECISION OVER AVOIDING OVERFLOW template class Decimal { using decimal_type = std::int64_t; @@ -56,7 +57,7 @@ class Decimal { Decimal difference(const Decimal& other) const; private: - constexpr Decimal(decimal_type value) : value_(value) {} + constexpr explicit Decimal(decimal_type value) : value_(value) {} static constexpr bool double_within_bounds(double value) @@ -110,19 +111,23 @@ struct hash> { // TODO: add unit tests template class numeric_limits> { + using scaled_decimal = nutc::common::Decimal; + public: - static nutc::common::Decimal + static scaled_decimal max() { - return std::numeric_limits< - typename nutc::common::Decimal::decimal_type>::max(); + return scaled_decimal{ + std::numeric_limits::max() + }; } - static nutc::common::Decimal + static scaled_decimal min() { - return std::numeric_limits< - typename nutc::common::Decimal::decimal_type>::min(); + return scaled_decimal{ + std::numeric_limits::min() + }; } }; diff --git a/exchange/src/exchange/bots/bot_container.cpp b/exchange/src/exchange/bots/bot_container.cpp index 316457cb..d6584c06 100644 --- a/exchange/src/exchange/bots/bot_container.cpp +++ b/exchange/src/exchange/bots/bot_container.cpp @@ -18,7 +18,7 @@ BotContainer::generate_orders( { variance_calculator_.record_price(midprice); - decimal_price cumulative_interest_limit{}; + common::decimal_price cumulative_interest_limit{}; common::decimal_quantity cumulative_quantity_held{}; for (const auto& bot : bots_) { diff --git a/exchange/src/exchange/bots/bot_types/market_maker.cpp b/exchange/src/exchange/bots/bot_types/market_maker.cpp index 81626cac..fa3d8197 100644 --- a/exchange/src/exchange/bots/bot_types/market_maker.cpp +++ b/exchange/src/exchange/bots/bot_types/market_maker.cpp @@ -64,8 +64,9 @@ MarketMakerBot::place_orders_( decimal_price MarketMakerBot::calculate_lean_percent(const shared_bot_state& state) { - auto lean_price = state.CUMULATIVE_QUANTITY_HELD / state.CUMULATIVE_INTEREST_LIMIT; - return lean_price * state.MIDPRICE; + double ratio = double{state.CUMULATIVE_QUANTITY_HELD} + / double{state.CUMULATIVE_INTEREST_LIMIT}; + return ratio * static_cast(state.MIDPRICE); } // TODO: clean up diff --git a/exchange/test/src/unit/types/decimal.cpp b/exchange/test/src/unit/types/decimal.cpp index a6b58452..eaa1b641 100644 --- a/exchange/test/src/unit/types/decimal.cpp +++ b/exchange/test/src/unit/types/decimal.cpp @@ -18,6 +18,19 @@ TEST(UnitDecimalPrice, AssignmentOperator) EXPECT_EQ(second, 5.0); } +TEST(UnitDecimalPrice, TestCommutativity) +{ + decimal_price three = 3.0; + decimal_price four = 4.0; + decimal_price six = 6.0; + + decimal_price twelve = three*four; + EXPECT_EQ(double{twelve/six}, 2.0); + + decimal_price point_five = three/six; + EXPECT_EQ(double{point_five*four}, 2.0); +} + TEST(UnitDecimalPrice, UnaryNegate) { decimal_price first = 5.0; @@ -45,8 +58,8 @@ TEST(UnitDecimalPrice, DecimalDecimalDivision) { decimal_price first = 10.0; decimal_price second = 2.0; - double third{first / second}; - EXPECT_EQ(third, 5.0); + decimal_price third = first/second; + EXPECT_EQ(double{third}, 5.0); } TEST(UnitDecimalPrice, DecimalDecimalMultiplication)