Files
spark/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp
T
John Colvin 3efb4da14b diag: Add Trace parameter to matcher constructor
This makes it easy for matcher-validation commands to bind trace
parameters, such as order/event ids, to cli options.
2021-02-01 22:56:21 -06:00

406 lines
15 KiB
C++

#include "back_testing/matcher/FifoMergedSecurityMatcher.h"
#include "back_testing/matcher/FifoImpliedSecurityMatcher.h"
#include "back_testing/matcher/FifoSecurityMatcher.h"
#include "back_testing/matcher/fill_factory.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/ActionSummarizerMacros.h"
#include "utils/TableDisplay.h"
#include <range/v3/algorithm.hpp>
#include <range/v3/numeric.hpp>
#include <range/v3/view.hpp>
#define _LOG(level, msg) LOG(OrderMan, level, "{} {}", _log_header(), msg)
#define DIAG(...) _LOG(Diag, bts::orderman::Formatter::fmt(__VA_ARGS__))
#define INFO(...) _LOG(Info, bts::orderman::Formatter::fmt(__VA_ARGS__))
#define WARN(...) _LOG(Info, bts::orderman::Formatter::fmt(__VA_ARGS__))
#define ERROR(...) _LOG(Error, bts::orderman::Formatter::fmt(__VA_ARGS__))
using namespace ranges;
using namespace bts::spartan::mdp3::book_utils;
namespace bts::spartan::mdp3 {
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),
matcher(new FifoSecurityMatcher(
{args.security_id, 0, args.ignore_ops_on_deleted_orders,
nullptr, args.trace})),
implied_matchers() {}
~PImpl() {
for (uint32_t i = 0; i < implied_matchers.size(); i++)
delete implied_matchers[i];
implied_matchers.clear();
}
const SecurityID security_id;
const bool ignore_ops_on_deleted_orders;
const Trace trace;
bool implied_matching_status;
// the Security matcher is kept in a shared_ptr
// so it can be passed around to one or more ImpliedMatchers.
// Note: if a signficant performance penalty is attributed
// to the use of shared_ptrs here, it should be fine if this
// class deletes them in its destructor.
// It seems inappropriate for an ImpliedMatcher to outlive
// the SecurityMatchers that make up its legs anyways.
std::shared_ptr<FifoSecurityMatcher> matcher;
std::vector<FifoImpliedSecurityMatcher *> implied_matchers;
};
///////////////////////
// constructors
///////////////////////
FifoMergedSecurityMatcher::FifoMergedSecurityMatcher(
const FifoMergedSecurityMatcher::InitArgs &args)
: _me(new FifoMergedSecurityMatcher::PImpl(args)) {}
FifoMergedSecurityMatcher::~FifoMergedSecurityMatcher() {}
///////////////////////
// diagnostic related
///////////////////////
std::string FifoMergedSecurityMatcher::_log_header() const {
return FMT("[", security_id(), "]", "FifoMergedSecurityMatcher");
}
SecurityID FifoMergedSecurityMatcher::security_id() const {
return _me->security_id;
}
///////////////////////
// matcher accessors
///////////////////////
FifoSecurityMatcher *FifoMergedSecurityMatcher::matcher() {
return _me->matcher.get();
}
const FifoSecurityMatcher *FifoMergedSecurityMatcher::matcheri() const {
return _me->matcher.get();
}
const std::vector<FifoImpliedSecurityMatcher *> &
FifoMergedSecurityMatcher::implied_matchers() {
return _me->implied_matchers;
}
FifoImpliedSecurityMatcher *
FifoMergedSecurityMatcher::add_implied_matcher(SecurityID sec) {
auto *m = new FifoImpliedSecurityMatcher(sec);
_me->implied_matchers.push_back(m);
return m;
}
FifoImpliedSecurityMatcher *
FifoMergedSecurityMatcher::_best_implied_matcher(Side side) {
if (!_me->implied_matching_status)
return nullptr;
FifoImpliedSecurityMatcher *rv = nullptr;
Price best_price;
for (auto &im : _me->implied_matchers) {
auto itop = im->top_of_book(side);
if (itop.quantity.value() > 0 && itop.price.is_better(side, best_price)) {
rv = im;
best_price = itop.price;
}
}
return rv;
}
bool FifoMergedSecurityMatcher::implied_matching_status() const {
return _me->implied_matching_status;
}
void FifoMergedSecurityMatcher::implied_matching_status(bool value) {
_me->implied_matching_status = value;
}
void FifoMergedSecurityMatcher::on_order_change(
const SmallOrderChange &ch,
std::vector<MatchEffect> *effects,
FillDepot &fills) {
if (!implied_matching_status()) {
// there is no "merged matching" when implied matching is off,
// just pass the order changes the direct matcher.
matcher()->on_order_change(ch, effects);
return;
}
bts_assert_msg(
ch.affect_book, "affect-free matching is not supported in "
"markets with implied matching.");
switch (ch.change) {
case OrderChange::ChangeType::updated_price:
case OrderChange::ChangeType::updated: {
auto prev_ord_tup = matcher()->find_order(ch.order.key);
auto prev_ord = std::get<1>(prev_ord_tup);
if (prev_ord == nullptr) {
bts_assert_msg(
_me->ignore_ops_on_deleted_orders,
"unable to process order-update event: failed to find order.");
break;
}
if (ch.order.price.is_better(ch.order.side, prev_ord->price)) {
// price improved, try to fill the order
auto new_ch = ch;
_fill(new_ch.order, effects, fills, false);
if (new_ch.order.quantity.value() == 0) {
matcher()->on_order_completed(ch.order.key);
} else {
matcher()->on_order_change(new_ch, effects);
}
} else {
// As far as we know, the only type of update that could trigger fills
// is a price improvement. It is worth noting, however, that there is
// the potential for this assumption to be invalid. In a very strange
// market, we may observe down the road: an implied market with a leg
// for this security's same side market, that also has a leg size
// greater than 1.
matcher()->on_order_change(ch);
}
break;
}
case OrderChange::ChangeType::added: {
auto new_ch = ch;
_fill(new_ch.order, effects, fills, false);
if (get_total_qty(new_ch.order).value() > 0)
matcher()->on_order_change(new_ch);
break;
}
case OrderChange::ChangeType::removed: {
matcher()->on_order_change(ch);
break;
}
}
}
/////////////////////////
// books
/////////////////////////
BookSide FifoMergedSecurityMatcher::book(Side side) {
return matcher()->book(side);
}
ImpliedBookSide FifoMergedSecurityMatcher::implied_book(Side side) {
if (_me->implied_matchers.empty())
return ImpliedBookSide();
// In theory, computing the quantity of any price level beyond the top of the
// book should be done in a hypothetical scenario, where an order with the
// specified price and infinite qty is partially filled.
//
// This accounts for the possibility that implied markets may have securities
// in common, and therefore filling an order in one implied market may impact
// another implied market.
//
// However, in practice, the implied market candidates the exchange uses are
// independent.
//
// TODO: this should be a linked list, as
// the only functionality needed is forward iteration,
// and erase (the latter for optimization).
std::vector<std::tuple<ImpliedBookSide, int>> ibooks;
auto oside = other_side(side);
bts::orderman::SidePriceComp cmp(map_side(oside));
for (auto it = _me->implied_matchers.begin();
it != _me->implied_matchers.end(); it++) {
auto res = ibooks.emplace_back((*it)->book(oside), 0);
auto &ib = std::get<0>(res);
if (ib[0].price.is_nan())
ibooks.pop_back();
}
auto is_exhausted = [&](const std::tuple<ImpliedBookSide, int> &ib_tup) {
const auto &ib = std::get<0>(ib_tup);
auto i = std::get<1>(ib_tup);
return static_cast<uint32_t>(i) >= ib.size() || ib[i].price.is_nan();
};
auto rem_exhausted = [&]() {
ibooks.erase(std::remove_if(ibooks.begin(), ibooks.end(), is_exhausted));
};
ImpliedBookSide dest;
for (auto &curr : dest) {
// find best price
curr.price = std::get<0>(*max_element(
ibooks, cmp,
[](const auto &ibt) {
return std::get<0>(ibt)[std::get<1>(ibt)].price.to_double();
}))[0]
.price;
if (curr.price.is_nan())
break;
curr.quantity = Quantity(0);
// combine quantities of implied books with the same top price, as there
// could be multiple.
for (auto &ib_tup : ibooks) {
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());
}
rem_exhausted();
}
return dest;
}
void FifoMergedSecurityMatcher::merged_book(
Side side, std::vector<BookLevel> &dest) {
//
std::vector<std::tuple<ImpliedBookSide, int>> ibooks;
auto oside = other_side(side);
bts::orderman::SidePriceComp cmp(map_side(oside));
for (auto it = _me->implied_matchers.begin();
it != _me->implied_matchers.end(); it++) {
auto res = ibooks.emplace_back((*it)->book(oside), 0);
auto &ib = std::get<0>(res);
if (ib[0].price.is_nan())
ibooks.pop_back();
}
auto dbook = matcher()->book(side);
int dindex = 0;
auto is_exhausted = [&](const std::tuple<ImpliedBookSide, int> &ib_tup) {
const auto &ib = std::get<0>(ib_tup);
auto i = std::get<1>(ib_tup);
return static_cast<uint32_t>(i) >= ib.size() || ib[i].price.is_nan();
};
while (true) {
// erase empties
ibooks.erase(
std::remove_if(ibooks.begin(), ibooks.end(), is_exhausted),
ibooks.end());
// dir top price/qty
Price dprice(
static_cast<uint32_t>(dindex) < dbook.size() ? dbook[dindex].price
: Price());
Quantity dqty(dprice.is_nan() ? 0 : dbook[dindex].quantity.value());
// init best price/qty
Price best_price(dprice);
Quantity curr_qty(0);
// find best impl price
for (auto &ib_tup : ibooks) {
auto &ib = std::get<0>(ib_tup);
auto index = std::get<1>(ib_tup);
if (cmp(ib[index].price, best_price))
best_price = ib[index].price;
}
// combine quantities of books with the same top price
for (auto &ib_tup : ibooks) {
auto &ib = std::get<0>(ib_tup);
auto &index = std::get<1>(ib_tup);
auto &bl = ib[index];
if (bl.price == best_price) {
curr_qty = Quantity(curr_qty.value() + bl.quantity.value());
index++;
}
}
if (dprice == best_price) {
curr_qty = Quantity(curr_qty.value() + dqty.value());
dindex++;
}
if (curr_qty.value() == 0)
break;
dest.push_back({best_price, curr_qty});
}
}
void FifoMergedSecurityMatcher::_fill(
MDP3Order &order,
std::vector<MatchEffect> *effects,
FillDepot &fills,
bool reset_show_qty) {
auto rem = _fill(
order.key, order.side, order.price, get_total_qty(order), effects,
fills);
reset_show_qty |= rem == get_total_qty(order);
set_total_qty(order, rem, reset_show_qty);
}
inline SmallBookLevel
tuple_to_small_book_level(std::tuple<Price, Quantity> t) {
return SmallBookLevel(std::get<0>(t), std::get<1>(t));
}
Quantity FifoMergedSecurityMatcher::_fill(
OrderKey key,
Side side,
Price price,
Quantity quantity,
std::vector<MatchEffect> *effects,
FillDepot &fills) {
//
auto oside = other_side(side);
bts::orderman::SidePriceComp oside_comp(map_side(oside));
while (quantity.value() > 0) {
// Fill one price level at a time.
// While one market may have the best 1st price level,
// another may have the best 2nd price level.
auto *imatcher = _best_implied_matcher(oside);
auto itop =
imatcher == nullptr ? SmallBookLevel() : imatcher->top_of_book(oside);
auto dtop = tuple_to_small_book_level(matcher()->book_level(oside, 0));
bool is_dcross = !dtop.price.is_nan() && !oside_comp(price, dtop.price);
bool is_icross = !itop.price.is_nan() && !oside_comp(price, itop.price);
if (!is_dcross && !is_icross)
break;
// the order crossed at least one of the markets
auto ord_desc = [&]() { return fmt_order(key, side, price, quantity); };
if (!is_dcross || (is_icross && oside_comp(itop.price, dtop.price))) {
// fill with the implied market
DIAG(
"filling ", ord_desc(), " in implied market ",
fmt_price_level(oside, itop));
auto target_fill_qty = std::min(quantity, itop.quantity);
auto rem = imatcher->fill(key, side, target_fill_qty, effects, fills);
auto fill_qty = target_fill_qty - rem;
quantity -= fill_qty;
DIAG("filled ", fill_qty, ". Order now: ", ord_desc());
} else {
// fill with the direct market
DIAG(
"filling ", ord_desc(), " in direct market ",
fmt_price_level(oside, dtop));
bool local_effects = effects == nullptr;
if (local_effects)
effects = new std::vector<MatchEffect>();
uint32_t fills_pos = effects->size();
auto target_fill_qty = std::min(quantity, dtop.quantity);
auto rem = matcher()->fill(
key, side, dtop.price, target_fill_qty, effects, true);
auto fill_qty = target_fill_qty - rem;
quantity -= fill_qty;
DIAG("filled ", fill_qty, ". Order now: ", ord_desc());
MDP3Fill aggr_fill;
bts_assert(aggr_fill.price.is_nan());
aggr_fill.key = key;
aggr_fill.aggressor = _to_aggressor(side);
for (auto it = effects->begin() + fills_pos; it != effects->end();
it++) {
auto &df = *it;
bts_assert(std::holds_alternative<SimOrderUpdate>(df));
auto &upd = std::get<SimOrderUpdate>(df);
if (upd.key == key) {
if (aggr_fill.price.is_nan()) {
aggr_fill.price = upd.last_price;
aggr_fill.quantity = upd.last_fills;
} else if (aggr_fill.price == upd.last_price) {
aggr_fill.quantity = Quantity(
aggr_fill.quantity.value() + upd.last_fills.value());
} else {
fills.add_fill(security_id(), key, aggr_fill);
aggr_fill.quantity = Quantity(0);
aggr_fill.price = Price();
bts_assert(aggr_fill.price.is_nan());
}
} else {
fills.add_fill(security_id(), key, _to_passive_fill(upd, false));
}
}
if (local_effects) {
delete effects;
effects = nullptr;
}
if (!aggr_fill.price.is_nan()) {
fills.add_fill(security_id(), key, aggr_fill);
}
}
}
bts_assert(quantity.value() >= 0);
return quantity;
}
} // namespace bts::spartan::mdp3