refactor vector<tuple<price,qty>> to SmallBook (vector alias), and SmallBookLevel (actual struct instead of tuple)

This commit is contained in:
John Colvin committed 2020-12-01 12:44:25 -06:00
1 parent afb0cdfbaf
commit 724df36bdc
10 files changed
+99 -82

No files matched your search

+1 -1
View File
@@ -219,7 +219,7 @@ int polygon_books(const std::string& pcap, const std::string& db, const std::str
uint32_t i = 0;
bts_assert(bk.size() <= 10);
for (; i < bk.size(); i++)
out << "," << std::get<0>(bk[i]) << "," << std::get<1>(bk[i]);
out << "," << bk[i].price << "," << bk[i].quantity.value();
while (i++ < MDP3_MAX_BOOK_DEPTH)
out << ",NaN,0";
return out;
@@ -90,8 +90,8 @@ void FifoImpliedSecurityMatcher::print_legs(std::ostream& out) const {
append_bbo = false;
auto bid = top_of_book(api::Side::SIDE_BUY);
auto ask = top_of_book(api::Side::SIDE_SELL);
out << std::setw(8) << FMT("(", std::get<1>(bid), ") ") << std::setw(6) << std::get<0>(bid) << std::setw(6) << std::get<0>(ask) <<
FMT("(", std::get<1>(ask), ")");
out << std::setw(8) << FMT("(", bid.quantity, ") ") << std::setw(6) << bid.price << std::setw(6) << ask.price <<
FMT("(", ask.quantity, ")");
}
out << std::endl;
}
@@ -104,28 +104,28 @@ ImpliedBook FifoImpliedSecurityMatcher::book(api::Side side) const {
// this isn't the best way to do this (copying),
// but the ImpliedBook only has 2 levels in practice,
// so it should be fine.
std::vector<_BookLevel> tuple_ib;
SmallBook tuple_ib;
ImpliedBook ib;
_book(side, tuple_ib, ib.size());
for (uint32_t i = 0; i < tuple_ib.size() && i < ib.size(); i++) {
ib[i].price = std::get<0>(tuple_ib[i]);
ib[i].quantity = std::get<1>(tuple_ib[i]);
ib[i].price = tuple_ib[i].price;
ib[i].quantity = tuple_ib[i].quantity;
}
return ib;
}
_BookLevel FifoImpliedSecurityMatcher::top_of_book(api::Side side) const {
std::vector<_BookLevel> b;
SmallBookLevel FifoImpliedSecurityMatcher::top_of_book(api::Side side) const {
SmallBook b;
_book(side, b, 1);
if (b.empty())
return {md::IntPrice(), MDP3Quantity(0)};
return b[0];
}
void FifoImpliedSecurityMatcher::_book(api::Side side, std::vector<_BookLevel>& dest, uint32_t depth) const {
std::vector<std::tuple<MDP3Quantity, md::IntPrice, std::vector<_BookLevel>>> im_vec;
void FifoImpliedSecurityMatcher::_book(api::Side side, SmallBook& dest, uint32_t depth) const {
std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook>> im_vec;
for (const auto& p : _me->implied_mkt) {
std::vector<_BookLevel> bk;
SmallBook bk;
auto [sec_matcher, leg_qty] = p.second;
bts_assert(leg_qty.value() != 0);
auto leg_side = leg_qty.value() > 0 ? securities::other_side(side) : side;
@@ -142,13 +142,13 @@ void FifoImpliedSecurityMatcher::add_leg(MDP3SecurityID sec_id, FifoSecurityMatc
}
MDP3Quantity FifoImpliedSecurityMatcher::fill(MDP3OrderKey key, api::Side side, MDP3Quantity qty, std::vector<MatchEffect>* effects, FillDepot& fills) {
std::vector<std::tuple<md::IntPrice, MDP3Quantity>> b;
SmallBook b;
auto oside = securities::other_side(side);
_book(oside, b, MDP3_MAX_IMPLIED_BOOK_DEPTH);
auto orig_qty = qty;
// can't fill more than the volume in the book
qty = std::min(book_volume(b), qty);
md::IntPrice impl_fill_price = std::get<0>(top_of_book(oside));
md::IntPrice impl_fill_price = top_of_book(oside).price;
bool has_added_aggr_fills = false;
int fill_qty = 0;
bool local_fills = effects == nullptr;
@@ -32,7 +32,7 @@ struct FifoImpliedSecurityMatcher {
void add_leg(MDP3SecurityID sec_id, FifoSecurityMatcher* m, MDP3Quantity unit_qty, std::string leg_sym);
MDP3SecurityID security_id() const;
std::string symbol() const;
std::tuple<md::IntPrice, MDP3Quantity> top_of_book(api::Side side) const;
SmallBookLevel top_of_book(api::Side side) const;
void print_legs(std::ostream& out = std::cout) const;
private:
@@ -42,7 +42,7 @@ struct FifoImpliedSecurityMatcher {
FifoImpliedSecurityMatcher(const FifoImpliedSecurityMatcher&);
std::string _log_header() const;
void _book(api::Side side, std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& dest, uint32_t depth) const;
void _book(api::Side side, SmallBook& dest, uint32_t depth) const;
uint32_t _fill_order(const MDP3Order& order);
};
} // namespace bts::spartan::mdp3
@@ -89,12 +89,11 @@ FifoMergedSecurityMatcher::_best_implied_matcher(api::Side side) {
bts::orderman::SidePriceComp cmp(side);
FifoImpliedSecurityMatcher* rv = nullptr;
md::IntPrice best_price;
MDP3Quantity best_qty(0);
for (auto& im : _me->implied_matchers) {
auto [price, qty] = im->top_of_book(side);
if (qty.value() > 0 && cmp(price, best_price)) {
auto itop = im->top_of_book(side);
if (itop.quantity.value() > 0 && cmp(itop.price, best_price)) {
rv = im;
best_price = price;
best_price = itop.price;
}
}
return rv;
@@ -280,6 +279,10 @@ void FifoMergedSecurityMatcher::_fill(MDP3Order& order, std::vector<MatchEffect>
set_total_qty(order, rem, reset_show_qty);
}
inline SmallBookLevel tuple_to_small_book_level(std::tuple<md::IntPrice, MDP3Quantity> t) {
return SmallBookLevel(std::get<0>(t), std::get<1>(t));
}
MDP3Quantity FifoMergedSecurityMatcher::_fill(
MDP3OrderKey key,
api::Side side,
@@ -296,19 +299,19 @@ MDP3Quantity FifoMergedSecurityMatcher::_fill(
// another may have the best 2nd price level.
auto* imatcher = _best_implied_matcher(oside);
auto itop = imatcher == nullptr ?
std::make_tuple(bts::md::IntPrice(), MDP3Quantity(0)) :
SmallBookLevel() :
imatcher->top_of_book(oside);
auto dtop = matcher()->book_level(oside, 0);
bool is_dcross = !get_price(dtop).is_nan() && !oside_comp(price, get_price(dtop));
bool is_icross = !get_price(itop).is_nan() && !oside_comp(price, get_price(itop));
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(get_price(itop), get_price(dtop)))) {
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, get_qty(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;
@@ -320,8 +323,8 @@ MDP3Quantity FifoMergedSecurityMatcher::_fill(
if (local_effects)
effects = new std::vector<MatchEffect>();
uint32_t fills_pos = effects->size();
auto target_fill_qty = std::min(quantity, get_qty(dtop));
auto rem = matcher()->fill(key, side, get_price(dtop), target_fill_qty, effects, true);
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());
@@ -92,6 +92,26 @@ inline std::ostream &operator<<(std::ostream &s, BookLevel const &l) {
return s;
}
struct SmallBookLevel {
SmallBookLevel() : price(md::INT_NAN), quantity(0) {}
SmallBookLevel(md::IntPrice price, MDP3Quantity quantity)
: price(price), quantity(quantity) {}
md::IntPrice price;
MDP3Quantity quantity;
};
inline bool operator==(const SmallBookLevel& lhs, const SmallBookLevel& rhs) {
return lhs.price == rhs.price && lhs.quantity.value() == rhs.quantity.value();
}
inline bool operator!=(const SmallBookLevel& lhs, const SmallBookLevel& rhs) {
return lhs.price != rhs.price || lhs.quantity.value() != rhs.quantity.value();
}
inline std::ostream& operator<<(std::ostream& out, const SmallBookLevel& sbl) {
out << sbl.quantity.value() << " @ " << sbl.price;
return out;
}
struct IcebergQuantity {
MDP3Quantity show;
MDP3Quantity total;
@@ -285,6 +305,7 @@ inline std::ostream &operator<<(std::ostream &s, Fills const &fills) {
const auto MDP3_MAX_BOOK_DEPTH = 10;
const auto MDP3_MAX_IMPLIED_BOOK_DEPTH = 2;
using Book = std::array<BookLevel, MDP3_MAX_BOOK_DEPTH>;
using SmallBook = std::vector<SmallBookLevel>;
inline std::ostream &operator<<(std::ostream &s, Book const &b) {
for (auto const &l : b)
@@ -2,15 +2,13 @@
#include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_quantity_operators.h"
namespace bts::spartan::mdp3::book_utils {
using _BookLevel = std::tuple<md::IntPrice, MDP3Quantity>;
inline bool is_empty(const _BookLevel& bl) {
return std::get<0>(bl).is_nan() || std::get<1>(bl).value() == 0;
inline bool is_empty(const SmallBookLevel& bl) {
return bl.price.is_nan() || bl.quantity.value() == 0;
}
void compute_synthetic_book(
std::vector<_BookLevel>& dest,
const std::vector<std::tuple<MDP3Quantity, md::IntPrice, std::vector<_BookLevel>>>&
SmallBook& dest,
const std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook>>&
leg_mkts,
uint32_t depth) {
//
@@ -18,7 +16,7 @@ void compute_synthetic_book(
return;
struct Leg {
std::vector<std::tuple<md::IntPrice, MDP3Quantity>> book;
SmallBook book;
uint32_t curr_level;
MDP3Quantity curr_level_qty_used, leg_qty;
@@ -33,10 +31,10 @@ void compute_synthetic_book(
// as a result of deliberate action, rather than an inadvertant
// side-effect.
bts_assert(!is_exhausted());
return std::get<0>(book[curr_level]);
return book[curr_level].price;
}
MDP3Quantity curr_level_qty_rem() const {
return std::get<1>(book[curr_level]) - curr_level_qty_used;
return book[curr_level].quantity - curr_level_qty_used;
}
// The implied market's volume is limited by its thinnest market.
// When one leg 'is_exhausted,' the full volume of the implied market
@@ -100,21 +98,18 @@ void compute_synthetic_book(
}
void compute_synthetic_books(
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& bids_dest,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& asks_dest,
const std::vector<std::tuple<
MDP3Quantity,
md::IntPrice,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>>>& leg_mkts,
SmallBook& bids_dest,
SmallBook& asks_dest,
const std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook, SmallBook>>&
leg_mkts,
uint32_t depth) {
std::vector<std::tuple<MDP3Quantity, md::IntPrice, std::vector<std::tuple<md::IntPrice, MDP3Quantity>>>> leg_bids;
std::vector<std::tuple<MDP3Quantity, md::IntPrice, std::vector<std::tuple<md::IntPrice, MDP3Quantity>>>> leg_asks;
for (auto [qty, price_mult, bids, asks] : leg_mkts) {
leg_bids.emplace_back(qty, price_mult, qty.value() > 0 ? bids : asks);
leg_asks.emplace_back(qty, price_mult, qty.value() < 0 ? bids : asks);
}
compute_synthetic_book(bids_dest, leg_bids, depth);
compute_synthetic_book(asks_dest, leg_asks, depth);
std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook>> leg_bids;
std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook>> leg_asks;
for (auto [qty, price_mult, bids, asks] : leg_mkts) {
leg_bids.emplace_back(qty, price_mult, qty.value() > 0 ? bids : asks);
leg_asks.emplace_back(qty, price_mult, qty.value() < 0 ? bids : asks);
}
compute_synthetic_book(bids_dest, leg_bids, depth);
compute_synthetic_book(asks_dest, leg_asks, depth);
}
} // namespace bts::spartan::mdp3::book_utils
@@ -36,6 +36,12 @@ inline std::string fmt_price_level(api::Side side, md::IntPrice price, MDP3Quant
return out.str();
}
inline std::string fmt_price_level(api::Side side, SmallBookLevel l) {
std::stringstream out;
fmt_price_level(out, side, l.price, l.quantity);
return out.str();
}
inline std::string fmt_price_level(api::Side side, const std::tuple<md::IntPrice, MDP3Quantity>& pl) {
return fmt_price_level(side, std::get<0>(pl), std::get<1>(pl));
}
@@ -84,16 +90,13 @@ inline std::ostream& fmt_ladder(
// TIter should point to BookLevel items.
template <class TIter>
inline void change_unit_qty(
TIter book_in_first,
TIter book_in_last,
std::vector<std::tuple<bts::md::IntPrice, MDP3Quantity>>& book_out,
MDP3Quantity unit_qty) {
TIter book_in_first, TIter book_in_last, SmallBook& book_out, MDP3Quantity unit_qty) {
//
bts_assert(unit_qty.value() > 0);
int mlev = 0, mlev_qty_used = 0;
std::vector<std::tuple<bts::md::IntPrice, MDP3Quantity>> unit_lev_span;
SmallBook unit_lev_span;
while (book_in_first != book_in_last) {
MDP3Quantity mlev_qty_rem(std::get<1>(*book_in_first).value() - mlev_qty_used);
MDP3Quantity mlev_qty_rem(book_in_first->quantity.value() - mlev_qty_used);
if (mlev_qty_rem.value() == 0) {
mlev_qty_used = 0;
mlev++;
@@ -103,10 +106,11 @@ inline void change_unit_qty(
if (unit_lev_span.size() == 0) {
int mlev_units_rem = mlev_qty_rem.value() / unit_qty.value();
if (mlev_units_rem > 0) {
book_out.push_back({std::get<0>(*book_in_first), MDP3Quantity(mlev_units_rem)});
book_out.emplace_back(
book_in_first->price, MDP3Quantity(mlev_units_rem));
mlev_qty_used += (mlev_units_rem * unit_qty.value());
} else {
unit_lev_span.push_back({std::get<0>(*book_in_first), mlev_qty_rem});
unit_lev_span.emplace_back(book_in_first->price, mlev_qty_rem);
mlev_qty_used = 0;
mlev++;
book_in_first++;
@@ -116,38 +120,34 @@ inline void change_unit_qty(
// It will be in its own [output]price level.
MDP3Quantity unit_rem(unit_qty);
for (auto lev : unit_lev_span)
unit_rem = MDP3Quantity(unit_rem.value() - std::get<1>(lev).value());
unit_rem = MDP3Quantity(unit_rem.value() - lev.quantity.value());
if (mlev_qty_rem.value() >= unit_rem.value()) {
mlev_qty_used += unit_rem.value();
auto unit_price = md::INT_NAN;
for (uint32_t adj_lev = 0; adj_lev < unit_lev_span.size(); adj_lev++) {
auto leg_price = std::get<0>(unit_lev_span[adj_lev]) * std::get<1>(unit_lev_span[adj_lev]).value();
auto leg_price =
unit_lev_span[adj_lev].price * unit_lev_span[adj_lev].quantity.value();
if (unit_price.is_nan())
unit_price = leg_price;
else unit_price += leg_price;
else
unit_price += leg_price;
}
unit_lev_span.clear();
book_out.push_back({unit_price, MDP3Quantity(1)});
book_out.emplace_back(unit_price, MDP3Quantity(1));
}
}
}
}
void compute_synthetic_book(
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& dest,
const std::vector<std::tuple<
MDP3Quantity,
md::IntPrice,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>>>& leg_mkts,
SmallBook& dest,
const std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook>>& leg_mkts,
uint32_t depth);
void compute_synthetic_books(
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& bids_dest,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& asks_dest,
const std::vector<std::tuple<
MDP3Quantity,
md::IntPrice,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>,
std::vector<std::tuple<md::IntPrice, MDP3Quantity>>>>& leg_mkts,
SmallBook& bids_dest,
SmallBook& asks_dest,
const std::vector<std::tuple<MDP3Quantity, md::IntPrice, SmallBook, SmallBook>>&
leg_mkts,
uint32_t depth);
} // namespace bts::spartan::mdp3::book_utils
@@ -51,8 +51,8 @@ inline void set_total_qty(MDP3Order& order, MDP3Quantity total_qty, bool reset_s
order.quantity = total_qty;
}
inline MDP3Quantity book_volume(const std::vector<std::tuple<md::IntPrice, MDP3Quantity>>& book) {
auto get_qty = [](const auto& bl) { return std::get<1>(bl).value(); };
inline MDP3Quantity book_volume(const SmallBook& book) {
auto get_qty = [](const auto& bl) { return bl.quantity.value(); };
return MDP3Quantity(ranges::accumulate(book | ranges::views::transform(get_qty), 0));
}
@@ -21,7 +21,7 @@ using BookMap =
using LegBookVec = std::vector<
std::tuple<size_t, MDP3SecurityID, MDP3Quantity, md::IntPrice, SmallBook, SmallBook>>;
SmallBook to_small_book_vec(const Book& bk) {
SmallBook to_small_book(const Book& bk) {
SmallBook rv;
for (const auto& pl : bk)
rv.emplace_back(pl.price, pl.quantity);
@@ -59,8 +59,8 @@ void update_books(
const InstrumentSnapshot* s = *first;
books.insert_or_assign(
s->security_id(), std::make_tuple(
event_id, to_small_book_vec(s->book(api::Side::SIDE_BUY)),
to_small_book_vec(s->book(api::Side::SIDE_SELL))));
event_id, to_small_book(s->book(api::Side::SIDE_BUY)),
to_small_book(s->book(api::Side::SIDE_SELL))));
first++;
}
}
@@ -11,8 +11,6 @@
#include <unordered_map>
namespace bts::spartan::mdp3 {
using SmallBook = std::vector<std::tuple<md::IntPrice, MDP3Quantity>>;
struct SyntheticMBPUpdate {
SyntheticMBPUpdate();
SyntheticMBPUpdate(