diff --git a/src/server/sparkd/cmds/Spartan.cpp b/src/server/sparkd/cmds/Spartan.cpp index 78b0f92be..9785ecc99 100644 --- a/src/server/sparkd/cmds/Spartan.cpp +++ b/src/server/sparkd/cmds/Spartan.cpp @@ -11,7 +11,6 @@ #include "cmds/SQL.h" #include "cmds/Sim.h" #include "cmds/Stats.h" -#include "cmds/ValidateFifo.h" #include "cmds/ValuePolys.h" #include "cmds/Test.h" @@ -33,7 +32,6 @@ Spartan::Spartan() : CLI::App("Futures trading system for devs", "spartan") { add_subcommand(CLI::App_p(new Sim())); add_subcommand(CLI::App_p(new SQL())); add_subcommand(CLI::App_p(new Stats())); - add_subcommand(CLI::App_p(new ValidateFifo())); add_subcommand(CLI::App_p(new ValuePolys())); add_subcommand(CLI::App_p(new Test())); } diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.cpp deleted file mode 100644 index e8bd37051..000000000 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.cpp +++ /dev/null @@ -1,541 +0,0 @@ -#include "back_testing/matcher/validation/matcher_validation_observer.h" -#include "back_testing/matcher/FifoMatcherContext.h" -#include "back_testing/matcher/FillDepot.h" -#include "back_testing/matcher/iceberg_helper.h" -#include "back_testing/matcher/matcher_diagnostics.h" -#include "back_testing/matcher/validation/event_flags.h" -#include "domain/SecuritySnapshot.h" -#include "lib/orderman/orderman_utils/Formatter.h" -#include "lib/orderman/orderman_utils/PriceComp.h" -#include "back_testing/matcher/iceberg_helper.h" -#include "back_testing/matcher/matcher_diagnostics.h" -#include "back_testing/matcher/validation/event_flags.h" -#include "domain/SecuritySnapshot.h" -#include "feed_handler/detail/gen/src/SecurityTradingStatus.h" -#include "back_testing/common/event_ordinal.h" -#include "back_testing/common/mdp3_utils.h" -#include "back_testing/common/order_utils.h" -#include "back_testing/common/security_comp.h" -#include "utils/ActionSummarizer.h" -#include "utils/ActionSummarizerMacros.h" -#include -#include -#include - -// Error logs are automatically logged to the console -// (w/ default logging configuration), but they lag behind -// immediate console output (i.e., calls to std::cout). -// If _ECHO_ON is defined, the _ERROR_LEVEL macro is set to Info -// to avoid duplicate, and (perhaps more importantly) unsynchronized output. -// #define _ECHO_ON -#ifdef _ECHO_ON -#define _ECHO(...) std::cout << bts::orderman::Formatter::fmt(__VA_ARGS__); -#define _ERROR_LEVEL Info -#else -#define _ECHO(...) -#define _ERROR_LEVEL Error -#endif -#define _LOG(level, msg) \ - _ECHO(_log_header(), " ", msg, "\n") \ - LOG(OrderMan, level, "{} {}", _log_header(), msg) -#define INFO(...) _LOG(Info, bts::orderman::Formatter::fmt(__VA_ARGS__)) -#define ERROR(...) \ - _LOG(_ERROR_LEVEL, bts::orderman::Formatter::fmt(__VA_ARGS__)) - -// SUMMARIZE macro wrappers -#define _SUMMARIZE SUMMARIZE(_log_header()) -#define _SUMMARIZE_TRANSACTION(...) \ - SUMMARIZE_TRANSACTION(_log_header(), FMT(__VA_ARGS__)) -#define _SUMMARIZE_TRANSACTION_NAMED(name, ...) \ - SUMMARIZE_TRANSACTION_NAMED(name, _log_header(), FMT(__VA_ARGS__)) - -namespace bts::spartan::mdp3 { -using namespace bts::spartan::mdp3::iceberg_helper; -// aliases -using TradingStatus = MatcherValidationObserver::TradingStatus; -template -using flat_map = boost::container::flat_map; -// consts -const size_t NO_EVENT = 0, MIN_OBSERVED_EVENT = 100; - -enum class Verbosity { Minimal = 0, Normal = 1, Verbose = 2 }; - -/////////////////////// -// stateless helpers -/////////////////////// -inline std::string _log_header() { return "MatcherValidationObserver"; } - -inline bool _is_interesting_sec(const SecuritySnapshot &s) { - return s.definition().asset() == "ES"; - // return strcmp(s.definition().asset(), "SI") == 0; -} - -void _sync_orders( - size_t event_id, - const SecuritySnapshot &s, - Orders &orders, - const std::unordered_map &icebergs) { - // - for (auto ch : s.order_changes()) { - switch (ch.change) { - case OrderChange::added: - case OrderChange::updated: - case OrderChange::updated_price: - add_iceberg_annotation(ch.order, ch.change, icebergs, event_id, true); - orders[ch.order.key] = ch.order; - break; - case OrderChange::removed: - orders.erase(ch.order.key); - break; - } - } -} - -bool _has_multiple_aggressor_secs(const FeedEvent &ev) { - bool aggr_sec_found = false; - for (auto *s : ev) { - for (auto f : s->fills()) { - switch (f.aggressor) { - case MDP3Fill::Aggressor::THIS_BUY: - case MDP3Fill::Aggressor::THIS_SELL: - if (aggr_sec_found) - return true; - aggr_sec_found = true; - break; - default: - continue; - } - break; - } - } - return false; -} - -inline std::ostream &operator<<(std::ostream &ostr, const TradingStatus &ts) { - switch (ts) { - case TradingStatus::None: - ostr << "None"; - break; - case TradingStatus::Preopen: - ostr << "Preopen"; - break; - case TradingStatus::Uninitialized: - ostr << "Uninitialized"; - break; - case TradingStatus::Initialized: - ostr << "Initialized"; - break; - default: - ostr << "TradingStatus_" << static_cast(ts); - break; - } - return ostr; -} - -template -inline void print_if_not_empty( - TIter first, TIter last, std::string title, std::ostream &out = std::cout) { - bool printed_title = false; - while (first != last) { - if (!printed_title) { - out << title << std::endl; - printed_title = true; - } - out << *first << std::endl; - first++; - } -} - -inline bool fill_equals(const MDP3Fill &a, const MDP3Fill &b) { - return a.aggressor == b.aggressor && a.quantity == b.quantity && - orderman::price_comp::is_equal(a.price, b.price); -} - -inline bool match_fills( - const FillDepot &fd, - std::vector snaps, - std::ostream &out = std::cout) { - std::unordered_map> fill_map; - fd.copy_to(fill_map); - bool errors = false; - for (const auto *s : snaps) { - auto sec_fills_it = fill_map.find(s->security_id()); - if (sec_fills_it == fill_map.end()) { - print_if_not_empty( - s->fills().begin(), s->fills().end(), - FMT(s->symbol(), " unmatched fills"), out); - continue; - } - auto &act_fills = sec_fills_it->second; - auto exp_it = s->fills().begin(); - std::vector unmatched_exp; - while (exp_it != s->fills().end()) { - bool matched = false; - for (auto act_it = act_fills.begin(); act_it != act_fills.end(); - act_it++) { - if (fill_equals(*act_it, *exp_it)) { - act_fills.erase(act_it); - matched = true; - break; - } - } - if (!matched) { - unmatched_exp.push_back(*exp_it); - } - exp_it++; - } - errors |= (act_fills.size() || unmatched_exp.size()); - print_if_not_empty( - act_fills.begin(), act_fills.end(), - FMT(s->symbol(), " unexpected fills"), out); - print_if_not_empty( - unmatched_exp.begin(), unmatched_exp.begin(), - FMT(s->symbol(), " unmatched fills"), out); - } - return !errors; -} -/////////////////////// -// private classes -/////////////////////// -struct _SecurityData { - _SecurityData() - : status(TradingStatus::None), current_orders(), previous_orders() {} - TradingStatus status; - Orders current_orders, previous_orders; - size_t last_observed_event = NO_EVENT; - const SecuritySnapshot *snapshot = nullptr; - std::string symbol() const { - return snapshot == nullptr ? "N/A" : snapshot->symbol(); - } - SecurityID security_id() const { - return snapshot == nullptr ? SecurityID(0) : snapshot->security_id(); - } -}; - -struct MatcherValidationObserver::PImpl { - PImpl() {} - PImpl( - std::unordered_map icebergs, - flat_map interesting_event_ranges) - : icebergs(icebergs), - interesting_event_ranges(interesting_event_ranges) {} - - flat_map security_cache; - std::unordered_map icebergs; - flat_map interesting_event_ranges; - flat_map interesting_orders; - - // number of 'interesting events' observed so far. - uint32_t interesting_event_count = 0; - // actual event_id (from the exchange) of most recently observed event. - size_t event_id = NO_EVENT; - FifoMatcherContext exch_matcher; - Verbosity verbosity = Verbosity::Minimal; - EventFlags ignore_flags = EventFlags::MultipleSecurityAggressors; - - // event_id is used to transition the security's trading status from - // Preopen to Uninitialized - _SecurityData & - get_security_data(const SecuritySnapshot &s, size_t event_id) { - auto it = security_cache.find(s.security_id()); - if (it != security_cache.end()) { - auto &data = it->second; - if (event_id != data.last_observed_event) { - data.last_observed_event = event_id; - switch (data.status) { - case TradingStatus::Preopen: - data.status = TradingStatus::Uninitialized; - break; - case TradingStatus::None: - if (s.trading_status() == - ::mdp3::SecurityTradingStatus::ReadyToTrade) - data.status = TradingStatus::Preopen; - break; - default: - break; - } - } - data.snapshot = &s; - return data; - } - // this is the first time this security has been observed; initialize its - // data. - auto &data = security_cache.emplace(s.security_id(), _SecurityData()) - .first->second; - data.snapshot = &s; - data.last_observed_event = event_id; - data.previous_orders = s.orders(); - for (auto &o : data.previous_orders) - add_iceberg_annotation( - o.second, OrderChange::ChangeType::added, icebergs, event_id); - // the first event at the open isn't processed bc - // we don't have a clear understanding of how preopen matching works. - data.status = - s.trading_status() == ::mdp3::SecurityTradingStatus::ReadyToTrade - ? TradingStatus::Preopen - : TradingStatus::None; - data.snapshot = &s; - return data; - } - - EventFlags get_event_flags(const FeedEvent &ev) { - EventFlags flags = EventFlags::None; - if (_has_multiple_aggressor_secs(ev)) - flags |= EventFlags::MultipleSecurityAggressors; - for (const auto *s : ev) { - for (auto &f : s->fills()) { - if (icebergs.find(f.key) != icebergs.end()) - flags |= EventFlags::IcebergOrders; - switch (f.aggressor) { - case MDP3Fill::Aggressor::THIS_BUY: - case MDP3Fill::Aggressor::THIS_SELL: - flags |= EventFlags::DirectFills; - break; - case MDP3Fill::Aggressor::IMPLIED: - flags |= EventFlags::ImpliedFills; - break; - default: - break; - } - } - } - return flags; - } -}; -/////////////////////// -// constructors -/////////////////////// -MatcherValidationObserver::MatcherValidationObserver( - std::unordered_map icebergs) - : MatcherValidationObserver(icebergs, flat_map{}) {} - -MatcherValidationObserver::MatcherValidationObserver( - std::unordered_map icebergs, - flat_map interesting_event_ranges) - : _me(new MatcherValidationObserver::PImpl( - icebergs, interesting_event_ranges)) {} - -MatcherValidationObserver::~MatcherValidationObserver() {} - -/////////////////////// -// misc private methods -/////////////////////// -TradingStatus MatcherValidationObserver::_trading_status( - const SecuritySnapshot &s, size_t event_id) { - return _me->get_security_data(s, event_id).status; -} - -bool MatcherValidationObserver::_is_ready_to_trade( - const SecuritySnapshot &s, size_t event_id) { - switch (_trading_status(s, event_id)) { - case TradingStatus::Uninitialized: - case TradingStatus::Initialized: - return true; - default: - return false; - } -} - -void MatcherValidationObserver::_sync_current_orders(FeedEvent const &ev) { - for (auto s : ev) { - auto &new_orders = _me->get_security_data(*s, ev.event_id).current_orders; - _sync_orders(ev.event_id, *s, new_orders, _me->icebergs); - } -} - -void MatcherValidationObserver::_sync_previous_orders(const FeedEvent &ev) { - for (auto s : ev) { - auto &old_orders = - _me->get_security_data(*s, ev.event_id).previous_orders; - _sync_orders(ev.event_id, *s, old_orders, _me->icebergs); - } -} - -void MatcherValidationObserver::_reset_matcher() { - // remove orders, and change [actively trading] securities - // trading status back to Preopen so they will be added back - // in the next update (the state must be re-initialized - // before updates can be consumed incrementally again). - if (_me->verbosity > Verbosity::Normal) - INFO("clearing orders to reset the matcher's state"); - _me->exch_matcher.clear(); - for (auto &kv : _me->security_cache) { - if (kv.second.status != TradingStatus::None) - kv.second.status = TradingStatus::Preopen; - } -} -/////////////////////// -// IMDP3FeedObserver -/////////////////////// -void MatcherValidationObserver::on_recovery_complete( - InitialSnapshot const &snap) { - _SUMMARIZE; - for (auto s : snap) { - auto &sec_data = _me->get_security_data(*s, NO_EVENT); - sec_data.current_orders.clear(); - _sync_orders(NO_EVENT, *s, sec_data.current_orders, _me->icebergs); - sec_data.previous_orders = sec_data.current_orders; - } -} - -void MatcherValidationObserver::on_event_end(FeedEvent const &ev) { - bts_assert(ev.event_id > _me->event_id); - _me->event_id = ev.event_id; - switch (get_event_ordinal( - _me->interesting_event_ranges, ev.event_id, MIN_OBSERVED_EVENT)) { - case bts::spartan::EventOrdinal::Waiting: - // just need to synchronize the order caches. - // the matcher is only validated during 'interesting events.' - _sync_current_orders(ev); - _sync_previous_orders(ev); - return; - case bts::spartan::EventOrdinal::Interesting: - // _sync_previous_orders should be called after - // the matcher has digested the order changes. - break; - case bts::spartan::EventOrdinal::Done: - // nothing left to do (might as well kill the process). - bts_assert_msg(false, "no further events of interest."); - return; - } - auto flags = _me->get_event_flags(ev); - if ((flags & _me->ignore_flags) != EventFlags::None) { - if (_me->verbosity > Verbosity::Minimal) - INFO(FMT("skipping event ", ev.event_id, ", with flags ", flags)); - _sync_current_orders(ev); - _sync_previous_orders(ev); - _reset_matcher(); - return; - } - ++_me->interesting_event_count; - if (_me->verbosity > Verbosity::Minimal) - INFO( - "Update No ", _me->interesting_event_count, "(event id ", ev.event_id, - ")"); - // Note: the exchange processes the order updates per security, - // in a particular order, sorted hierarchically by symbol, then expiration. - // 'sorted_secs' will contain the proper order of securities. - std::vector sorted_secs; - for (auto *s : ev) { - if (_is_interesting_sec(*s)) - sorted_secs.push_back(s); - } - if (sorted_secs.empty()) - return; - std::sort( - sorted_secs.begin(), sorted_secs.end(), security_comp::SymbolComp()); - // securities must be added to the matcher before the order - // changes are processed to ensure the correct implied markets are used. - // Fills are also indexed in this pass, so they can be added to the iceberg - // qty - std::unordered_map fill_map; - for (const auto *s : sorted_secs) { - accumulate_fills(s->fills().begin(), s->fills().end(), fill_map); - _me->exch_matcher.add_security( - s->security_id(), s->symbol(), s->definition().legs()); - _me->exch_matcher.implied_matching_status( - s->security_id(), s->definition().implied_matching()); - } - // initialize matcher with previous state (only applies - // to securities whose orders the matcher hasn't seen yet). - for (const auto *s : sorted_secs) { - if (_trading_status(*s, ev.event_id) != TradingStatus::Uninitialized) - continue; - auto it = _me->security_cache.find(s->security_id()); - bts_assert(it != _me->security_cache.end()); - auto prev_ords = - it->second.previous_orders | - ranges::views::transform([](auto &p) { return p.second; }); - _me->exch_matcher.reset( - s->security_id(), prev_ords.begin(), prev_ords.end()); - _me->security_cache[s->security_id()].status = TradingStatus::Initialized; - } - // process changes with matcher - FillDepot fills; - for (const auto *s : sorted_secs) { - switch (_trading_status(*s, ev.event_id)) { - case TradingStatus::Initialized: - break; - default: - continue; - } - auto deduced = s->deduced_order_changes(); - if (deduced.size()) { - std::vector deduced_sm; - for (auto &ch : deduced) { - add_iceberg_annotation( - ch.order, ch.change, _me->icebergs, ev.event_id); - deduced_sm.emplace_back(ch); - } - _me->exch_matcher.on_order_change( - s->security_id(), deduced_sm, nullptr, fills); - } - } - // Verify the matcher's orders are identitical to the exchange's. - _sync_current_orders(ev); - bool orders_match = true; - for (const auto *s : sorted_secs) { - if (!_is_ready_to_trade(*s, ev.event_id)) - continue; - auto &sec_data = _me->security_cache[s->security_id()]; - auto &curr_orders = sec_data.current_orders; - auto error = - _me->exch_matcher.has_same_orders(s->security_id(), curr_orders); - if (error) { - ERROR( - "order discrepancies detected for ", s->symbol(), " on event ", - ev.event_id, "\nError: ", error.value(), "\nOld Orders:\n", - _me->security_cache[s->security_id()].previous_orders); - orders_match = false; - } - } - bool fills_match = match_fills(fills, sorted_secs); - if (!orders_match || !fills_match) { - // todo: add the iceberg fills back, to get to the previous state. - // describe current state for diagnostic purposes - std::stringstream msg; - msg << "Spartan Top of Books:" << std::endl; - _me->exch_matcher.print_all_bbos(msg); - msg << std::endl; - // spartan fills - msg << "Spartan Total Fill Qty: " << fills.total_fill_qty() << std::endl; - auto fill_cmp = [](const auto &lhs, const auto &rhs) { - return lhs.trade_entry_id == rhs.trade_entry_id - ? static_cast(lhs.aggressor) > - static_cast(rhs.aggressor) - : lhs.trade_entry_id < rhs.trade_entry_id; - }; - std::vector spartan_fills; - fills.copy_to(spartan_fills); - std::sort(spartan_fills.begin(), spartan_fills.end(), fill_cmp); - msg << "Spartan Fills (ordered by Trade ID):" << std::endl; - diagnostics::print_fills(spartan_fills.begin(), spartan_fills.end(), msg); - // exch top of books - msg << "Exchange Top of Books:" << std::endl; - diagnostics::print_many_bbo( - sorted_secs.begin(), sorted_secs.end(), true, true, msg); - int exch_fill_qty = 0; - for (auto *s : sorted_secs) { - for (auto f : s->fills()) - exch_fill_qty += f.quantity.value(); - } - // exch fills - msg << "Exchange Total Fill Qty: " << exch_fill_qty << std::endl; - std::vector all_exch_fills; - for (auto *s : sorted_secs) { - for (auto f : s->fills()) - all_exch_fills.push_back(f); - } - std::sort(all_exch_fills.begin(), all_exch_fills.end(), fill_cmp); - msg << "Exchange Fills (ordered by Trade ID):" << std::endl; - diagnostics::print_fills( - all_exch_fills.begin(), all_exch_fills.end(), msg); - INFO(msg.str()); - // reset the matcher to avoid cascading/compounding errors in subsequent - // events. - _reset_matcher(); - } - _sync_previous_orders(ev); -} - -} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.h deleted file mode 100644 index 68ad21b81..000000000 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/validation/matcher_validation_observer.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include "back_testing/matcher/FifoMatcherContext.h" -#include "domain/SecuritySnapshot.h" -#include "back_testing/common/mdp3_common.h" -#include "back_testing/feed_observers/IcebergFinder.h" -#include "back_testing/feed_observers/FeedObserver.h" -#include "back_testing/common/mdp3_utils.h" -#include -#include -#include - -namespace bts::spartan::mdp3 { -struct MatcherValidationObserver : FeedObserver { - MatcherValidationObserver( - std::unordered_map icebergs); - // interesting_event_ranges: - // - keys indicate the first event in an interesting range - // - values indicate the number of events in the range (i.e., the range's - // length). - MatcherValidationObserver( - std::unordered_map icebergs, - boost::container::flat_map interesting_event_ranges); - ~MatcherValidationObserver(); - void on_recovery_complete(InitialSnapshot const &snap) override; - void on_event_end(FeedEvent const &ev) override; - // the first time a security's actual trading status - // (i.e., mdp3::SecurityTradingStatus from the exchange) - // is observed to be 'ReadyToTrade,' its local TradingStatus - // (this enum) is switched from 'None' to Preopen.' The security's - // changes in that event are ignored, but on the subsequent event - // its TradingStatus will be flipped from 'Preopen' to 'Uninitialized' - // (in the beginning of the callback). That will be the first event - // in which order changes will be processed for the security. - // As such, the matcher will initialize the state for the security - // by adding every resting order it had prior to the event. - // After that, the TradingStatus will be flipped to 'Initialized.' - // From then on, updates for the security will be processed incrementally. - enum class TradingStatus { None, Preopen, Uninitialized, Initialized }; - - private: - struct PImpl; - std::unique_ptr _me; - - TradingStatus _trading_status(const SecuritySnapshot &s, size_t event_id); - // returns true if the security's TradingStatus is 'Uninitialized', or - // 'Initialized' (in other words, if it isn't 'None,' or 'Preopen') - bool _is_ready_to_trade(const SecuritySnapshot &s, size_t event_id); - // std::tuple _event_filter(const FeedEvent&); - - // state maintenance - void _sync_current_orders(FeedEvent const &ev); - void _sync_previous_orders(FeedEvent const &ev); - void _reset_matcher(); -}; -} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.cpp b/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.cpp deleted file mode 100644 index a8226fe92..000000000 --- a/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "ValidateFifo.h" -#include "back_testing/matcher/validation/matcher_validation_observer.h" -#include "back_testing/feed_observers/IcebergFinder.h" -#include "back_testing/common/mdp3_utils.h" -#include - -namespace bts::spartan::cmds { - -struct ValidateFifo::Args { - std::string pcap; -}; - -ValidateFifo::ValidateFifo() - : CLI::App("", "validate-fifo"), _args(new ValidateFifo::Args()) { - add_option("pcap,--pcap", _args->pcap)->required(); - callback([&] { - auto ibs = mdp3::IcebergFinder::find_all(_args->pcap); - mdp3::MatcherValidationObserver obs(ibs); - mdp3::observe_channel(_args->pcap, obs); - }); -} - -ValidateFifo::~ValidateFifo() {} - -} // namespace bts::spartan::cmds diff --git a/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.h b/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.h deleted file mode 100644 index d54c7059e..000000000 --- a/src/server/sparkd/cmds/spartan/cmds/ValidateFifo.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include -#include - -namespace bts::spartan::cmds { - -struct ValidateFifo : public CLI::App { - ValidateFifo(); - ~ValidateFifo(); - -private: - struct Args; - std::unique_ptr _args; -}; - -} // namespace bts::spartan::cmds