refactor: Re-implement implied market logic in 'ImpliedMarketModel'
This commit is contained in:
1 parent
290b5d9da2
commit
1cfb7ee6a4
8 files changed
+295
-240
No files matched your search
@@ -23,18 +23,6 @@ namespace bts::spartan::mdp3 {
|
||||
///////////////////////
|
||||
// static inline std::string _log_header() { return "FifoMatcherContext"; }
|
||||
|
||||
static inline Quantity _norm_qty(Side side, Quantity qty) {
|
||||
switch (side) {
|
||||
case Side::Buy:
|
||||
return qty;
|
||||
case 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;
|
||||
const Trace trace;
|
||||
@@ -43,7 +31,7 @@ struct FifoMatcherContext::PImpl {
|
||||
// re-evaluated before any susbequent order-matching (re-evaluation is
|
||||
// performed lazily).
|
||||
bool implied_candidates_dirty;
|
||||
ImpliedMarket contract_matrix;
|
||||
ImpliedMarketModel implied_market_model;
|
||||
flat_map<SecurityID, FifoMergedSecurityMatcher *> matchers;
|
||||
flat_map<SecurityID, std::string> symbols;
|
||||
|
||||
@@ -76,21 +64,25 @@ FifoMatcherContext::~FifoMatcherContext() {}
|
||||
bool FifoMatcherContext::add_security(
|
||||
SecurityID sec_id,
|
||||
const std::string &symbol,
|
||||
const std::vector<LegDef> &legs) {
|
||||
const LegVec &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);
|
||||
LegMap ic;
|
||||
// Add the legs explicitly (ImpliedMarketBuilder doesn't know about
|
||||
// 'SecurityDefinition,' 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, Quantity(1));
|
||||
auto &l = ic[sec_id];
|
||||
l.security_id = sec_id;
|
||||
l.side = Side::Buy;
|
||||
l.ratio = Quantity(1);
|
||||
} else {
|
||||
for (auto l : legs)
|
||||
ic.add_leg(l.security_id, _norm_qty(l.side, l.ratio));
|
||||
for (const auto &l : legs)
|
||||
ic.emplace(l.security_id, l);
|
||||
}
|
||||
_me->implied_market_model.insert(sec_id, ic);
|
||||
_me->matchers.insert(std::make_pair(
|
||||
sec_id,
|
||||
new FifoMergedSecurityMatcher(
|
||||
@@ -110,7 +102,7 @@ bool FifoMatcherContext::remove_security(SecurityID 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->implied_market_model.erase(sec_id);
|
||||
_me->symbols.erase(sec_id);
|
||||
_me->implied_candidates_dirty = true;
|
||||
return true;
|
||||
@@ -245,15 +237,19 @@ 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 (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::vector<LegMap> 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)
|
||||
continue;
|
||||
for (const auto &[sec, leg] : can)
|
||||
imatcher->add_leg(
|
||||
leg.first, _get_or_add_matcher(leg.first)->matcher(),
|
||||
std::get<1>(leg.second));
|
||||
sec, _get_or_add_matcher(sec)->matcher(),
|
||||
leg.side == Side::Buy ? leg.ratio : -leg.ratio);
|
||||
}
|
||||
}
|
||||
_me->implied_candidates_dirty = false;
|
||||
|
||||
@@ -28,7 +28,7 @@ struct FifoMatcherContext {
|
||||
bool add_security(
|
||||
SecurityID sec_id,
|
||||
const std::string &symbol,
|
||||
const std::vector<LegDef> &legs);
|
||||
const LegVec &legs);
|
||||
bool remove_security(SecurityID sec_id);
|
||||
void implied_matching_status(SecurityID sec_id, bool value);
|
||||
bool implied_matching_status(SecurityID sec_id) const;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "FifoMergedSecurityMatcher.h"
|
||||
#include "back_testing/matcher/fill_factory.h"
|
||||
#include "FifoImpliedSecurityMatcher.h"
|
||||
#include "FifoSecurityMatcher.h"
|
||||
#include "fill_factory.h"
|
||||
#include "common/MatcherOneof.h"
|
||||
#include "common/MatchingSession.h"
|
||||
#include "lib/md/IntPrice.h"
|
||||
@@ -8,7 +10,7 @@
|
||||
#include "market_data/utils/book_utils.h"
|
||||
#include "market_data/utils/order_utils.h"
|
||||
#include "utils/ActionSummarizerMacros.h"
|
||||
#include "utils/TableDisplay.h"
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <range/v3/algorithm.hpp>
|
||||
#include <range/v3/numeric.hpp>
|
||||
#include <range/v3/view.hpp>
|
||||
@@ -24,6 +26,39 @@ using namespace bts::spartan::mdp3::book_utils;
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
struct LegComp {
|
||||
bool operator()(const LegDef &lhs, const LegDef &rhs) {
|
||||
if (lhs.security_id != rhs.security_id)
|
||||
return lhs.security_id < rhs.security_id;
|
||||
if (lhs.side != rhs.side)
|
||||
return lhs.side < rhs.side;
|
||||
return lhs.ratio < rhs.ratio;
|
||||
}
|
||||
};
|
||||
|
||||
struct LegMapComp {
|
||||
bool operator()(const LegMap &lhs, const LegMap &rhs) const {
|
||||
auto lit = lhs.begin();
|
||||
auto rit = rhs.begin();
|
||||
while (lit != lhs.end() && rit != rhs.end()) {
|
||||
if (lit->second == rit->second) {
|
||||
lit++;
|
||||
rit++;
|
||||
}
|
||||
return LegComp()(lit->second, rit->second);
|
||||
}
|
||||
return lhs.size() < rhs.size();
|
||||
}
|
||||
};
|
||||
|
||||
// The number of legs in an implied market is presumably very small (for ex, GC
|
||||
// implieds never have more than 2 legs), so storing the implied markets in a
|
||||
// flat map whose keys are themselves a flat_map (of the implied market's legs)
|
||||
// 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<LegMap, FifoImpliedSecurityMatcher*, LegMapComp>;
|
||||
|
||||
struct FifoMergedSecurityMatcher::PImpl {
|
||||
PImpl(const FifoMergedSecurityMatcher::InitArgs &args)
|
||||
: security_id(args.security_id),
|
||||
@@ -36,8 +71,8 @@ struct FifoMergedSecurityMatcher::PImpl {
|
||||
implied_matchers() {}
|
||||
|
||||
~PImpl() {
|
||||
for (uint32_t i = 0; i < implied_matchers.size(); i++)
|
||||
delete implied_matchers[i];
|
||||
for (auto p : implied_matchers)
|
||||
delete p.second;
|
||||
implied_matchers.clear();
|
||||
}
|
||||
|
||||
@@ -53,7 +88,7 @@ struct FifoMergedSecurityMatcher::PImpl {
|
||||
// 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;
|
||||
ImpliedMatchers implied_matchers;
|
||||
};
|
||||
|
||||
///////////////////////
|
||||
@@ -83,11 +118,14 @@ FifoSecurityMatcher *FifoMergedSecurityMatcher::matcher() {
|
||||
const FifoSecurityMatcher *FifoMergedSecurityMatcher::matcheri() const {
|
||||
return _me->matcher.get();
|
||||
}
|
||||
FifoImpliedSecurityMatcher *
|
||||
FifoMergedSecurityMatcher::add_implied_matcher(SecurityID sec) {
|
||||
auto *m = new FifoImpliedSecurityMatcher(sec);
|
||||
_me->implied_matchers.push_back(m);
|
||||
return m;
|
||||
|
||||
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());
|
||||
return res.first->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool FifoMergedSecurityMatcher::implied_matching_status() const {
|
||||
@@ -202,7 +240,7 @@ ImpliedBookSide FifoMergedSecurityMatcher::implied_book(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 res = ibooks.emplace_back(it->second->book(oside), 0);
|
||||
auto &ib = std::get<0>(res);
|
||||
if (ib[0].price.is_nan())
|
||||
ibooks.pop_back();
|
||||
@@ -249,7 +287,7 @@ void FifoMergedSecurityMatcher::merged_book(
|
||||
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 res = ibooks.emplace_back(it->second->book(oside), 0);
|
||||
auto &ib = std::get<0>(res);
|
||||
if (ib[0].price.is_nan())
|
||||
ibooks.pop_back();
|
||||
@@ -321,9 +359,12 @@ Quantity FifoMergedSecurityMatcher::_fill(
|
||||
std::vector<MatchEffect> *effects,
|
||||
FillDepot &fills) {
|
||||
//
|
||||
std::vector<FifoImpliedSecurityMatcher*> imatcher_vec;
|
||||
for (auto& p : _me->implied_matchers)
|
||||
imatcher_vec.push_back(p.second);
|
||||
auto oside = other_side(side);
|
||||
MatchingSession match_session(
|
||||
*_me->matcher, _me->implied_matchers, oside, price);
|
||||
*_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 &&
|
||||
|
||||
@@ -28,7 +28,7 @@ class FifoMergedSecurityMatcher {
|
||||
bool implied_matching_status() const;
|
||||
void on_order_change(
|
||||
const SmallOrderChange &, std::vector<MatchEffect> *, FillDepot &, AlgoEventID = no_algo_event());
|
||||
FifoImpliedSecurityMatcher *add_implied_matcher(SecurityID);
|
||||
FifoImpliedSecurityMatcher *add_implied_matcher(const LegMap &);
|
||||
FifoSecurityMatcher *matcher();
|
||||
const FifoSecurityMatcher *matcheri() const;
|
||||
// books
|
||||
|
||||
@@ -40,8 +40,7 @@ void MergedMatcher::add_def(const SecurityDefinition &def) {
|
||||
m = _me->matcher_map.emplace(ass, FifoMatcherContext({true, _me->trace}))
|
||||
.first;
|
||||
m->second.add_security(
|
||||
def.security_id(), std::string(def.symbol()),
|
||||
std::vector<LegDef>{def.legs().begin(), def.legs().end()});
|
||||
def.security_id(), std::string(def.symbol()), def.legs());
|
||||
m->second.implied_matching_status(def.security_id(), def.implied_matching());
|
||||
}
|
||||
|
||||
|
||||
+1
-5
@@ -428,11 +428,7 @@ void MatcherValidationObserver::on_event_end(MDP3Event const &ev) {
|
||||
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(),
|
||||
std::vector<LegDef>{
|
||||
s->definition().legs().begin(), s->definition().legs().end()}
|
||||
|
||||
);
|
||||
s->security_id(), s->symbol(), s->definition().legs());
|
||||
_me->exch_matcher.implied_matching_status(
|
||||
s->security_id(), s->definition().implied_matching());
|
||||
}
|
||||
|
||||
@@ -1,163 +1,165 @@
|
||||
#include "implied_market.h"
|
||||
#include "lib/utils/MapUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "server/sparkd/cmds/spartan/domain/SecurityDefinition.h"
|
||||
#include "server/sparkd/cmds/spartan/utils/Permutation.h"
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
///////////////////////
|
||||
// stateless helpers
|
||||
///////////////////////
|
||||
inline std::function<bool(const ImpliedContractData *)>
|
||||
get_candidate_filter(bool is_spread) {
|
||||
if (is_spread) {
|
||||
return std::function<bool(const ImpliedContractData *)>(
|
||||
[](const auto *icd) { return icd->legs().size() == 1; });
|
||||
|
||||
/**
|
||||
* Determine if two LegMaps are equal
|
||||
*/
|
||||
static bool are_equal(const LegMap &lhs, const LegMap &rhs) {
|
||||
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply the quantities of each leg by a particular value
|
||||
*/
|
||||
static LegMap multiply(LegMap legs, int mult) {
|
||||
if (mult == 1)
|
||||
return legs;
|
||||
if (mult == 0)
|
||||
return {};
|
||||
for (auto &leg : legs) {
|
||||
leg.second.ratio.value() *= mult;
|
||||
if (leg.second.ratio < 0) {
|
||||
leg.second.side = other_side(leg.second.side);
|
||||
leg.second.ratio = -leg.second.ratio;
|
||||
}
|
||||
}
|
||||
return std::function<bool(const ImpliedContractData *)>(
|
||||
[](const auto *) { return true; });
|
||||
}
|
||||
///////////////////////
|
||||
// ImpliedContractData
|
||||
///////////////////////
|
||||
ImpliedContractData::ImpliedContractData() : _security_id(), _legs() {}
|
||||
|
||||
ImpliedContractData::ImpliedContractData(SecurityID security_id)
|
||||
: _security_id(security_id), _legs() {}
|
||||
|
||||
void ImpliedContractData::add_leg(SecurityID sec, Quantity qty) {
|
||||
if (qty.value() == 0)
|
||||
return;
|
||||
auto it = _legs.find(sec);
|
||||
if (it != _legs.end()) {
|
||||
it->second += qty;
|
||||
if (it->second.value() == 0)
|
||||
_legs.erase(sec);
|
||||
} else
|
||||
_legs.insert(std::make_pair(sec, qty));
|
||||
return legs;
|
||||
}
|
||||
|
||||
void ImpliedContractData::remove_leg(SecurityID sec, Quantity qty) {
|
||||
add_leg(sec, -qty);
|
||||
/**
|
||||
* Flip the side of each leg
|
||||
*/
|
||||
static LegMap negate(const LegMap &legs) {
|
||||
return multiply(legs, -1);
|
||||
}
|
||||
|
||||
void ImpliedContractData::multiply_legs(Quantity multiplier) {
|
||||
if (multiplier.value() == 1)
|
||||
return;
|
||||
for (auto &l : _legs)
|
||||
l.second *= multiplier;
|
||||
/**
|
||||
* Combine one collection of legs with another. Legs with new securities
|
||||
* are appended to the target collection, while legs with common securities
|
||||
* are merged together. The merged leg's quantity is the sum of the two legs'
|
||||
* quantities if they have the same side, otherwise it is the difference. If
|
||||
* the resultant quantity is zero, the leg for that security is removed; if it
|
||||
* is negative, the side is flipped, and the quantity replaced with its absolute
|
||||
* value.
|
||||
*/
|
||||
static void merge_legs(LegMap &target_legs, const LegMap &other_legs) {
|
||||
for (const auto &[osec, oleg] : other_legs) {
|
||||
auto res = target_legs.emplace(osec, oleg);
|
||||
auto &l = res.first->second;
|
||||
if (!res.second)
|
||||
l.ratio += l.side == oleg.side ? oleg.ratio : -oleg.ratio;
|
||||
if (l.ratio == 0)
|
||||
target_legs.erase(osec);
|
||||
else if (l.ratio < 0) {
|
||||
l.side = other_side(l.side);
|
||||
l.ratio = -l.ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ImpliedContractData::print_legs(std::ostream &ostr) const {
|
||||
for (auto l : legs())
|
||||
ostr << l.second << " " << l.first << " ";
|
||||
}
|
||||
/**
|
||||
* Holds per-security data needed to discover implied market candidates
|
||||
*/
|
||||
struct SecData {
|
||||
LegMap legs;
|
||||
|
||||
void ImpliedContractData::merge(
|
||||
const ImpliedContractData &other, Quantity multiplier) {
|
||||
if (multiplier.value() != 0)
|
||||
for (auto l : other.legs())
|
||||
add_leg(l.first, l.second * multiplier);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
// ImpliedMarket
|
||||
///////////////////////
|
||||
struct ImpliedMarket::PImpl {
|
||||
std::map<SecurityID, ImpliedContractData> contracts;
|
||||
std::vector<const ImpliedContractData *> contracts_vect;
|
||||
bool is_spread() const { return legs.size() > 1; }
|
||||
};
|
||||
|
||||
///////////////////////
|
||||
// constructors
|
||||
///////////////////////
|
||||
ImpliedMarket::ImpliedMarket() : _me(new ImpliedMarket::PImpl()) {}
|
||||
// destructor
|
||||
ImpliedMarket::~ImpliedMarket() {}
|
||||
struct ImpliedMarketModel::PImpl {
|
||||
std::unordered_map<SecurityID, SecData> securities;
|
||||
};
|
||||
|
||||
///////////////////////
|
||||
// contract(s) accessors
|
||||
///////////////////////
|
||||
const std::map<SecurityID, ImpliedContractData> &
|
||||
ImpliedMarket::contracts() const {
|
||||
return _me->contracts;
|
||||
ImpliedMarketModel::ImpliedMarketModel()
|
||||
: _me(new ImpliedMarketModel::PImpl()) {}
|
||||
|
||||
ImpliedMarketModel::~ImpliedMarketModel() {}
|
||||
|
||||
void ImpliedMarketModel::insert(SecurityID sec, const LegMap &legs) {
|
||||
_me->securities[sec] = {legs};
|
||||
}
|
||||
|
||||
// The returned ImpliedContractData& will not have any legs
|
||||
// (callers need to add them explicitly).
|
||||
ImpliedContractData &ImpliedMarket::add_contract(SecurityID sec) {
|
||||
auto it = _me->contracts.find(sec);
|
||||
if (it == _me->contracts.end()) {
|
||||
it = _me->contracts.emplace(sec, ImpliedContractData(sec)).first;
|
||||
_me->contracts_vect.push_back(&it->second);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
void ImpliedMarketModel::erase(SecurityID sec) { _me->securities.erase(sec); }
|
||||
|
||||
void ImpliedMarket::remove_contract(SecurityID sec) {
|
||||
_me->contracts.erase(sec);
|
||||
for (auto it = _me->contracts_vect.begin(); it != _me->contracts_vect.end();
|
||||
it++) {
|
||||
if ((*it)->security_id() == sec) {
|
||||
_me->contracts_vect.erase(it);
|
||||
break;
|
||||
void ImpliedMarketModel::clear() { _me->securities.clear(); }
|
||||
|
||||
std::vector<LegMap> ImpliedMarketModel::find_implied_markets(SecurityID sec) {
|
||||
std::vector<LegMap> dest;
|
||||
std::vector<SecurityID> securities_vec;
|
||||
for (auto it = _me->securities.begin(); it != _me->securities.end(); it++)
|
||||
securities_vec.push_back(it->first);
|
||||
std::vector<std::vector<SecurityID>> sec_sets;
|
||||
if (_me->securities[sec].is_spread()) {
|
||||
for (auto leg1_it = _me->securities.begin();
|
||||
leg1_it != _me->securities.end(); leg1_it++) {
|
||||
if (leg1_it->second.is_spread())
|
||||
continue;
|
||||
for (auto leg2_it = std::next(leg1_it);
|
||||
leg2_it != _me->securities.end(); leg2_it++) {
|
||||
if (leg2_it->second.is_spread())
|
||||
continue;
|
||||
auto &ss = sec_sets.emplace_back();
|
||||
ss.push_back(leg1_it->first);
|
||||
ss.push_back(leg2_it->first);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (auto leg1_it = _me->securities.begin();
|
||||
leg1_it != _me->securities.end(); leg1_it++) {
|
||||
for (auto leg2_it = std::next(leg1_it);
|
||||
leg2_it != _me->securities.end(); leg2_it++) {
|
||||
if (leg1_it->second.is_spread() == leg2_it->second.is_spread())
|
||||
continue;
|
||||
auto &ss = sec_sets.emplace_back();
|
||||
ss.push_back(leg1_it->first);
|
||||
ss.push_back(leg2_it->first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ImpliedMarket::_add_implied_combinations(
|
||||
const ImpliedContractData &contract,
|
||||
const ImpliedContractData &implied_contract,
|
||||
const LegData &impl_legs,
|
||||
uint32_t leg_index,
|
||||
uint32_t max_legs_remaining,
|
||||
std::vector<LegData> &dest,
|
||||
std::function<bool(const ImpliedContractData *)> candidate_filter) const {
|
||||
// add 'impl_legs' to 'dest' if contract, and implied_contract have the same
|
||||
// the legs.
|
||||
if (implied_contract.legs().size() == contract.legs().size() &&
|
||||
std::equal(
|
||||
implied_contract.legs().begin(), implied_contract.legs().end(),
|
||||
contract.legs().begin(), [](const auto &lhs, const auto &rhs) {
|
||||
return lhs.first == rhs.first && lhs.second == rhs.second;
|
||||
})) {
|
||||
dest.push_back(impl_legs);
|
||||
return;
|
||||
}
|
||||
if (max_legs_remaining == 0 || leg_index >= _me->contracts_vect.size())
|
||||
return;
|
||||
std::vector<int> mults{1, -1};
|
||||
for (; leg_index < _me->contracts_vect.size(); leg_index++) {
|
||||
if (!candidate_filter(_me->contracts_vect[leg_index]))
|
||||
continue;
|
||||
for (auto m : mults) {
|
||||
ImpliedContractData temp = implied_contract;
|
||||
temp.merge(*_me->contracts_vect[leg_index], Quantity(m));
|
||||
auto mod_legs = impl_legs;
|
||||
mod_legs.insert(std::make_pair(
|
||||
_me->contracts_vect[leg_index]->security_id(),
|
||||
std::make_tuple(*_me->contracts_vect[leg_index], m)));
|
||||
_add_implied_combinations(
|
||||
contract, temp, mod_legs, leg_index + 1, max_legs_remaining - 1,
|
||||
dest, candidate_filter);
|
||||
// Add implied market if the legs match the target
|
||||
auto add_candidate_if_match = [&](const std::vector<SecurityID> &leg_secs,
|
||||
const std::vector<int> &leg_qtys,
|
||||
const LegMap &fut_legs) {
|
||||
if (are_equal(_me->securities[sec].legs, fut_legs)) {
|
||||
auto &implied_legs = dest.emplace_back();
|
||||
for (uint32_t i = 0; i < leg_secs.size(); i++) {
|
||||
auto &l = implied_legs[leg_secs[i]];
|
||||
l.security_id = leg_secs[i];
|
||||
l.ratio.value() = std::abs(leg_qtys[i]);
|
||||
l.side = leg_qtys[i] > 0 ? Side::Buy : Side::Sell;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
for (const auto &secs : sec_sets) {
|
||||
// The implementation presupposes that implied markets consist of exactly
|
||||
// 2 legs, and that the quantities of said legs are +/- 1. This
|
||||
// simplifies implementation by eliminating the need for complicated
|
||||
// factorizations of arbitrary leg quantities, as there are only 4
|
||||
// possible permutations of quantities for the pair of legs:
|
||||
// {1, 1}, {1, -1}, {-1, -1}, {-1, 1}.
|
||||
bts_assert(secs.size() == 2);
|
||||
bts_assert(secs[0] != secs[1]);
|
||||
LegMap and_legs = _me->securities[secs[0]].legs;
|
||||
LegMap xor_legs = _me->securities[secs[0]].legs;
|
||||
merge_legs(and_legs, _me->securities[secs[1]].legs);
|
||||
merge_legs(xor_legs, negate(_me->securities[secs[1]].legs));
|
||||
(add_candidate_if_match(secs, {1, 1}, and_legs) ||
|
||||
add_candidate_if_match(secs, {-1, -1}, negate(and_legs)) ||
|
||||
add_candidate_if_match(secs, {1, -1}, xor_legs) ||
|
||||
add_candidate_if_match(secs, {-1, 1}, negate(xor_legs)));
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
void ImpliedMarket::_add_implied_combinations(
|
||||
const ImpliedContractData &contract, std::vector<LegData> &dest) const {
|
||||
//
|
||||
LegData impl_legs;
|
||||
ImpliedContractData c;
|
||||
_add_implied_combinations(
|
||||
contract, c, impl_legs, 0, 2, dest,
|
||||
get_candidate_filter(contract.legs().size() > 1));
|
||||
}
|
||||
|
||||
std::vector<LegData> ImpliedMarket::implied_mkts(SecurityID sec) const {
|
||||
auto cdata_it = _me->contracts.find(sec);
|
||||
std::vector<LegData> rv;
|
||||
bts_assert(cdata_it != _me->contracts.end());
|
||||
if (cdata_it != _me->contracts.end())
|
||||
_add_implied_combinations(cdata_it->second, rv);
|
||||
return rv;
|
||||
}
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "server/sparkd/cmds/spartan/domain/SecurityDefinition.h"
|
||||
#include "server/sparkd/cmds/spartan/market_data/common/mdp3_common.h"
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <iostream>
|
||||
@@ -7,69 +8,89 @@
|
||||
#include <vector>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
using Legs = boost::container::flat_map<SecurityID, Quantity>;
|
||||
|
||||
struct ImpliedContractData {
|
||||
ImpliedContractData();
|
||||
ImpliedContractData(SecurityID security_id);
|
||||
// Implementation Notes:
|
||||
// The current implied market implementation makes the following assumptions:
|
||||
//
|
||||
// * IMPLIED FUTURE MARKETS are 1 spread and 1 future
|
||||
// * the spread has exactly 2 legs, each with qty +/- 1
|
||||
// * one leg's security is the target future
|
||||
// * the other leg is canceled out by the future component in the
|
||||
// implied market
|
||||
// * there are typically many potential implied markets for a given future
|
||||
// (as many as there are spreads with a leg for the target future)
|
||||
//
|
||||
// * IMPLIED SPREAD MARKETS are the outrights that make up the spread
|
||||
// * unlike futures, this means there is only one potential implied market
|
||||
// for spreads
|
||||
//
|
||||
// The assumptions above naturally limit the securities for which implied
|
||||
// markets can be accurately modeled. Market channels with more complex
|
||||
// securities (e.g., butterfly spreads), and more sophisticated implied
|
||||
// matching behavior (e.g., inter-product spreads, ratios, etc.) will require
|
||||
// a more complex model. This could be implemented with a contract matrix,
|
||||
// whose rows represent future contracts, and columns represent exchange
|
||||
// listed securities. Given such a matrix 'A,' an implied candidate column
|
||||
// space basis for an exchange listed security (represented by column 'b' in
|
||||
// the contract matrix) would be the independent solution vectors 'x' for
|
||||
// 'Ax=b.'
|
||||
//
|
||||
// A simple example may better illustrate the concept of the 'contract
|
||||
// matrix.' Consider a contract matrix that represents a few ES futures and
|
||||
// calendar spreads:
|
||||
// __________________________________________________________
|
||||
// _______|_ESU1_|_ESZ1_|_ESG2_|_ESU1-ESZ1_|_ESU1-ESG2_|_ESZ1-ESG2_|
|
||||
// | ESU1 | 1 | 0 | 0 | 1 | 1 | 0 |
|
||||
// | ESZ1 | 0 | 1 | 0 | -1 | 0 | 1 |
|
||||
// |_ESG2_|__0___|__0___|__1___|_____0_____|____-1_____|____-1_____|
|
||||
//
|
||||
// Notice how the rows represent futures, and the columns represent exch
|
||||
// listed securities. There are no spread rows, only spread columns.
|
||||
//
|
||||
// If 'A' is the contract matrix, and 'b' is the column vector for 'ESU1,'
|
||||
// then the potential implied markets for 'ESU1' are the solutions 'x' to
|
||||
// the equation 'Ax=b'
|
||||
//
|
||||
// (A) (x) (b)
|
||||
// | 1 | 0 | 0 | 1 | 0 | * | ? | = | 1 | --> x = | 1 |, | 0 |, | 0 |
|
||||
// | 0 | 1 | 0 |-1 | 1 | | ? | | 0 | | 0 | | 1 | | 0 |
|
||||
// | 0 | 0 | 1 | 0 |-1 | | ? | | 0 | | 0 | | 0 | | 1 |
|
||||
// | 0 | | 1 | | 0 |
|
||||
// | 0 | | 0 | | 1 |
|
||||
// | 0 | | 0 | | 0 |
|
||||
|
||||
SecurityID security_id() const { return _security_id; }
|
||||
const Legs &legs() const { return _legs; }
|
||||
void add_leg(SecurityID sec, Quantity qty);
|
||||
void remove_leg(SecurityID sec, Quantity qty);
|
||||
void merge(
|
||||
const ImpliedContractData &other,
|
||||
Quantity multiplier = Quantity(1));
|
||||
Quantity leg_size(SecurityID sec) const {
|
||||
auto it = _legs.find(sec);
|
||||
return it == _legs.end() ? Quantity(0) : it->second;
|
||||
}
|
||||
void multiply_legs(Quantity multiplier);
|
||||
void negate() { multiply_legs(Quantity(-1)); }
|
||||
void print_legs(std::ostream &ostr = std::cout) const;
|
||||
// Since 'b' is itself an exchange listed security, one of these solutions
|
||||
// (the first one) is simply the security itself. The other 2 solutions are
|
||||
// implied markets for ESU1:
|
||||
//
|
||||
// 1) ESU1 = ESZ1 + ESU1-ESZ1
|
||||
// 2) ESU1 = ESG2 + ESU1-ESG2
|
||||
|
||||
private:
|
||||
SecurityID _security_id;
|
||||
Legs _legs;
|
||||
};
|
||||
/**
|
||||
* A model used to generate implied market candidates for securities
|
||||
* of a particular asset (cross product implied markets are not supported)
|
||||
*/
|
||||
class ImpliedMarketModel {
|
||||
public:
|
||||
ImpliedMarketModel();
|
||||
~ImpliedMarketModel();
|
||||
|
||||
using LegData = boost::container::
|
||||
flat_map<SecurityID, std::tuple<ImpliedContractData, Quantity>>;
|
||||
|
||||
struct ImpliedMarket {
|
||||
ImpliedMarket();
|
||||
~ImpliedMarket();
|
||||
|
||||
const std::map<SecurityID, ImpliedContractData> &contracts() const;
|
||||
// The returned ImpliedContractData& will not have any legs
|
||||
// (callers need to add them explicitly).
|
||||
ImpliedContractData &add_contract(SecurityID sec);
|
||||
void remove_contract(SecurityID sec);
|
||||
std::vector<LegData> implied_mkts(SecurityID sec) const;
|
||||
void insert(SecurityID, const LegMap &);
|
||||
void erase(SecurityID);
|
||||
void clear();
|
||||
/**
|
||||
* Get all possible implied markets for a particular security
|
||||
*
|
||||
* @param security The security for which to get implied markets
|
||||
* @returns The vector of implied markets
|
||||
*/
|
||||
std::vector<LegMap> find_implied_markets(SecurityID security);
|
||||
|
||||
const std::vector<LegMap> &find() const;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> _me;
|
||||
|
||||
void _add_implied_combinations(
|
||||
const ImpliedContractData &contract, std::vector<LegData> &dest) const;
|
||||
void _add_implied_combinations(
|
||||
const ImpliedContractData &contract,
|
||||
const ImpliedContractData &implied_contract,
|
||||
const LegData &impl_legs,
|
||||
uint32_t leg_index,
|
||||
uint32_t max_legs_remaining,
|
||||
std::vector<LegData> &dest,
|
||||
std::function<bool(const ImpliedContractData *)> candidate_filter) const;
|
||||
};
|
||||
|
||||
inline std::ostream &
|
||||
operator<<(std::ostream &out, const ImpliedContractData &i) {
|
||||
out << "Target Sec: " << i.security_id() << std::endl;
|
||||
out << "Legs:" << std::endl;
|
||||
for (auto it = i.legs().begin(); it != i.legs().end(); it++) {
|
||||
out << "\tid: " << it->first << ", qty: " << it->second << std::endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace bts::spartan::mdp3
|
||||
Reference in new issue
Block a user