refactor: Clean up FifoImpliedSecurityMatcher

* Remove dependency on symbol (was only there to make logs prettier, but
  sec id alone provides sufficient information)

* Remove several includes from the header to reduce compilation overhead
This commit is contained in:
John Colvin committed 2021-02-01 23:44:21 -06:00
1 parent b66241b006
commit 290b5d9da2
3 files changed
+18 -111

No files matched your search

@@ -1,71 +1,29 @@
#include "back_testing/matcher/FifoImpliedSecurityMatcher.h"
#include "FifoImpliedSecurityMatcher.h"
#include "FifoSecurityMatcher.h"
#include "lib/domain_model/Security.h"
#include "back_testing/matcher/fill_factory.h"
#include "back_testing/sim/sim_common.h"
#include "lib/domain_model/Security.h"
#include "lib/md/IntPrice.h"
#include "lib/orderman/orderman_utils/Formatter.h"
#include "lib/orderman/orderman_utils/PriceComp.h"
#include "market_data/utils/book_utils.h"
#include "market_data/utils/order_utils.h"
#include "utils/ActionSummarizer.h"
#include "utils/ActionSummarizerMacros.h"
#include <limits>
#include <variant>
// 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 _BookLevel = std::tuple<Price, Quantity>;
struct FifoImpliedSecurityMatcher::PImpl {
PImpl(SecurityID security_id, std::string symbol)
: security_id(security_id), implied_mkt(), symbol(symbol),
leg_symbols() {}
PImpl(SecurityID security_id)
: security_id(security_id), implied_mkt() {}
SecurityID security_id;
std::map<SecurityID, std::tuple<FifoSecurityMatcher *, Quantity>>
implied_mkt;
// symbol/leg_symbols serve diagnostic purposes
// (easier to read logs w/ actual symbols than sec ids)
std::string symbol;
std::map<SecurityID, std::string> leg_symbols;
};
///////////////////////
// constructors
///////////////////////
FifoImpliedSecurityMatcher::FifoImpliedSecurityMatcher(
SecurityID security_id)
: FifoImpliedSecurityMatcher(security_id, std::string()) {}
FifoImpliedSecurityMatcher::FifoImpliedSecurityMatcher(
SecurityID security_id, std::string symbol)
: _me(new FifoImpliedSecurityMatcher::PImpl(security_id, symbol)) {}
FifoImpliedSecurityMatcher::FifoImpliedSecurityMatcher(SecurityID security_id)
: _me(new FifoImpliedSecurityMatcher::PImpl(security_id)) {}
FifoImpliedSecurityMatcher::~FifoImpliedSecurityMatcher() {}
@@ -75,35 +33,6 @@ FifoImpliedSecurityMatcher::~FifoImpliedSecurityMatcher() {}
SecurityID FifoImpliedSecurityMatcher::security_id() const {
return _me->security_id;
}
std::string FifoImpliedSecurityMatcher::symbol() const { return _me->symbol; }
std::string FifoImpliedSecurityMatcher::_log_header() const {
return FMT(
"FifoImpliedSecurityMatcher (", security_id(), ", ", symbol(), ")");
}
void FifoImpliedSecurityMatcher::print_legs(std::ostream &out) const {
bool append_bbo = true;
for (const auto &kv : _me->implied_mkt) {
auto leg_size = std::get<1>(kv.second);
std::string leg_sym;
auto it = _me->leg_symbols.find(kv.first);
if (it != _me->leg_symbols.end()) {
leg_sym = it->second;
}
out << std::setw(12) << kv.first << " " << std::setw(12) << leg_sym
<< " " << leg_size;
if (append_bbo) {
append_bbo = false;
auto bid = top_of_book(Side::Buy);
auto ask = top_of_book(Side::Sell);
out << std::setw(8) << FMT("(", bid.quantity, ") ") << std::setw(6)
<< bid.price << std::setw(6) << ask.price
<< FMT("(", ask.quantity, ")");
}
out << std::endl;
}
}
///////////////////////
// book related
@@ -146,12 +75,8 @@ void FifoImpliedSecurityMatcher::_book(
}
void FifoImpliedSecurityMatcher::add_leg(
SecurityID sec_id,
FifoSecurityMatcher *m,
Quantity unit_qty,
std::string leg_sym) {
SecurityID sec_id, FifoSecurityMatcher *m, Quantity unit_qty) {
_me->implied_mkt.insert({sec_id, {m, unit_qty}});
_me->leg_symbols.insert_or_assign(sec_id, leg_sym);
}
Quantity FifoImpliedSecurityMatcher::fill(
@@ -180,10 +105,8 @@ Quantity FifoImpliedSecurityMatcher::fill(
effects = new std::vector<MatchEffect>();
uint32_t leg_fills_pos = effects->size();
auto raw_leg_fill_qty = qty * abs(leg_size);
// INFO("expecting to fill ", raw_leg_fill_qty);
auto raw_leg_rem_qty = m.fill(
key, leg_side, Price(), raw_leg_fill_qty, effects, false);
// INFO("remaining (unfilled) qty: ", raw_leg_rem_qty);
auto raw_leg_rem_qty =
m.fill(key, leg_side, Price(), raw_leg_fill_qty, effects, false);
bts_assert(raw_leg_rem_qty.value() == 0);
for (auto it = effects->begin() + leg_fills_pos; it != effects->end();
it++) {
@@ -1,25 +1,19 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "back_testing/matcher/FifoSecurityMatcher.h"
#include "back_testing/common/MatchEffect.h"
#include "back_testing/matcher/fill_depot.h"
#include "domain/SecuritySnapshot.h"
#include "market_data/common/mdp3_common.h"
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
namespace bts::spartan::mdp3 {
class FifoSecurityMatcher;
class FifoImpliedSecurityMatcher {
public:
FifoImpliedSecurityMatcher(SecurityID security_id);
// Symbol/sec id serve diagnostic purposes, but aren't absolutely neccessary.
// Logs made by this class begin with a header that includes the symbol/sec
// id.
FifoImpliedSecurityMatcher(SecurityID security_id, std::string symbol);
~FifoImpliedSecurityMatcher();
ImpliedBookSide book(Side) const;
@@ -37,15 +31,10 @@ class FifoImpliedSecurityMatcher {
// FifoMergedSecurityMatcher.
// Note the absense of a 'side' parameter: unit_qty should be positive for
// Buys, negative for Sells.
void add_leg(
SecurityID sec_id,
FifoSecurityMatcher *m,
Quantity unit_qty,
std::string leg_sym);
void add_leg(SecurityID sec_id, FifoSecurityMatcher *m, Quantity unit_qty);
SecurityID security_id() const;
std::string symbol() const;
SmallBookLevel top_of_book(Side side) const;
void print_legs(std::ostream &out = std::cout) const;
private:
struct PImpl;
@@ -53,8 +42,8 @@ class FifoImpliedSecurityMatcher {
// hide copy ctr
FifoImpliedSecurityMatcher(const FifoImpliedSecurityMatcher &);
std::string _log_header() const;
void _book(Side side, SmallBook &dest, uint32_t depth) const;
uint32_t _fill_order(const MDP3Order &order);
};
} // namespace bts::spartan::mdp3
@@ -250,15 +250,10 @@ void FifoMatcherContext::_ensure_sync_implied_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;
for (auto &leg : can)
imatcher->add_leg(
leg.first, _get_or_add_matcher(leg.first)->matcher(),
std::get<1>(leg.second), sym);
}
std::get<1>(leg.second));
}
}
_me->implied_candidates_dirty = false;