diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp index 6e14795ea..e60785e69 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp @@ -67,9 +67,7 @@ FifoMatcherContext::~FifoMatcherContext() {} // security accessors /////////////////////// bool FifoMatcherContext::add_security( - SecurityID sec_id, - const std::string &symbol, - const LegVec &legs) { + SecurityID sec_id, const std::string &symbol, const LegVec &legs) { auto curr = _me->matchers.find(sec_id); if (curr == _me->matchers.end()) { _me->symbols.insert(std::make_pair(sec_id, symbol)); @@ -150,14 +148,14 @@ FifoMergedSecurityMatcher *FifoMatcherContext::_get_matcher(SecurityID sec_id) { return m == _me->matchers.end() ? nullptr : m->second; } -const FifoMergedSecurityMatcher * -FifoMatcherContext::_get_matcheri(SecurityID sec_id) const { +const FifoMergedSecurityMatcher *FifoMatcherContext::_get_matcheri( + SecurityID sec_id) const { auto m = _me->matchers.find(sec_id); return m == _me->matchers.end() ? nullptr : m->second; } -FifoMergedSecurityMatcher * -FifoMatcherContext::_get_or_add_matcher(SecurityID sec_id) { +FifoMergedSecurityMatcher *FifoMatcherContext::_get_or_add_matcher( + SecurityID sec_id) { auto m = _me->matchers.find(sec_id); if (m == _me->matchers.end()) { auto *new_m = new FifoMergedSecurityMatcher( @@ -206,8 +204,8 @@ void FifoMatcherContext::on_order_change( /////////////////////// // diagnostic /////////////////////// -std::optional -FifoMatcherContext::has_same_orders(SecurityID sec, const Orders &orders) { +std::optional FifoMatcherContext::has_same_orders( + SecurityID sec, const Orders &orders) { return _get_matcher(sec)->matcher()->has_same_orders(orders); } @@ -256,7 +254,10 @@ void FifoMatcherContext::_reset( } } -void FifoMatcherContext::reset(SecurityID sec, std::vector orders, std::vector *effects) { +void FifoMatcherContext::reset( + SecurityID sec, + std::vector orders, + std::vector *effects) { _reset(sec, orders.begin(), orders.end(), effects); } @@ -290,11 +291,13 @@ void FifoMatcherContext::print_all_bbos(std::ostream &out) { void FifoMatcherContext::_ensure_sync_implied_candidates() { if (!_me->implied_candidates_dirty) return; - // For each security, just remove the existing implied matchers and generate them again. - // While this isn't the most efficient implementation (it could be optimized to add/remove - // implied matchers incrementally), it is the simplest. + // For each security, just remove the existing implied matchers and generate + // them again. While this isn't the most efficient implementation (it could + // be optimized to add/remove implied matchers incrementally), it is the + // simplest. for (auto p : _me->matchers) { - std::vector candidates = _me->implied_market_model.find_implied_markets(p.first); + std::vector candidates = + _me->implied_market_model.find_implied_markets(p.first); for (const auto &can : candidates) { auto *imatcher = p.second->add_implied_matcher(can); if (imatcher == nullptr) diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp index aaa060a1f..d4d1941ef 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp @@ -2,20 +2,21 @@ #include "FifoImpliedSecurityMatcher.h" #include "FifoSecurityMatcher.h" #include "FillDepot.h" +#include "back_testing/common/OrderModification.h" +#include "back_testing/common/order_utils.h" #include "common/MatcherOneof.h" #include "common/MatchingSession.h" +#include "domain/book/book_utils.h" #include "fill_factory.h" #include "lib/orderman/orderman_utils/PriceComp.h" -#include "back_testing/common/OrderModification.h" -#include "domain/book/book_utils.h" -#include "back_testing/common/order_utils.h" #include #include #include #include #include -#define _LOG(level, ...) LOG(OrderMan, level, "{} {}", _log_header(), fmt::format(__VA_ARGS__)) +#define _LOG(level, ...) \ + LOG(OrderMan, level, "{} {}", _log_header(), fmt::format(__VA_ARGS__)) #define DIAG(...) _LOG(Diag, __VA_ARGS__) #define INFO(...) _LOG(Info, __VA_ARGS__) #define WARN(...) _LOG(Info, __VA_ARGS__) @@ -57,17 +58,17 @@ struct LegMapComp { // should exhibit Log(n) lookups. Although technically lookups for such a // container are O(m)Log(n). If 'm' becomes large, a tree-based // implementation may become appropriate. -using ImpliedMatchers = boost::container::flat_map; +using ImpliedMatchers = boost::container:: + flat_map; struct FifoMergedSecurityMatcher::PImpl { PImpl(const FifoMergedSecurityMatcher::InitArgs &args) : security_id(args.security_id), ignore_ops_on_deleted_orders(args.ignore_ops_on_deleted_orders), - trace(args.trace), - implied_matching_status(true), + trace(args.trace), implied_matching_status(true), matcher(new FifoSecurityMatcher( - {args.security_id, 0, args.ignore_ops_on_deleted_orders, - nullptr, args.trace})), + {args.security_id, 0, args.ignore_ops_on_deleted_orders, nullptr, + args.trace})), implied_matchers() {} ~PImpl() { @@ -119,7 +120,8 @@ const FifoSecurityMatcher *FifoMergedSecurityMatcher::matcheri() const { return _me->matcher.get(); } -FifoImpliedSecurityMatcher * FifoMergedSecurityMatcher::add_implied_matcher(const LegMap & legs) { +FifoImpliedSecurityMatcher *FifoMergedSecurityMatcher::add_implied_matcher( + const LegMap &legs) { auto res = _me->implied_matchers.emplace(legs, nullptr); if (res.second) { res.first->second = new FifoImpliedSecurityMatcher(security_id()); @@ -282,8 +284,8 @@ ImpliedBookSide FifoMergedSecurityMatcher::implied_book(Side side) { auto &ib = std::get<0>(ib_tup); auto &index = std::get<1>(ib_tup); if (ib[index].price == curr.price) - curr.quantity = Quantity( - curr.quantity.value() + ib[index++].quantity.value()); + curr.quantity = + Quantity(curr.quantity.value() + ib[index++].quantity.value()); } rem_exhausted(); } @@ -369,12 +371,11 @@ Quantity FifoMergedSecurityMatcher::_fill( std::vector *effects, FillDepot &fills) { // - std::vector imatcher_vec; - for (auto& p : _me->implied_matchers) + std::vector imatcher_vec; + for (auto &p : _me->implied_matchers) imatcher_vec.push_back(p.second); auto oside = other_side(side); - MatchingSession match_session( - *_me->matcher, imatcher_vec, oside, price); + MatchingSession match_session(*_me->matcher, imatcher_vec, oside, price); auto match = match_session.get_next(); auto ord_desc = [&]() { return fmt_order(key, side, price, quantity); }; while (quantity.value() > 0 && diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h index 3873cea73..137457371 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h @@ -28,7 +28,10 @@ class FifoMergedSecurityMatcher { void implied_matching_status(bool value); bool implied_matching_status() const; void on_order_change( - const SmallOrderChange &, std::vector *, FillDepot &, AlgoEventID = no_algo_event()); + const SmallOrderChange &, + std::vector *, + FillDepot &, + AlgoEventID = no_algo_event()); FifoImpliedSecurityMatcher *add_implied_matcher(const LegMap &); FifoSecurityMatcher *matcher(); const FifoSecurityMatcher *matcheri() const; diff --git a/src/server/sparkd/cmds/spartan/domain/book/book_utils.h b/src/server/sparkd/cmds/spartan/domain/book/book_utils.h index 86cb87387..4d5fde3b7 100644 --- a/src/server/sparkd/cmds/spartan/domain/book/book_utils.h +++ b/src/server/sparkd/cmds/spartan/domain/book/book_utils.h @@ -14,8 +14,8 @@ namespace bts::spartan::mdp3::book_utils { -inline std::ostream & -fmt_price_level(std::ostream &out, Price price, Quantity qty) { +inline std::ostream &fmt_price_level( + std::ostream &out, Price price, Quantity qty) { out << qty << " @ " << price; return out; } @@ -33,13 +33,11 @@ inline std::string fmt_price_level(Price price, Quantity qty) { return out.str(); } -inline std::string -fmt_price_level(const std::tuple &pl) { +inline std::string fmt_price_level(const std::tuple &pl) { return fmt_price_level(std::get<0>(pl), std::get<1>(pl)); } -inline std::string -fmt_price_level(Side side, Price price, Quantity qty) { +inline std::string fmt_price_level(Side side, Price price, Quantity qty) { std::stringstream out; fmt_price_level(out, side, price, qty); return out.str(); @@ -116,9 +114,9 @@ inline bool has_min_qty( if (qty.value() == 0) return true; bts_assert(qty.value() > 0); - while (first != last && - (limit_price.is_nan() || - (side == Side::Buy ? limit_price < first->price : limit_price > first->price))) { + while (first != last && (limit_price.is_nan() || + (side == Side::Buy ? limit_price < first->price + : limit_price > first->price))) { qty -= first->quantity; if (qty.value() <= first->quantity.value()) return true; @@ -223,7 +221,8 @@ inline bool is_populated(const TBook &book, uint16_t level) { */ template bool is_valid(const TBook &book, const BookMark &mark) { - return is_beyond_book(book, mark) || book[mark.level()].quantity > mark.offset(); + return is_beyond_book(book, mark) || + book[mark.level()].quantity > mark.offset(); } void to_small_book(const BookSide &book, SmallBook &book_out); @@ -257,8 +256,7 @@ BookMark get_mark(const TBook &book, Quantity qty) { * the third. */ template -inline BookSlice -get_slice(const TBook &book, Quantity start, Quantity stop) { +inline BookSlice get_slice(const TBook &book, Quantity start, Quantity stop) { // todo: optimize to avoid counting up to the first book mark twice. // The statr qty mark could be simply subtracted from the stop qty, // then only the difference need be counted, beginning from the start @@ -274,8 +272,8 @@ get_slice(const TBook &book, Quantity start, Quantity stop) { * @param book_out the book to which the slice is written. */ template -inline void -slice_book(const TBook &book, const BookSlice &slice, SmallBook &book_out) { +inline void slice_book( + const TBook &book, const BookSlice &slice, SmallBook &book_out) { bts_assert(is_valid(book, slice.start)); bts_assert(is_valid(book, slice.stop)); if (slice.start == slice.stop) @@ -286,7 +284,8 @@ slice_book(const TBook &book, const BookSlice &slice, SmallBook &book_out) { book_out.emplace_back( book[slice.start.level()].price, book[slice.start.level()].quantity - slice.start.offset()); - for (uint16_t i = 1; i < slice.stop.level() - 1 && is_populated(book, i); i++) + for (uint16_t i = 1; i < slice.stop.level() - 1 && is_populated(book, i); + i++) book_out.emplace_back(book[i].price, book[i].quantity); if (is_populated(book, slice.stop.level()) && book[slice.stop.level()].quantity > slice.stop.offset()) @@ -318,8 +317,7 @@ inline Price best_price(const BookSide &bk) { return bk[0].price; } * Get the best price in the book after the specified qty. */ template -inline Price -best_price_after_qty(Iter first, Iter last, Quantity qty) { +inline Price best_price_after_qty(Iter first, Iter last, Quantity qty) { while (first != last) { if (first->quantity.value() == 0) break; @@ -341,7 +339,7 @@ inline Price best_price_after_qty(const TBook &bk, Quantity qty) { } /** - * Count the total quantity in a book. + * Count the total quantity in a book. * * @tparam Iter the type of itearator used to iterate through the price levels. * @param first iterator pointing to the first element in the book. @@ -360,7 +358,7 @@ inline Quantity total_qty(Iter first, Iter last) { } /** - * Count the total quantity in a book. + * Count the total quantity in a book. * * @tparam Container the type of book (an iterable of price-levels). * @param first iterator pointing to the first element in the book.