243 lines
8.7 KiB
C++
243 lines
8.7 KiB
C++
#include "server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.h"
|
|
#include "server/sparkd/cmds/spartan/back_testing/matcher/FifoImpliedSecurityMatcher.h"
|
|
#include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_quantity_operators.h"
|
|
#include "server/sparkd/cmds/spartan/md/mdp3/implied/implied_market.h"
|
|
#include "server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h"
|
|
#include "server/sparkd/cmds/spartan/md/mdp3/utils/order_utils.h"
|
|
#include "server/sparkd/cmds/spartan/md/mdp3/utils/security_comp.h"
|
|
#include "server/sparkd/cmds/spartan/util/action_summarizer_macros.h"
|
|
#include "server/sparkd/cmds/spartan/util/table_display.h"
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace bts::spartan::diagnostics;
|
|
|
|
template<typename K, typename V>
|
|
using flat_map = boost::container::flat_map<K, V>;
|
|
|
|
namespace bts::spartan::mdp3 {
|
|
///////////////////////
|
|
// stateless helpers
|
|
///////////////////////
|
|
//static inline std::string _log_header() { return "FifoMatcherContext"; }
|
|
|
|
static inline MDP3Quantity _norm_qty(api::Side side, MDP3Quantity qty) {
|
|
switch (side) {
|
|
case api::Side::SIDE_BUY: return qty;
|
|
case api::Side::SIDE_SELL: return -qty;
|
|
default:
|
|
bts_assert_msg(false, FMT("unexpected side '", side, "'"));
|
|
return qty;
|
|
}
|
|
}
|
|
|
|
struct FifoMatcherContext::PImpl {
|
|
const bool ignore_ops_on_deleted_orders;
|
|
// _implied_candidates_dirty should be set to true anytime a security is added/removed.
|
|
// This signals that implied market candidates must be re-evaluated
|
|
// before any susbequent order-matching (re-evaluation is performed lazily).
|
|
bool implied_candidates_dirty;
|
|
ImpliedMarket contract_matrix;
|
|
flat_map<MDP3SecurityID, FifoMergedSecurityMatcher*> matchers;
|
|
flat_map<MDP3SecurityID, std::string> symbols;
|
|
|
|
PImpl() : ignore_ops_on_deleted_orders(false) {}
|
|
|
|
PImpl(const InitArgs& args)
|
|
: ignore_ops_on_deleted_orders(args.ignore_ops_on_deleted_orders) {}
|
|
|
|
~PImpl() {
|
|
for (auto& p : matchers)
|
|
delete p.second;
|
|
matchers.clear();
|
|
}
|
|
};
|
|
|
|
///////////////////////
|
|
// constructors
|
|
///////////////////////
|
|
FifoMatcherContext::FifoMatcherContext() : FifoMatcherContext({false}) {}
|
|
|
|
FifoMatcherContext::FifoMatcherContext(const InitArgs& args) : _me(new PImpl(args)) {}
|
|
|
|
FifoMatcherContext::~FifoMatcherContext() { }
|
|
|
|
///////////////////////
|
|
// security accessors
|
|
///////////////////////
|
|
bool FifoMatcherContext::add_security(MDP3SecurityID sec_id, const std::string& symbol, const std::vector<LegDef>& legs) {
|
|
auto curr = _me->matchers.find(sec_id);
|
|
if (curr == _me->matchers.end()) {
|
|
_me->symbols.insert(std::make_pair(sec_id, symbol));
|
|
auto& ic = _me->contract_matrix.add_contract(sec_id);
|
|
// Add the legs explicitly (ImpliedMarketBuilder doesn't know about
|
|
// 'InstrumentDefinition,' or its leg structure 'LegDef').
|
|
if (legs.empty()) {
|
|
// must be an outright (to normalize outrights and spreads,
|
|
// the former is treated like a 'single leg spread')
|
|
ic.add_leg(sec_id, MDP3Quantity(1));
|
|
} else {
|
|
for (auto l : legs)
|
|
ic.add_leg(l.security_id, _norm_qty(l.side, l.ratio));
|
|
}
|
|
_me->matchers.insert(std::make_pair(sec_id, new FifoMergedSecurityMatcher({sec_id, _me->ignore_ops_on_deleted_orders})));
|
|
} else {
|
|
// Not sure why, but [John]I've seen _me->symbols contain empty strings.
|
|
// Overwrite the existing value to ensure a valid symbol.
|
|
_me->symbols[sec_id] = symbol;
|
|
return false;
|
|
}
|
|
_me->implied_candidates_dirty = true;
|
|
return true;
|
|
}
|
|
|
|
bool FifoMatcherContext::remove_security(MDP3SecurityID sec_id) {
|
|
auto curr = _me->symbols.find(sec_id);
|
|
if (curr == _me->symbols.end())
|
|
return false;
|
|
bts_assert(_me->symbols[sec_id] == curr->second);
|
|
_me->contract_matrix.remove_contract(sec_id);
|
|
_me->symbols.erase(sec_id);
|
|
_me->implied_candidates_dirty = true;
|
|
return true;
|
|
}
|
|
|
|
bool FifoMatcherContext::implied_matching_status(MDP3SecurityID sec_id) const {
|
|
auto* m = _get_matcheri(sec_id);
|
|
return m != nullptr && m->implied_matching_status();
|
|
}
|
|
|
|
void FifoMatcherContext::implied_matching_status(MDP3SecurityID sec_id, bool value) {
|
|
auto* m = _get_or_add_matcher(sec_id);
|
|
if (m->implied_matching_status() != value) {
|
|
m->implied_matching_status(value);
|
|
_me->implied_candidates_dirty = true;
|
|
}
|
|
}
|
|
|
|
///////////////////////
|
|
// book
|
|
///////////////////////
|
|
Book FifoMatcherContext::book(MDP3SecurityID sec_id, api::Side side) {
|
|
return _get_matcher(sec_id)->book(side);
|
|
}
|
|
|
|
ImpliedBook FifoMatcherContext::implied_book(MDP3SecurityID sec_id, api::Side side) {
|
|
return _get_matcher(sec_id)->implied_book(side);
|
|
}
|
|
|
|
///////////////////////
|
|
// matcher accessors
|
|
///////////////////////
|
|
FifoMergedSecurityMatcher* FifoMatcherContext::_get_matcher(MDP3SecurityID sec_id) {
|
|
auto m = _me->matchers.find(sec_id);
|
|
return m == _me->matchers.end() ? nullptr : m->second;
|
|
}
|
|
|
|
const FifoMergedSecurityMatcher* FifoMatcherContext::_get_matcheri(MDP3SecurityID sec_id) const {
|
|
auto m = _me->matchers.find(sec_id);
|
|
return m == _me->matchers.end() ? nullptr : m->second;
|
|
}
|
|
|
|
FifoMergedSecurityMatcher* FifoMatcherContext::_get_or_add_matcher(MDP3SecurityID sec_id) {
|
|
auto m = _me->matchers.find(sec_id);
|
|
if (m == _me->matchers.end()) {
|
|
auto* new_m = new FifoMergedSecurityMatcher({sec_id, _me->ignore_ops_on_deleted_orders});
|
|
_me->matchers.insert(std::make_pair(sec_id, new_m));
|
|
return new_m;
|
|
}
|
|
return m->second;
|
|
}
|
|
|
|
std::tuple<FifoPriceLevelMatcher*, MatcherOrder*>
|
|
FifoMatcherContext::find_order(MDP3SecurityID sec_id, MDP3OrderKey ok) {
|
|
auto* m = _get_matcher(sec_id);
|
|
if (m != nullptr)
|
|
return m->matcher()->find_order(ok);
|
|
return { nullptr, nullptr };
|
|
}
|
|
|
|
///////////////////////
|
|
// order callbacks
|
|
///////////////////////
|
|
void FifoMatcherContext::on_order_change(
|
|
MDP3SecurityID sec_id,
|
|
const std::vector<SmallOrderChange>& changes,
|
|
std::vector<MatchEffect>* effects,
|
|
FillDepot& fills) {
|
|
//
|
|
// SUMMARIZE(_log_header());
|
|
_ensure_sync_implied_candidates();
|
|
auto* matcher = _get_or_add_matcher(sec_id);
|
|
bts_assert(matcher != nullptr);
|
|
for (auto ch : changes)
|
|
matcher->on_order_change(ch, effects, fills);
|
|
}
|
|
|
|
void FifoMatcherContext::on_order_change(
|
|
const SmallOrderChange& change, std::vector<MatchEffect>* effects, FillDepot& fills) {
|
|
//
|
|
on_order_change(change.order.security, {change}, effects, fills);
|
|
}
|
|
|
|
///////////////////////
|
|
// diagnostic
|
|
///////////////////////
|
|
std::optional<std::string> FifoMatcherContext::has_same_orders(MDP3SecurityID sec, const Orders& orders) {
|
|
return _get_matcher(sec)->matcher()->has_same_orders(orders);
|
|
}
|
|
|
|
void FifoMatcherContext::clear() {
|
|
for (auto kv : _me->matchers)
|
|
kv.second->matcher()->clear();
|
|
}
|
|
|
|
using BBORow = std::tuple<std::string, bts::md::IntPrice, MDP3Quantity, bts::md::IntPrice, MDP3Quantity>;
|
|
|
|
void FifoMatcherContext::print_all_bbos(std::ostream& out) {
|
|
std::vector<BBORow> rows;
|
|
for (auto kv : _me->matchers) {
|
|
auto bid = kv.second->matcher()->book_level(api::Side::SIDE_BUY, 0);
|
|
auto ask = kv.second->matcher()->book_level(api::Side::SIDE_SELL, 0);
|
|
rows.emplace_back(
|
|
std::make_tuple(_me->symbols[kv.first], std::get<0>(bid), std::get<1>(bid),
|
|
std::get<0>(ask), std::get<1>(ask)));
|
|
}
|
|
std::sort(rows.begin(), rows.end(), [](const BBORow& lhs, const BBORow& rhs) {
|
|
return security_comp::SymbolComp()({std::get<0>(lhs)}, {std::get<0>(rhs)});
|
|
});
|
|
bts::spartan::table_display::print_table(out, rows.begin(), rows.end(),
|
|
std::vector<std::tuple<std::string, int, std::function<std::string(BBORow)>>> {
|
|
{ " Security ", 12, [](auto r) { return std::get<0>(r); } },
|
|
{ " Qty ", 10, [](auto r) { return FMT(std::get<2>(r)); } },
|
|
{ " Bid ", 10, [](auto r) { return FMT(std::get<1>(r)); } },
|
|
{ " Ask ", 10, [](auto r) { return FMT(std::get<3>(r)); } },
|
|
{ " Qty ", 10, [](auto r) { return FMT(std::get<4>(r)); } }
|
|
});
|
|
}
|
|
|
|
void FifoMatcherContext::_ensure_sync_implied_candidates() {
|
|
if (!_me->implied_candidates_dirty)
|
|
return;
|
|
for (auto p : _me->matchers) {
|
|
std::vector<LegData> candidates = _me->contract_matrix.implied_mkts(p.first);
|
|
for (auto can : candidates) {
|
|
auto* imatcher = p.second->add_implied_matcher(p.first);
|
|
for (auto& leg : can) {
|
|
std::string sym;
|
|
auto it = _me->symbols.find(leg.first);
|
|
if (it != _me->symbols.end())
|
|
sym = it->second;
|
|
imatcher->add_leg(
|
|
leg.first, _get_or_add_matcher(leg.first)->matcher(),
|
|
std::get<1>(leg.second), sym);
|
|
}
|
|
}
|
|
}
|
|
_me->implied_candidates_dirty = false;
|
|
}
|
|
} // namespace bts::spartan::mdp3
|