diff --git a/src/lib/domain/Price.h b/src/lib/domain/Price.h index 52b28fc95..50d2e348c 100644 --- a/src/lib/domain/Price.h +++ b/src/lib/domain/Price.h @@ -54,14 +54,22 @@ class Price { [[nodiscard]] constexpr bool is_nan() const noexcept; [[nodiscard]] constexpr bool is_better(Side s, Price p2) const noexcept; + [[nodiscard]] static constexpr bool is_better( + Side s, double p1, double p2) noexcept; [[nodiscard]] constexpr bool is_worse(Side s, Price p2) const noexcept; + [[nodiscard]] static constexpr bool is_worse( + Side s, double p1, double p2) noexcept; [[nodiscard]] constexpr bool is_better_or_equal( Side s, Price p2) const noexcept; + [[nodiscard]] static constexpr bool is_better_or_equal( + Side s, double p1, double p2) noexcept; [[nodiscard]] constexpr bool is_worse_or_equal( Side s, Price p2) const noexcept; + [[nodiscard]] static constexpr bool is_worse_or_equal( + Side s, double p1, double p2) noexcept; [[nodiscard]] constexpr Price repl_nan(Price repl) const noexcept; @@ -233,10 +241,27 @@ constexpr bool Price::is_better(Side s, Price p2) const noexcept { return *this < p2; } +constexpr bool Price::is_better(Side s, double p1, double p2) noexcept { + if (std::isnan(p1)) { + return false; + } + if (std::isnan(p2)) { + return true; + } + if (s == Side::Buy) { + return p1 > p2; + } + return p1 < p2; +} + constexpr bool Price::is_worse(Side s, Price p2) const noexcept { return p2.is_better(s, *this); } +constexpr bool Price::is_worse(Side s, double p1, double p2) noexcept { + return is_better(s, p2, p1); +} + 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. @@ -251,10 +276,29 @@ constexpr bool Price::is_better_or_equal(Side s, Price p2) const noexcept { return *this <= p2; } +constexpr bool Price::is_better_or_equal( + Side s, double p1, double p2) noexcept { + if (std::isnan(p1)) { + // This case is always false (even if p2 is nan), because nan != nan. + return false; + } + if (std::isnan(p2)) { + return true; + } + if (s == Side::Buy) { + return p1 >= p2; + } + return p1 <= p2; +} + constexpr bool Price::is_worse_or_equal(Side s, Price p2) const noexcept { return p2.is_better_or_equal(s, *this); } +constexpr bool Price::is_worse_or_equal(Side s, double p1, double p2) noexcept { + return is_better_or_equal(s, p2, p1); +} + constexpr Price Price::repl_nan(Price repl) const noexcept { if (is_nan()) { return repl; diff --git a/src/lib/domain/PriceComp.h b/src/lib/domain/PriceComp.h index 09f843b69..c27c7fb33 100644 --- a/src/lib/domain/PriceComp.h +++ b/src/lib/domain/PriceComp.h @@ -10,6 +10,7 @@ struct PriceComp { constexpr PriceComp(const PriceComp &other) = default; constexpr bool operator()(const Price &lhs, const Price &rhs); + constexpr bool operator()(double lhs, double rhs); private: Side _side = Side::Buy; @@ -25,4 +26,8 @@ constexpr bool PriceComp::operator()(const Price &lhs, const Price &rhs) { return lhs.is_better(_side, rhs); } +constexpr bool PriceComp::operator()(double lhs, double rhs) { + return Price::is_better(_side, lhs, rhs); +} + } // namespace bts::domain