Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas
This commit is contained in:
commit
d69aee25f9
9 files changed
+89
-56
No files matched your search
@@ -121,14 +121,12 @@ void FifoMatcherContext::implied_matching_status(MDP3SecurityID sec_id, bool val
|
||||
///////////////////////
|
||||
// book
|
||||
///////////////////////
|
||||
Book FifoMatcherContext::book(MDP3SecurityID sec_id, api::Side side) const {
|
||||
const auto* m = _get_matcheri(sec_id);
|
||||
return m->book(side);
|
||||
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) const {
|
||||
const auto* m = _get_matcheri(sec_id);
|
||||
return m->implied_book(side);
|
||||
ImpliedBook FifoMatcherContext::implied_book(MDP3SecurityID sec_id, api::Side side) {
|
||||
return _get_matcher(sec_id)->implied_book(side);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
|
||||
@@ -30,8 +30,8 @@ struct FifoMatcherContext {
|
||||
void implied_matching_status(MDP3SecurityID sec_id, bool value);
|
||||
bool implied_matching_status(MDP3SecurityID sec_id) const;
|
||||
void clear();
|
||||
Book book(MDP3SecurityID sec_id, api::Side side) const;
|
||||
ImpliedBook implied_book(MDP3SecurityID sec_id, api::Side side) const;
|
||||
Book book(MDP3SecurityID sec_id, api::Side side);
|
||||
ImpliedBook implied_book(MDP3SecurityID sec_id, api::Side side);
|
||||
|
||||
// the Fills param records individual fills (potentially more than one per order)
|
||||
void on_order_change(MDP3SecurityID sec_id, const std::vector<SmallOrderChange>& changes, std::vector<MatchEffect>* effects, FillDepot& fills);
|
||||
|
||||
+5
-5
@@ -150,10 +150,10 @@ void FifoMergedSecurityMatcher::on_order_change(const SmallOrderChange& ch, std:
|
||||
/////////////////////////
|
||||
// books
|
||||
/////////////////////////
|
||||
Book FifoMergedSecurityMatcher::book(api::Side side) const {
|
||||
return matcheri()->book(side);
|
||||
Book FifoMergedSecurityMatcher::book(api::Side side) {
|
||||
return matcher()->book(side);
|
||||
}
|
||||
ImpliedBook FifoMergedSecurityMatcher::implied_book(api::Side side) const {
|
||||
ImpliedBook FifoMergedSecurityMatcher::implied_book(api::Side side) {
|
||||
if (_me->implied_matchers.empty())
|
||||
return ImpliedBook();
|
||||
// In theory, computing the quantity of any price level beyond the top of the book
|
||||
@@ -208,7 +208,7 @@ ImpliedBook FifoMergedSecurityMatcher::implied_book(api::Side side) const {
|
||||
}
|
||||
|
||||
void FifoMergedSecurityMatcher::merged_book(
|
||||
api::Side side, std::vector<BookLevel>& dest) const {
|
||||
api::Side side, std::vector<BookLevel>& dest) {
|
||||
//
|
||||
std::vector<std::tuple<ImpliedBook, int>> ibooks;
|
||||
auto oside = bts::securities::other_side(side);
|
||||
@@ -219,7 +219,7 @@ void FifoMergedSecurityMatcher::merged_book(
|
||||
if (ib[0].price.is_nan())
|
||||
ibooks.pop_back();
|
||||
}
|
||||
auto dbook = matcheri()->book(side);
|
||||
auto dbook = matcher()->book(side);
|
||||
int dindex = 0;
|
||||
auto is_exhausted = [&](const std::tuple<ImpliedBook, int>& ib_tup) {
|
||||
const auto& ib = std::get<0>(ib_tup);
|
||||
|
||||
+3
-3
@@ -28,9 +28,9 @@ struct FifoMergedSecurityMatcher {
|
||||
const FifoSecurityMatcher* matcheri() const;
|
||||
const std::vector<FifoImpliedSecurityMatcher*>& implied_matchers();
|
||||
// books
|
||||
Book book(api::Side) const;
|
||||
ImpliedBook implied_book(api::Side) const;
|
||||
void merged_book(api::Side, std::vector<BookLevel>&) const;
|
||||
Book book(api::Side);
|
||||
ImpliedBook implied_book(api::Side);
|
||||
void merged_book(api::Side, std::vector<BookLevel>&);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
+38
-25
@@ -60,16 +60,16 @@ FifoPriceLevelMatcher* FifoSecurityMatcher::_top(api::Side side){
|
||||
// this is expensive
|
||||
void FifoSecurityMatcher::_cleanup() {
|
||||
// remove orders from the price levels
|
||||
for (auto& p : _bid_book)
|
||||
for (auto& p : _bids.book())
|
||||
p.cleanup();
|
||||
for (auto& p : _ask_book)
|
||||
for (auto& p : _asks.book())
|
||||
p.cleanup();
|
||||
// remove empty levels
|
||||
auto rem_if = [](const auto &p_lev) { return !p_lev.populated(); };
|
||||
_bid_book.erase(std::remove_if(_bid_book.begin(), _bid_book.end(), rem_if),
|
||||
_bid_book.end());
|
||||
_ask_book.erase(std::remove_if(_ask_book.begin(), _ask_book.end(), rem_if),
|
||||
_ask_book.end());
|
||||
_bids.book().erase(std::remove_if(_bids.book().begin(), _bids.book().end(), rem_if),
|
||||
_bids.book().end());
|
||||
_asks.book().erase(std::remove_if(_asks.book().begin(), _asks.book().end(), rem_if),
|
||||
_asks.book().end());
|
||||
// remove inactive orders from map
|
||||
std::experimental::erase_if(_order_map, [](const auto& p) {
|
||||
auto& ord = std::get<1>(p);
|
||||
@@ -193,8 +193,8 @@ FifoSecurityMatcher::FifoSecurityMatcher(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs args)
|
||||
:
|
||||
_order_map(),
|
||||
_bid_book(),
|
||||
_ask_book(),
|
||||
_bids(),
|
||||
_asks(),
|
||||
_search_tok(_order_map, MDP3SecurityID{},
|
||||
md::IntPrice(), api::Side::SIDE_BUY, Trace()),
|
||||
_sec(args.security_id),
|
||||
@@ -228,18 +228,29 @@ FifoSecurityMatcher::~FifoSecurityMatcher(){
|
||||
}
|
||||
// SecurityMatcher implementation /////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
Book FifoSecurityMatcher::book(api::Side side) const {
|
||||
auto i = 0;
|
||||
Book rv;
|
||||
for (const auto &p_level : _book(side)) {
|
||||
if(p_level.populated()){
|
||||
if (i < MDP3_MAX_BOOK_DEPTH)
|
||||
rv[i++] = p_level.book_level();
|
||||
else
|
||||
break;
|
||||
const Book& FifoSecurityMatcher::BookKeeper::mbp() {
|
||||
if (_is_dirty) {
|
||||
int i = 0;
|
||||
for (const auto& p_level : _book) {
|
||||
if (p_level.populated()) {
|
||||
if (i < MDP3_MAX_BOOK_DEPTH)
|
||||
_mbp[i++] = p_level.book_level();
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Explicitly set the remaining entries to nan/0
|
||||
// (they could have stale values since the book is recycled).
|
||||
while (i < MDP3_MAX_BOOK_DEPTH) {
|
||||
_mbp[i].price = md::IntPrice();
|
||||
_mbp[i++].quantity.value() = 0;
|
||||
}
|
||||
_is_dirty = false;
|
||||
}
|
||||
return rv;
|
||||
return _mbp;
|
||||
}
|
||||
Book FifoSecurityMatcher::book(api::Side side) {
|
||||
return (side == api::Side::SIDE_BUY ? _bids : _asks).mbp();
|
||||
}
|
||||
size_t FifoSecurityMatcher::total_n_orders(api::Side side) const{
|
||||
auto rv=0;
|
||||
@@ -274,6 +285,7 @@ void FifoSecurityMatcher::on_order_change(SmallOrderChange const &o,
|
||||
_on_order_remove(o.order.key, res);
|
||||
break;
|
||||
}
|
||||
_invalidate_mbp();
|
||||
if(_running_op_count++ > _clean_up_op_threshold){
|
||||
_running_op_count = 0;
|
||||
_cleanup();
|
||||
@@ -303,6 +315,7 @@ MDP3Quantity FifoSecurityMatcher::fill(MDP3OrderKey aggr_key,
|
||||
if(top != nullptr){
|
||||
bts_assert(top->price()==agg_price || agg_price.is_nan());
|
||||
auto t = top->on_new_opp_order(aggr_key,qty,add_aggr_fill,res);
|
||||
_invalidate_mbp();
|
||||
return std::get<1>(t);
|
||||
}
|
||||
return MDP3Quantity(0);
|
||||
@@ -361,14 +374,14 @@ void FifoSecurityMatcher::reset(const std::vector<MDP3Order>& orders) {
|
||||
orders.empty(),
|
||||
"this method hasn't been fully implemented yet, and currently only supports "
|
||||
"'clear' functionality (it asserts the 'orders' param is empty).");
|
||||
_bid_book.clear();
|
||||
_ask_book.clear();
|
||||
_bids.book().clear();
|
||||
_asks.book().clear();
|
||||
_order_map.clear();
|
||||
_running_op_count = 0;
|
||||
}
|
||||
// logging and printing ///////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::string FifoSecurityMatcher::print_book(size_t n_levels) const {
|
||||
std::string FifoSecurityMatcher::print_book(size_t n_levels) {
|
||||
// auto rem_if = [](const auto& p_lev) { return !p_lev.populated(); };
|
||||
std::ostringstream buff;
|
||||
auto add_to_buff = [&](const auto &pl) {
|
||||
@@ -378,14 +391,14 @@ std::string FifoSecurityMatcher::print_book(size_t n_levels) const {
|
||||
};
|
||||
// asks are stored lowest to highest
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
for (const auto &pl : _asks.book()){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
}
|
||||
// bids are scored highest to lowest
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
for (const auto &pl : _bids.book()){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
@@ -417,13 +430,13 @@ void FifoSecurityMatcher::_log_mbo(size_t n_levels, size_t n_orders){
|
||||
std::ostringstream buff;
|
||||
buff << "eid:" << _event_id << std::endl;
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
for (const auto &pl : _asks.book()){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
}
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
for (const auto &pl : _bids.book()){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <boost/container/flat_set.hpp>
|
||||
#include "../simulator/AlgoEvent.h"
|
||||
#include "SecurityMatcher.h"
|
||||
#include <optional>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
@@ -19,6 +20,18 @@ public:
|
||||
Orders const* orders = nullptr;
|
||||
};
|
||||
private:
|
||||
struct BookKeeper {
|
||||
boost::container::flat_set<FifoPriceLevelMatcher>& book() { return _book; }
|
||||
const boost::container::flat_set<FifoPriceLevelMatcher>& booki() const { return _book; }
|
||||
const Book& mbp();
|
||||
void invalidate() { _is_dirty = true; }
|
||||
|
||||
private:
|
||||
api::Side _side;
|
||||
Book _mbp;
|
||||
bool _is_dirty = true;
|
||||
boost::container::flat_set<FifoPriceLevelMatcher> _book;
|
||||
};
|
||||
// for orders we need a structure to provide fast access
|
||||
// and where adding and removing elements does not invalidate
|
||||
// iterators (or cached pointers)
|
||||
@@ -28,8 +41,8 @@ public:
|
||||
// have a key collision, would it be rehashed?)
|
||||
std::map<MDP3OrderKey,MatcherOrder> _order_map;
|
||||
// ask prices are sorted lowest to highest and and bid prices
|
||||
// are sorted highest to lowest
|
||||
boost::container::flat_set<FifoPriceLevelMatcher> _bid_book, _ask_book;
|
||||
// are sorted highest to lowest (see BookKeeper::_book).
|
||||
BookKeeper _bids, _asks;
|
||||
FifoPriceLevelMatcher _search_tok;
|
||||
const MDP3SecurityID _sec;
|
||||
const size_t _event_id;
|
||||
@@ -44,11 +57,15 @@ public:
|
||||
// complexity: O(lg(n))
|
||||
FifoPriceLevelMatcher *_find_price_level(api::Side, md::IntPrice price);
|
||||
boost::container::flat_set<FifoPriceLevelMatcher> &_book(api::Side side) {
|
||||
return side == api::Side::SIDE_BUY ? _bid_book : _ask_book;
|
||||
return (side == api::Side::SIDE_BUY ? _bids : _asks).book();
|
||||
}
|
||||
const boost::container::flat_set<FifoPriceLevelMatcher> &
|
||||
_book(api::Side side) const {
|
||||
return side == api::Side::SIDE_BUY ? _bid_book : _ask_book;
|
||||
return (side == api::Side::SIDE_BUY ? _bids : _asks).booki();
|
||||
}
|
||||
void _invalidate_mbp() {
|
||||
_bids.invalidate();
|
||||
_asks.invalidate();
|
||||
}
|
||||
// the complexity is O(lgN+lgM == lg(M*N)), since first it first searchess
|
||||
// for the order, from the order it gets the price, side and searches
|
||||
@@ -74,7 +91,7 @@ public:
|
||||
~FifoSecurityMatcher();
|
||||
// SecurityMatcher implementation //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
Book book(api::Side) const override;
|
||||
Book book(api::Side) override;
|
||||
// The qty, and add_aggr_fill params address a couple of issues wrt 'merged matching:'
|
||||
// Particular quantities of passive fills need to be generated when
|
||||
// an aggressor order is filled against an implied market, while the aggressor
|
||||
@@ -106,7 +123,7 @@ public:
|
||||
// debugging logging //////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
public:
|
||||
std::string print_book(size_t n_levels=3) const;
|
||||
std::string print_book(size_t n_levels=3);
|
||||
private:
|
||||
void _log_trace_order_state(const std::string& msg);
|
||||
void _log_trace_order(const MDP3Order& ord, const std::string& msg);
|
||||
|
||||
@@ -55,7 +55,7 @@ inline bool mbp_equals(const MBP<L>& a, const MBP<L>& b) {
|
||||
// accidentally mixing up before/after, or bids/asks.
|
||||
struct MBPData {
|
||||
MBPData() {}
|
||||
MBPData(const FifoMergedSecurityMatcher& mat)
|
||||
MBPData(FifoMergedSecurityMatcher& mat)
|
||||
: bids(mat.book(api::Side::SIDE_BUY)), asks(mat.book(api::Side::SIDE_SELL)),
|
||||
ibids(mat.implied_book(api::Side::SIDE_BUY)), iasks(mat.implied_book(api::Side::SIDE_SELL)) {}
|
||||
Book bids, asks;
|
||||
@@ -198,7 +198,7 @@ void MergedMatcher::match(ChangeSrc,
|
||||
}
|
||||
}
|
||||
|
||||
Book MergedMatcher::book(MDP3SecurityID id, api::Side s) const {
|
||||
Book MergedMatcher::book(MDP3SecurityID id, api::Side s) {
|
||||
auto ass_it = _asset_map.find(id);
|
||||
bts_assert(ass_it != _asset_map.end());
|
||||
auto it = _matcher_map.find(ass_it->second);
|
||||
@@ -206,7 +206,7 @@ Book MergedMatcher::book(MDP3SecurityID id, api::Side s) const {
|
||||
return it->second.matcher.book(id, s);
|
||||
}
|
||||
|
||||
ImpliedBook MergedMatcher::implied_book(MDP3SecurityID id, api::Side s) const {
|
||||
ImpliedBook MergedMatcher::implied_book(MDP3SecurityID id, api::Side s) {
|
||||
auto ass_it = _asset_map.find(id);
|
||||
bts_assert(ass_it != _asset_map.end());
|
||||
auto it = _matcher_map.find(ass_it->second);
|
||||
@@ -309,7 +309,7 @@ void DirectMatcher::match(ChangeSrc,
|
||||
}
|
||||
}
|
||||
|
||||
Book DirectMatcher::book(MDP3SecurityID id, api::Side s) const {
|
||||
Book DirectMatcher::book(MDP3SecurityID id, api::Side s) {
|
||||
auto it = _matcher_map.find(id);
|
||||
bts_assert(it != _matcher_map.end());
|
||||
return it->second.matcher.book(s);
|
||||
|
||||
@@ -48,8 +48,8 @@ public:
|
||||
SmallOrderChangeVec const&,
|
||||
std::vector<MatchEffect> *);
|
||||
|
||||
Book book(MDP3SecurityID, api::Side) const;
|
||||
ImpliedBook implied_book(MDP3SecurityID, api::Side) const;
|
||||
Book book(MDP3SecurityID, api::Side);
|
||||
ImpliedBook implied_book(MDP3SecurityID, api::Side);
|
||||
|
||||
template<class TIter>
|
||||
void init(TIter first, TIter last) {
|
||||
@@ -92,8 +92,8 @@ public:
|
||||
SmallOrderChangeVec const&,
|
||||
std::vector<MatchEffect> *);
|
||||
|
||||
Book book(MDP3SecurityID, api::Side) const;
|
||||
ImpliedBook implied_book(MDP3SecurityID, api::Side) const;
|
||||
Book book(MDP3SecurityID, api::Side);
|
||||
ImpliedBook implied_book(MDP3SecurityID, api::Side);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -8,7 +8,12 @@ class SecurityMatcher {
|
||||
public:
|
||||
virtual ~SecurityMatcher() {}
|
||||
// accessors
|
||||
virtual Book book(api::Side) const = 0;
|
||||
// unf the book() method cannot be const right now.
|
||||
// The FifoSecurityMatcher recomputes books lazily
|
||||
// for performance reasons. Its internal state will change
|
||||
// when this method is called if its book is dirty,
|
||||
// and has to be recomputed.
|
||||
virtual Book book(api::Side) = 0;
|
||||
virtual std::tuple<md::IntPrice, MDP3Quantity> book_level(
|
||||
api::Side, size_t level = 0) const = 0;
|
||||
// mutators
|
||||
|
||||
Reference in new issue
Block a user