diff --git a/src/lib/orderman/orderman_utils/PriceComp.h b/src/lib/orderman/orderman_utils/PriceComp.h index c23d9f4c7..d68eb566a 100644 --- a/src/lib/orderman/orderman_utils/PriceComp.h +++ b/src/lib/orderman/orderman_utils/PriceComp.h @@ -64,6 +64,31 @@ inline bool is_better_or_equal( return price == other || is_better(side, price, other); } +template <> +inline bool is_better( + api::Side side, spartan::mdp3::MDP3Price price, spartan::mdp3::MDP3Price other) { + if (price.is_nan()) + return false; + if (other.is_nan()) + return true; + switch (side) { + case api::Side::SIDE_BUY: + return price > other; + case api::Side::SIDE_SELL: + return price < other; + default: + bts_assert_msg( + false, + "invalid price comparison, specified side other than Buy or Sell."); + return false; + } +} + +template <> +inline bool is_better_or_equal( + api::Side side, spartan::mdp3::MDP3Price price, spartan::mdp3::MDP3Price other) { + return price == other || is_better(side, price, other); +} // This method is basically syntactic sugar; // It is another name for the 'is_better_or_equal' method. template @@ -71,6 +96,16 @@ inline bool is_cross(api::Side aggr_side, T aggr_price, T passive_price) { return is_better_or_equal(aggr_side, aggr_price, passive_price); } +inline bool is_equal(spartan::mdp3::MDP3Price a, spartan::mdp3::MDP3Price b) { + if (a.is_nan()) + return b.is_nan(); + if (b.is_nan()) + return false; + double diff = a.price() - b.price(); + return diff < securities::market_epsilon() && + diff > -securities::market_epsilon(); +} + inline bool is_equal(md::IntPrice a, md::IntPrice b) { if (a.is_nan()) return b.is_nan(); @@ -113,6 +148,9 @@ struct SidePriceComp { return price_comp::is_better(_side, lhs, rhs); } + bool operator()(const spartan::mdp3::MDP3Price &lhs, const spartan::mdp3::MDP3Price &rhs) { + return price_comp::is_better(_side, lhs, rhs); + } bool operator()( const spartan::mdp3::SmallBookLevel &lhs, const spartan::mdp3::SmallBookLevel &rhs) { @@ -123,4 +161,4 @@ struct SidePriceComp { api::Side _side; }; } // namespace orderman -} // namespace bts \ No newline at end of file +} // namespace bts diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index 2bfe57bba..e267d4fa0 100644 --- a/src/server/sparkd/CMakeDeps.cmake +++ b/src/server/sparkd/CMakeDeps.cmake @@ -1 +1 @@ -set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/back_testing/algos/PolyObserver.cpp;cmds/spartan/back_testing/algos/SniperModel.cpp;cmds/spartan/back_testing/algos/spreader/SimOrderGateway.cpp;cmds/spartan/back_testing/algos/spreader/SimSpreader.cpp;cmds/spartan/back_testing/algos/spreader/tests/sim_spreader_tests.cpp;cmds/spartan/back_testing/db/SimDB.cpp;cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp;cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/Matcher.cpp;cmds/spartan/back_testing/matcher/MatchingCommon.cpp;cmds/spartan/back_testing/matcher/fill_depot.cpp;cmds/spartan/back_testing/matcher/iceberg_helper.cpp;cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp;cmds/spartan/back_testing/matcher/validation/iceberg_file_cache.cpp;cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.cpp;cmds/spartan/back_testing/run/SimSessionOptions.proto;cmds/spartan/back_testing/run/run_sim.cpp;cmds/spartan/back_testing/sim/ISim.cpp;cmds/spartan/back_testing/sim/Sim.cpp;cmds/spartan/back_testing/sim/pandl/PandL.cpp;cmds/spartan/back_testing/sim/pcap/PCapEventGenerator.cpp;cmds/spartan/back_testing/sim/pcap/PCapEventSource.cpp;cmds/spartan/back_testing/sim/spark_bridge/AlgoServicesBridge.cpp;cmds/spartan/back_testing/sim/spark_bridge/BeforeLaunchBridge.cpp;cmds/spartan/common/IcebergQuantity.cpp;cmds/spartan/common/InstrumentDefinition.cpp;cmds/spartan/common/OrderKey.cpp;cmds/spartan/common/Quantity.cpp;cmds/spartan/common/SecurityID.cpp;cmds/spartan/common/TimeInterval.cpp;cmds/spartan/common/TimeStamp.cpp;cmds/spartan/md/mdp3/channel_observers/DefinitionCollector.cpp;cmds/spartan/md/mdp3/channel_observers/FillCounter.cpp;cmds/spartan/md/mdp3/channel_observers/IcebergFinder.cpp;cmds/spartan/md/mdp3/channel_observers/PolygonFinder.cpp;cmds/spartan/md/mdp3/channel_observers/PolygonOpportunityFinder.cpp;cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/common/mdp3_common.cpp;cmds/spartan/md/mdp3/common/tests/book_merging_tests.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/implied/implied_market.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/book_utils.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/order_utils.cpp;cmds/spartan/md/mdp3/utils/polygon_utils.cpp;cmds/spartan/md/mdp3/utils/security_comp.cpp;cmds/spartan/md/mdp3/utils/security_parser.cpp;cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp;cmds/spartan/md/mdp3/utils/tests/test_polygon_finder.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/action_summarizer.cpp;cmds/spartan/util/args_parser.cpp;cmds/spartan/util/sql/adapters/polygon_defs.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file +set(sources algo/AlgoDefinitionServiceServer.cpp;cmds/AdminReport.cpp;cmds/AutoCert.cpp;cmds/Benchmark.cpp;cmds/CMECertCmd.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/FIXSim.cpp;cmds/Help.cpp;cmds/KeyCode.cpp;cmds/Md.cpp;cmds/ParseHistoricalFIX.cpp;cmds/PrintHedgeOrders.cpp;cmds/QuickTest.cpp;cmds/Run.cpp;cmds/Span.cpp;cmds/Spartan.cpp;cmds/Test.cpp;cmds/Web.cpp;cmds/spartan/algo/tas/RunTAS.cpp;cmds/spartan/algo/tas/TASOrderReactor.cpp;cmds/spartan/algo/tas/TASTriggerDef.cpp;cmds/spartan/back_testing/algos/PolyObserver.cpp;cmds/spartan/back_testing/algos/SniperModel.cpp;cmds/spartan/back_testing/algos/spreader/SimOrderGateway.cpp;cmds/spartan/back_testing/algos/spreader/SimSpreader.cpp;cmds/spartan/back_testing/algos/spreader/tests/sim_spreader_tests.cpp;cmds/spartan/back_testing/db/SimDB.cpp;cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp;cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp;cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp;cmds/spartan/back_testing/matcher/Matcher.cpp;cmds/spartan/back_testing/matcher/MatchingCommon.cpp;cmds/spartan/back_testing/matcher/fill_depot.cpp;cmds/spartan/back_testing/matcher/iceberg_helper.cpp;cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp;cmds/spartan/back_testing/matcher/validation/iceberg_file_cache.cpp;cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.cpp;cmds/spartan/back_testing/run/SimSessionOptions.proto;cmds/spartan/back_testing/run/run_sim.cpp;cmds/spartan/back_testing/sim/ISim.cpp;cmds/spartan/back_testing/sim/Sim.cpp;cmds/spartan/back_testing/sim/pandl/PandL.cpp;cmds/spartan/back_testing/sim/pcap/PCapEventGenerator.cpp;cmds/spartan/back_testing/sim/pcap/PCapEventSource.cpp;cmds/spartan/back_testing/sim/spark_bridge/AlgoServicesBridge.cpp;cmds/spartan/back_testing/sim/spark_bridge/BeforeLaunchBridge.cpp;cmds/spartan/common/IcebergQuantity.cpp;cmds/spartan/common/InstrumentDefinition.cpp;cmds/spartan/common/MDP3Price.cpp;cmds/spartan/common/OrderKey.cpp;cmds/spartan/common/Quantity.cpp;cmds/spartan/common/SecurityID.cpp;cmds/spartan/common/TimeInterval.cpp;cmds/spartan/common/TimeStamp.cpp;cmds/spartan/md/mdp3/channel_observers/DefinitionCollector.cpp;cmds/spartan/md/mdp3/channel_observers/FillCounter.cpp;cmds/spartan/md/mdp3/channel_observers/IcebergFinder.cpp;cmds/spartan/md/mdp3/channel_observers/PolygonFinder.cpp;cmds/spartan/md/mdp3/channel_observers/PolygonOpportunityFinder.cpp;cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp;cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp;cmds/spartan/md/mdp3/common/mdp3_common.cpp;cmds/spartan/md/mdp3/common/tests/book_merging_tests.cpp;cmds/spartan/md/mdp3/feed_control/IFeedController.cpp;cmds/spartan/md/mdp3/handler/ChannelHandler.cpp;cmds/spartan/md/mdp3/implied/implied_market.cpp;cmds/spartan/md/mdp3/parsing/MDP3MsgBox.cpp;cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp;cmds/spartan/md/mdp3/recovery/BookSnapshot.cpp;cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.cpp;cmds/spartan/md/mdp3/recovery/IncrementalQueue.cpp;cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp;cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp;cmds/spartan/md/mdp3/utils/book_utils.cpp;cmds/spartan/md/mdp3/utils/mdp3_utils.cpp;cmds/spartan/md/mdp3/utils/order_utils.cpp;cmds/spartan/md/mdp3/utils/polygon_utils.cpp;cmds/spartan/md/mdp3/utils/security_comp.cpp;cmds/spartan/md/mdp3/utils/security_parser.cpp;cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp;cmds/spartan/md/mdp3/utils/tests/test_polygon_finder.cpp;cmds/spartan/util/MCastReceiver.cpp;cmds/spartan/util/PCapReader.cpp;cmds/spartan/util/action_summarizer.cpp;cmds/spartan/util/args_parser.cpp;cmds/spartan/util/sql/adapters/polygon_defs.cpp;dspark/mirage/quick_queue/quick_queue.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/book/Book.cpp;exchange_sim/book/ExchangeSim.proto;exchange_sim/book/Order.cpp;exchange_sim/book/OrderMatcher.cpp;exchange_sim/book/book_transform.cpp;exchange_sim/tests/test_book_transform.cpp;http/ApiGateway.cpp;http/MultipartFormDecoder.cpp;http/WebServer.cpp;http/tests/MultiPartFormTests.cpp;main.cpp;md/ChannelVolume.cpp;md/MarketDataAuxiliaryServiceServer.cpp;md/VolumeAtPriceState.cpp;md/platforms/IMDPlatform.cpp;md/platforms/ice_onixs/ICEListener.cpp;md/platforms/ice_onixs/ICEMain.cpp;md/platforms/ice_onixs/tests/ice_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/MarginServiceServer.cpp;position/PositionAggregator.cpp;position/PositionMap.cpp;position/PositionServicePNLHelper.cpp;position/PositionServiceServer.cpp;position/import/BasePositionServiceServer.cpp;position/import/DownloadCommonStateMachine.cpp;position/import/DownloadWindow.cpp;position/import/MoneyFileServiceServer.cpp;position/import/parsing/gmi_parser.cpp;position/import/parsing/money_parser.cpp;position/import/parsing/parser_common.cpp;position/tests/PositionTests.cpp;position/tests/position_tests_internal.cpp;subprocess/SubProcessRunner.cpp;support/ErrorReportService.cpp;support/ShellLatencyServer.cpp;user/AdminServiceDB.cpp;user/AdminServiceServer.cpp;user/AuthServiceServer.cpp;user/tests/admin_service_tests.cpp) \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp index bd1f8a3d8..759965a71 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.cpp @@ -140,7 +140,7 @@ void FifoImpliedSecurityMatcher::_book( auto leg_side = leg_qty.value() > 0 ? securities::other_side(side) : side; for (auto bl : sec_matcher->book(leg_side)) bk.emplace_back(bl.price, bl.quantity); - im_vec.emplace_back(leg_qty, md::INT_NAN, bk); + im_vec.emplace_back(leg_qty, MDP3Price{}, bk); } return book_utils::compute_synthetic_book(dest, im_vec, depth); } diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp index 61fcda354..e3b6f01f6 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp @@ -346,7 +346,7 @@ FifoSecurityMatcher::book_level(api::Side side, size_t idx) const { for (const auto &p_level : _book(side)) if (p_level.populated() && i++ == idx) return std::make_tuple(p_level.price(), p_level.total_quantity()); - return std::make_tuple(md::INT_NAN, Quantity(0)); + return std::make_tuple(MDP3Price{}, Quantity(0)); } void FifoSecurityMatcher::on_order_change( SmallOrderChange const &o, std::vector *res) { diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp index ee8b67f44..7068b0dca 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/tests/matcher_tests.cpp @@ -44,7 +44,7 @@ MDP3Order create_order( SecurityID(sec), side, MDP3OrderPriority(priority), - md::IntPrice_from_double<7>(price), + MDP3Price::from_double<7>(price), Quantity(qty), ib}; return rv; @@ -61,7 +61,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") { Trace trace; std::map master_ord_map; FifoPriceLevelMatcher price_level( - master_ord_map, SecurityID(0), md::IntPrice_from_double<7>(price), + master_ord_map, SecurityID(0), MDP3Price::from_double<7>(price), side, trace); auto create_book = [&](int n_orders) -> void { for (int i = 1; i <= n_orders; i++) { @@ -201,7 +201,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") { TEST_CASE("fifo_matching_engine_book", "[matcher]") { auto verify_book_level = [](const BookLevel &bl, double price, uint32_t qty, uint32_t n_orders) { - REQUIRE(bl.price == md::IntPrice_from_double<7>(price)); + REQUIRE(bl.price == MDP3Price::from_double<7>(price)); REQUIRE(bl.n_orders == n_orders); REQUIRE(bl.quantity.value() == qty); REQUIRE(bl.n_orders == n_orders); @@ -536,7 +536,7 @@ TEST_CASE("perf", "[matcher]") { price = top_bid - (p_lev * tick_size); else price = (top_bid + tick_size) + (p_lev * tick_size); - ord.price = md::IntPrice_from_double<7>(price); + ord.price = MDP3Price::from_double<7>(price); ord.quantity = Quantity(rnd_gen.random_int(5, 20)); }; auto add_n_orders = [&](std::vector &out_v, size_t n_orders, @@ -666,7 +666,7 @@ TEST_CASE("perf", "[matcher]") { if (oc.order.side == api::Side::SIDE_BUY) inc = -1 * inc; oc.order.price = - md::IntPrice_from_double<7>(oc.order.price.price() + inc), + MDP3Price::from_double<7>(oc.order.price.price() + inc), sec_m.on_order_change(oc); } auto t1 = TimeUtil::GetCurrentTime(); @@ -701,7 +701,7 @@ TEST_CASE("perf", "[matcher]") { SecurityID(100), api::Side::SIDE_BUY, MDP3OrderPriority(0), - md::IntPrice_from_double<7>(10000), + MDP3Price::from_double<7>(10000), Quantity(1), std::nullopt}; std::vector exec_rep; diff --git a/src/server/sparkd/cmds/spartan/common/MDP3Price.cpp b/src/server/sparkd/cmds/spartan/common/MDP3Price.cpp new file mode 100644 index 000000000..330e49f9f --- /dev/null +++ b/src/server/sparkd/cmds/spartan/common/MDP3Price.cpp @@ -0,0 +1 @@ +#include "MDP3Price.h" diff --git a/src/server/sparkd/cmds/spartan/common/MDP3Price.h b/src/server/sparkd/cmds/spartan/common/MDP3Price.h index b4fab9f25..eb636fdc3 100644 --- a/src/server/sparkd/cmds/spartan/common/MDP3Price.h +++ b/src/server/sparkd/cmds/spartan/common/MDP3Price.h @@ -1,6 +1,89 @@ #pragma once -#include "lib/md/IntPrice.h" + +#include +#include +#include "lib/utils/bts_assert.h" namespace bts::spartan::mdp3 { -using MDP3Price = md::IntPrice; + +class MDP3Price { + public: + MDP3Price() : _mantissa(std::numeric_limits::min()) {} + MDP3Price(int64_t mantissa, int8_t exponent) : _mantissa(mantissa) { + bts_assert(exponent == _exponent()); + } + + template static MDP3Price from_double(double price) { + static_assert(PRECISION == -_exponent()); + if (os::is_nan(price)) + return {}; + else { + double temp; + if (fabs(modf(price, &temp)) != 0.0) { + const double rounding = 0.5 * _pow10(); + price += (price > 0) ? rounding : -rounding; + price *= __pow10(); + } + return {(int64_t)price}; + } + } + + static MDP3Price zero() { + return MDP3Price{0}; + } + + MDP3Price(MDP3Price const &other) { _mantissa = other._mantissa; } + MDP3Price &operator=(MDP3Price const &other) { + _mantissa = other._mantissa; + return *this; + } + bool operator==(MDP3Price other) const { + return _mantissa == other._mantissa; + } + bool operator!=(MDP3Price other) const { + return _mantissa != other._mantissa; + } + bool operator<(MDP3Price other) const { return _mantissa < other._mantissa; } + bool operator<=(MDP3Price other) const { + return *this == other || *this < other; + } + bool operator>(MDP3Price other) const { return _mantissa > other._mantissa; } + bool operator>=(MDP3Price other) const { return !(*this < other); } + + MDP3Price operator-(MDP3Price other) { + return {_mantissa - other._mantissa}; + } + MDP3Price operator+(MDP3Price other) { + return {_mantissa + other._mantissa}; + } + MDP3Price &operator+=(MDP3Price other) { + _mantissa += other._mantissa; + return *this; + } + MDP3Price operator-() { return {-_mantissa}; } + MDP3Price &operator-=(MDP3Price other) { + _mantissa -= other._mantissa; + return *this; + } + MDP3Price operator*(int64_t factor) const { return {_mantissa * factor}; } + MDP3Price &operator*=(int64_t &other) { + _mantissa *= other; + return *this; + } + MDP3Price operator/(int64_t factor) const { return {_mantissa / factor}; } + double price() const { return _mantissa * _pow10(); } + bool is_nan() const { return *this == MDP3Price{}; } + + private: + MDP3Price(int64_t mantissa) : _mantissa(mantissa) {} + int64_t _mantissa; + static constexpr int8_t _exponent() { return -7; } + static constexpr double _pow10() { return std::pow(10, _exponent()); } + static constexpr double __pow10() { return std::pow(10, -_exponent()); } +}; + +inline std::ostream &operator<<(std::ostream &os, MDP3Price p) { + os << p.price(); + return os; } +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp index 6f046fcd6..d85ff70ab 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/channel_observers/SyntheticBookRecorder.cpp @@ -110,7 +110,7 @@ get_leg_books(const std::vector<_LegDef> &leg_defs, const BookMap &books) { LegBookVec rv; for (auto [lsid, lqty] : leg_defs) { auto [eid, bids, asks] = get_book_tuple(lsid, books); - rv.emplace_back(eid, lsid, lqty, md::INT_NAN, bids, asks); + rv.emplace_back(eid, lsid, lqty, MDP3Price{}, bids, asks); } return rv; } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp index dc5614079..a817b743e 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp @@ -360,11 +360,11 @@ void InstrumentSnapshot::remove_order(OrderKey id) { Book InstrumentSnapshot::build_book_from_orders(api::Side side) const { auto invert = side == api::SIDE_BUY ? -1 : 1; - auto book_idx = std::map(); + auto book_idx = std::map(); for (auto &o : orders()) if (o.second.side == side) { auto &order = o.second; - auto &level = book_idx[invert * order.price.mantissa()]; + auto &level = book_idx[order.price * invert]; level.n_orders++; level.price = order.price; level.quantity.value() += order.quantity.value(); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.cpp index 35b4596c6..9e4b6c496 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.cpp @@ -34,10 +34,10 @@ void merge_books(api::Side s, Book const &b, ImpliedBook const &ib, Book *out) { using Comp = std::function; auto comp = s == api::SIDE_BUY ? Comp([](BookLevel const &l1, BookLevel const &l2) { - return l1.price.mantissa() > l2.price.mantissa(); + return l1.price > l2.price; }) : Comp([](BookLevel const &l1, BookLevel const &l2) { - return l1.price.mantissa() < l2.price.mantissa(); + return l1.price < l2.price; }); // Copy direct book to merged diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h index 3fbb2a5f4..5bb59f671 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h @@ -80,7 +80,7 @@ inline std::ostream &operator<<(std::ostream &s, BookLevel const &l) { } struct SmallBookLevel { - SmallBookLevel() : price(md::INT_NAN), quantity(0) {} + SmallBookLevel() : price(), quantity(0) {} SmallBookLevel(MDP3Price price, Quantity quantity) : price(price), quantity(quantity) {} diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp index 446ab6c8f..7480cc9db 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp @@ -26,7 +26,7 @@ template struct LegBookCursor { // that price. std::tuple units_at_best_price() const { if (_curr_level >= _source->size()) - return {md::INT_NAN, Quantity(0)}; + return {MDP3Price{}, Quantity(0)}; auto auq = abs(_unit_qty); if (curr_level_qty() >= auq) return { @@ -43,13 +43,13 @@ template struct LegBookCursor { cost += it->price * it->quantity.value(); auq -= it->quantity; } - return {md::INT_NAN, Quantity(0)}; + return {MDP3Price{}, Quantity(0)}; } // Removes the specified qty from the top of the book, and returns the total // cost. MDP3Price remove(Quantity qty) { if (_curr_level >= _source->size() || qty <= 0) - return md::INT_NAN; + return MDP3Price{}; if (curr_level_qty() <= qty) { auto cost = curr_level_price() * curr_level_qty().value(); qty -= curr_level_qty(); @@ -118,7 +118,7 @@ inline void from_small_book( } if (clear_extra_levels) { while (i < dest.size()) { - dest[i].price = md::INT_NAN; + dest[i].price = MDP3Price{}; dest[i].quantity.value() = 0; i++; } @@ -158,7 +158,7 @@ inline void _compute_synthetic_book( leg_cursors.emplace_back(&std::get<2>(l), std::get<0>(l)); } while (max_depth-- > 0 && (!max_qty.has_value() || max_qty.value() > 0)) { - auto cost = md::INT_ZERO; + auto cost = MDP3Price::zero(); Quantity limiting_factor(std::numeric_limits::max()); for (const auto &l : leg_cursors) { auto [unit_price, qty_at_price] = l.units_at_best_price(); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h index 2be905f69..2f82a2ef5 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h @@ -106,7 +106,7 @@ inline bool has_min_qty( Iter first, Iter last, Quantity qty, - MDP3Price limit_price = md::INT_NAN, + MDP3Price limit_price = {}, api::Side side = api::Side::SIDE_UNDEFINED) { if (qty.value() == 0) return true; @@ -134,7 +134,7 @@ inline void change_unit_qty( Quantity unit_qty) { // bts_assert(unit_qty.value() > 0); - MDP3Price running_cost = md::INT_ZERO; + MDP3Price running_cost = MDP3Price::zero(); int running_qty = 0; for (auto it = book_in_first; it != book_in_last; it++) { auto unit_remainder = unit_qty.value() - running_qty; @@ -151,7 +151,7 @@ inline void change_unit_qty( running_cost += it->price * unit_remainder; book_out.emplace_back(running_cost, Quantity(1)); qty_in_lev -= unit_remainder; - running_cost = md::INT_ZERO; + running_cost = MDP3Price::zero(); running_qty = 0; } auto unit_lev_remainder = qty_in_lev / unit_qty.value(); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp index 3d8e49b14..59778a75e 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/tests/synthetic_book_tests.cpp @@ -48,7 +48,7 @@ MDP3Price qty_cost(const SmallBook &book, Quantity qty) { // Passing an empty book, however, is plausible, as synthetic leg(s) // with empty books are part of some of the test scenarios. if (book.empty()) - return md::INT_NAN; + return {}; // The book represents the opposite side of the market (relative to the qty). // If qty is positive, it is to be 'bought' from the 'ask' market, so the // book's prices should be in ascending order. @@ -56,7 +56,7 @@ MDP3Price qty_cost(const SmallBook &book, Quantity qty) { // so the book's prices should be in descending order. REQUIRE( is_sorted(book, qty > 0 ? api::Side::SIDE_SELL : api::Side::SIDE_BUY)); - MDP3Price cost = md::INT_ZERO; + MDP3Price cost = MDP3Price::zero(); bool negate = qty.value() < 0; qty = abs(qty); for (auto it = book.begin(); it != book.end(); it++) { @@ -76,11 +76,11 @@ MDP3Price qty_cost(const SmallBook &book, Quantity qty) { // The computed bound is the sum of "leg_qty * leg_best_price" // for each leg in the specified synthetic. MDP3Price min_first_price(const LegMarkets &synth) { - auto cost = md::INT_ZERO; + auto cost = MDP3Price::zero(); for (const auto &[qty, price_mult, bk] : synth) { bts_assert(price_mult.is_nan()); if (bk.empty()) - return md::INT_NAN; + return {}; cost += bk[0].price * qty.value(); } return cost; @@ -91,11 +91,11 @@ MDP3Price min_first_price(const LegMarkets &synth) { // The computed bound is the sum of "leg_qty * leg_worst_price" // for each leg in the specified synthetic. MDP3Price max_last_price(const LegMarkets &synth) { - auto cost = md::INT_ZERO; + auto cost = MDP3Price::zero(); for (const auto &[qty, price_mult, bk] : synth) { bts_assert(price_mult.is_nan()); if (bk.empty()) - return md::INT_NAN; + return {}; cost += bk[bk.size() - 1].price * qty.value(); } return cost; @@ -154,7 +154,7 @@ void expected_synth_book(LegMarkets legs, SmallBook &dest) { REQUIRE((book_utils::volume(bk) >= limiting_factor * abs(qty))); } - auto unit_cost = md::INT_ZERO; + auto unit_cost = MDP3Price::zero(); for (auto &[qty, price_mult, bk] : legs) { bts_assert(price_mult.is_nan()); unit_cost += qty_cost(bk, qty); @@ -260,30 +260,30 @@ void five_legged(std::vector &synths) { { synths.emplace_back(); auto &legs = synths.back(); - legs.emplace_back(Quantity(-2), md::INT_NAN, leg1); - legs.emplace_back(Quantity(1), md::INT_NAN, leg2); - legs.emplace_back(Quantity(2), md::INT_NAN, leg3); - legs.emplace_back(Quantity(-1), md::INT_NAN, leg4); - legs.emplace_back(Quantity(1), md::INT_NAN, leg5); + legs.emplace_back(Quantity(-2), MDP3Price {}, leg1); + legs.emplace_back(Quantity(1), MDP3Price {}, leg2); + legs.emplace_back(Quantity(2), MDP3Price {}, leg3); + legs.emplace_back(Quantity(-1), MDP3Price {}, leg4); + legs.emplace_back(Quantity(1), MDP3Price {}, leg5); } { synths.emplace_back(); auto &legs = synths.back(); - legs.emplace_back(Quantity(-2), md::INT_NAN, leg1); - legs.emplace_back(Quantity(1), md::INT_NAN, leg2); - legs.emplace_back(Quantity(7), md::INT_NAN, leg3); - legs.emplace_back(Quantity(-1), md::INT_NAN, leg4); - legs.emplace_back(Quantity(1), md::INT_NAN, leg5); + legs.emplace_back(Quantity(-2), MDP3Price{}, leg1); + legs.emplace_back(Quantity(1), MDP3Price{}, leg2); + legs.emplace_back(Quantity(7), MDP3Price{}, leg3); + legs.emplace_back(Quantity(-1), MDP3Price{}, leg4); + legs.emplace_back(Quantity(1), MDP3Price{}, leg5); } { // sneak an empty book in there. synths.emplace_back(); auto &legs = synths.back(); - legs.emplace_back(Quantity(-2), md::INT_NAN, leg1); - legs.emplace_back(Quantity(1), md::INT_NAN, leg2); - legs.emplace_back(Quantity(7), md::INT_NAN, leg3); - legs.emplace_back(Quantity(-1), md::INT_NAN, SmallBook()); - legs.emplace_back(Quantity(1), md::INT_NAN, leg5); + legs.emplace_back(Quantity(-2), MDP3Price{}, leg1); + legs.emplace_back(Quantity(1), MDP3Price{}, leg2); + legs.emplace_back(Quantity(7), MDP3Price{}, leg3); + legs.emplace_back(Quantity(-1), MDP3Price{}, SmallBook()); + legs.emplace_back(Quantity(1), MDP3Price{}, leg5); } } @@ -306,21 +306,21 @@ void two_legged(std::vector &synths) { // * a negative quantity is sold to the bid market, // so its price levels are expected to be in descending order. synths.emplace_back(); - synths.back().emplace_back(Quantity(i), md::INT_NAN, leg1_vars[j]); + synths.back().emplace_back(Quantity(i), MDP3Price{}, leg1_vars[j]); synths.back().emplace_back( - Quantity(-i), md::INT_NAN, leg2_vars[j]); + Quantity(-i), MDP3Price{}, leg2_vars[j]); synths.emplace_back(); synths.back().emplace_back( - Quantity(2 * i - 1), md::INT_NAN, leg1_vars[j]); + Quantity(2 * i - 1), MDP3Price{}, leg1_vars[j]); synths.back().emplace_back( - Quantity(-(3 * i + 1)), md::INT_NAN, leg2_vars[j]); + Quantity(-(3 * i + 1)), MDP3Price{}, leg2_vars[j]); synths.emplace_back(); synths.back().emplace_back( - Quantity(3 * i + 1), md::INT_NAN, leg1_vars[j]); + Quantity(3 * i + 1), MDP3Price{}, leg1_vars[j]); synths.back().emplace_back( - Quantity(-(2 * i - 1)), md::INT_NAN, leg2_vars[j]); + Quantity(-(2 * i - 1)), MDP3Price{}, leg2_vars[j]); } } } @@ -339,19 +339,19 @@ void one_legged(std::vector &synths) { auto append_variations = [&](const SmallBook &b) { synths.emplace_back(); - synths.back().emplace_back(Quantity(i), md::INT_NAN, b); + synths.back().emplace_back(Quantity(i), MDP3Price{}, b); // synths.emplace_back(); synths.back().emplace_back( - Quantity(i), md::INT_NAN, flip_book(b, false, true)); + Quantity(i), MDP3Price{}, flip_book(b, false, true)); // synths.emplace_back(); synths.back().emplace_back( - Quantity(-i), md::INT_NAN, flip_book(b, true, false)); + Quantity(-i), MDP3Price{}, flip_book(b, true, false)); // synths.emplace_back(); synths.back().emplace_back( - Quantity(-i), md::INT_NAN, flip_book(b, true, true)); + Quantity(-i), MDP3Price{}, flip_book(b, true, true)); }; append_variations(leg1); append_variations(leg2);