speed up
This commit is contained in:
1 parent
724eea5403
commit
fc8620939f
5 files changed
+535
-296
No files matched your search
+146
-79
@@ -6,21 +6,23 @@ namespace bts::spartan::mdp3 {
|
||||
// private helpers ////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// orders can cease to be part of this level if there is
|
||||
// a null slot (ex when the order is physcially moved to another
|
||||
// price level or at the end of the queue) or the order is cxl'ed
|
||||
// with the is_active marker
|
||||
bool in_this_level(FifoPriceLevelMatcher::Order const* o_ptr){
|
||||
return o_ptr!=nullptr && o_ptr->is_active;
|
||||
}
|
||||
|
||||
api::Side _opp_side(api::Side side){
|
||||
return side == api::Side::SIDE_BUY ? api::Side::SIDE_SELL :
|
||||
api::Side::SIDE_BUY;
|
||||
}
|
||||
|
||||
void FifoPriceLevelMatcher::_compress() {
|
||||
_orders.erase(std::remove_if(_orders.begin(), _orders.end(),
|
||||
[](const Order &o) { return !o.is_active; }),
|
||||
_orders.end());
|
||||
}
|
||||
|
||||
// O(n) linear search
|
||||
FifoPriceLevelMatcher::Order *FifoPriceLevelMatcher::_find(MDP3OrderKey key) {
|
||||
for (auto &o : _orders)
|
||||
if (o.key == key)
|
||||
return &o;
|
||||
for (Order* op : _orders)
|
||||
if (in_this_level(op) && op->key == key)
|
||||
return op;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -28,33 +30,38 @@ void FifoPriceLevelMatcher::_logically_delete(Order &o) {
|
||||
_log_order(o,"FifoPriceLevelMatcher::_logically_delete");
|
||||
o.is_active = false;
|
||||
}
|
||||
void FifoPriceLevelMatcher::_add_order(const MDP3Order &ord) {
|
||||
_orders.emplace_back(
|
||||
ord.key, ord.quantity, _priority++,
|
||||
FifoPriceLevelMatcher::IB{ord.iceberg_quantity.has_value(), ord.quantity,
|
||||
void FifoPriceLevelMatcher::_add_order(const MDP3Order &ord) {
|
||||
auto ib =
|
||||
FifoPriceLevelMatcher::IB{ord.iceberg_quantity.has_value(), ord.quantity,
|
||||
ord.iceberg_quantity.has_value()
|
||||
? ord.iceberg_quantity.value()
|
||||
: ord.quantity});
|
||||
//_log_order(ord,"FifoPriceLevelMatcher::_add_order");
|
||||
: ord.quantity};
|
||||
Order new_ord (ord.key,ord.side,ord.price,ord.quantity,
|
||||
_priority++,ib);
|
||||
auto[p,res] = _master_order_map->try_emplace(new_ord.key,new_ord);
|
||||
Order* new_ord_ptr = &p->second;
|
||||
_orders.push_back(new_ord_ptr);
|
||||
if(!res)
|
||||
// it exists, so this order is moved from another price level
|
||||
// to this price level
|
||||
*new_ord_ptr = new_ord;
|
||||
}
|
||||
// O(n) iteration through all orders to detect loadable ib's
|
||||
void FifoPriceLevelMatcher::_reload_icebergs() {
|
||||
_aux_usage.clear();
|
||||
auto mv_ibs = [&](const Order &o) {
|
||||
if (o.ready_to_load_ib()) {
|
||||
_aux_usage.push_back(o);
|
||||
return true;
|
||||
for (auto it=_orders.begin();it!=_orders.end();it++){
|
||||
Order* o_ptr = *it;
|
||||
if(in_this_level(o_ptr) && o_ptr->ready_to_load_ib()){
|
||||
_aux_usage.push_back(o_ptr);
|
||||
*it = nullptr;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
_orders.erase(std::remove_if(_orders.begin(), _orders.end(), mv_ibs),
|
||||
_orders.end());
|
||||
}
|
||||
// add it back
|
||||
for (auto &o : _aux_usage) {
|
||||
o.reload_ib();
|
||||
o.priority = _priority++;
|
||||
for (auto o : _aux_usage) {
|
||||
o->reload_ib();
|
||||
o->priority = _priority++;
|
||||
_orders.push_back(o);
|
||||
}
|
||||
_aux_usage.clear();
|
||||
}
|
||||
std::tuple<MDP3Quantity, MDP3Quantity>
|
||||
FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key,
|
||||
@@ -62,8 +69,9 @@ FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key,
|
||||
std::vector<MatchEffect>* res) {
|
||||
// go through all the orders and see which ones get filled
|
||||
// (orders are sorted by priority)
|
||||
for (auto &o : _orders) {
|
||||
if (o.is_active) {
|
||||
for (Order* op : _orders) {
|
||||
if (in_this_level(op)) {
|
||||
auto& o = *op;
|
||||
auto [o_rem, cross_rem, filled] = o.cross(cross_ord_qty.value());
|
||||
cross_ord_qty = MDP3Quantity(cross_rem);
|
||||
if(res != nullptr){
|
||||
@@ -108,7 +116,6 @@ FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key,
|
||||
bts_assert(quantity().value() == 0);
|
||||
// reload the filled ib's and put them at the back
|
||||
_reload_icebergs();
|
||||
_compress();
|
||||
return std::make_tuple(total_quantity(), cross_ord_qty);
|
||||
}
|
||||
void FifoPriceLevelMatcher::_log_order(const Order& ord,
|
||||
@@ -130,28 +137,17 @@ void FifoPriceLevelMatcher::_log_order(const MDP3Order& ord,
|
||||
: MDP3Quantity(0),
|
||||
ord.price.price());
|
||||
}
|
||||
std::string FifoPriceLevelMatcher::print_mbo(size_t n_orders) const{
|
||||
std::ostringstream buff;
|
||||
auto[n_ords,qty] = n_orders_qty();
|
||||
buff << (side()==api::Side::SIDE_BUY ? "B" : "S") << "," << price();
|
||||
buff << "," << qty << "," << n_ords << ":";
|
||||
size_t i=0;
|
||||
for (const auto o : _orders){
|
||||
if(++i>n_orders)
|
||||
break;
|
||||
buff << o.key << "," << o.quantity << ":";
|
||||
}
|
||||
return buff.str();
|
||||
}
|
||||
// constructors ///////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
FifoPriceLevelMatcher::FifoPriceLevelMatcher(
|
||||
MDP3SecurityID sec,
|
||||
md::IntPrice price,
|
||||
api::Side side,
|
||||
const Trace& trace)
|
||||
: _sec(sec), _price(price), _side(side), _orders(), _aux_usage(),
|
||||
_priority(0), _trace(trace) {}
|
||||
std::map<MDP3OrderKey,FifoPriceLevelMatcher::Order>& master_order_map,
|
||||
MDP3SecurityID sec,
|
||||
md::IntPrice price,
|
||||
api::Side side,
|
||||
const Trace& trace)
|
||||
: _master_order_map(&master_order_map),
|
||||
_sec(sec), _price(price), _side(side), _orders(), _aux_usage(),
|
||||
_priority(0), _trace(trace) {}
|
||||
FifoPriceLevelMatcher::FifoPriceLevelMatcher(const FifoPriceLevelMatcher &o) {
|
||||
*this = o;
|
||||
}
|
||||
@@ -162,6 +158,7 @@ FifoPriceLevelMatcher::FifoPriceLevelMatcher(FifoPriceLevelMatcher &&o) {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
FifoPriceLevelMatcher &
|
||||
FifoPriceLevelMatcher::operator=(const FifoPriceLevelMatcher &o) {
|
||||
_master_order_map = o._master_order_map;
|
||||
_sec = o._sec;
|
||||
_price = o._price;
|
||||
_side = o._side;
|
||||
@@ -172,6 +169,7 @@ FifoPriceLevelMatcher::operator=(const FifoPriceLevelMatcher &o) {
|
||||
}
|
||||
FifoPriceLevelMatcher &
|
||||
FifoPriceLevelMatcher::operator=(FifoPriceLevelMatcher &&o) {
|
||||
_master_order_map = std::move(o._master_order_map);
|
||||
_sec = std::move(o._sec);
|
||||
_price = std::move(o._price);
|
||||
_side = std::move(o._side);
|
||||
@@ -185,40 +183,32 @@ bool FifoPriceLevelMatcher::operator<(const FifoPriceLevelMatcher &o) const {
|
||||
// bid book is sorted high to low (since highest is the top of the book)
|
||||
return _side == api::Side::SIDE_SELL ? _price < o._price : o._price < _price;
|
||||
}
|
||||
// order interactions //////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// simple accessors ///////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::tuple<uint32_t, uint32_t> FifoPriceLevelMatcher::n_orders_qty(
|
||||
bool visible_only) const{
|
||||
auto [c, q] = std::make_tuple(0, 0);
|
||||
for (Order* o : _orders)
|
||||
if(in_this_level(o)){
|
||||
c++;
|
||||
q += visible_only ? o->working_qty().value() : o->total_qty().value();
|
||||
}
|
||||
return std::make_tuple(c, q);
|
||||
}
|
||||
void FifoPriceLevelMatcher::copy_values_to(std::vector<Order>& out_v) const{
|
||||
out_v.clear();
|
||||
for(Order* o : _orders)
|
||||
if(in_this_level(o))
|
||||
out_v.push_back(*o);
|
||||
}
|
||||
// order interactions /////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void FifoPriceLevelMatcher::on_new_same_side_order(const MDP3Order &new_ord) {
|
||||
bts_assert(new_ord.price == _price);
|
||||
bts_assert(new_ord.side == _side);
|
||||
_add_order(new_ord);
|
||||
}
|
||||
void FifoPriceLevelMatcher::on_order_update(const MDP3Order &upd_ord) {
|
||||
bts_assert(upd_ord.side == _side);
|
||||
bts_assert(upd_ord.price == _price);
|
||||
FifoPriceLevelMatcher::Order *ord_p = _find(upd_ord.key);
|
||||
bts_assert(ord_p != nullptr);
|
||||
if (upd_ord.quantity.value() > ord_p->quantity.value()) {
|
||||
// remove it from the current location and add it to
|
||||
// the end
|
||||
// TODO: what is it is an IB qty increase?
|
||||
_logically_delete(*ord_p);
|
||||
_add_order(upd_ord);
|
||||
} else
|
||||
ord_p->quantity = upd_ord.quantity;
|
||||
_compress();
|
||||
}
|
||||
void FifoPriceLevelMatcher::on_order_remove(MDP3OrderKey k,
|
||||
std::vector<MatchEffect>* res) {
|
||||
FifoPriceLevelMatcher::Order *ord_p = _find(k);
|
||||
bts_assert(ord_p != nullptr);
|
||||
_logically_delete(*ord_p);
|
||||
_compress();
|
||||
OrderUpdate o = {k,OrderUpdate::Status::CANCELED,
|
||||
MDP3SecurityID{0},api::SIDE_BUY,
|
||||
MDP3Quantity(0),_price};
|
||||
if(res != nullptr)
|
||||
res->push_back(o);
|
||||
}
|
||||
|
||||
std::tuple<MDP3Quantity,MDP3Quantity> FifoPriceLevelMatcher::on_new_opp_order(
|
||||
MDP3OrderKey opp_ord_key,
|
||||
MDP3Quantity cross_qty,
|
||||
@@ -230,4 +220,81 @@ std::tuple<MDP3Quantity,MDP3Quantity> FifoPriceLevelMatcher::on_new_opp_order(
|
||||
rv = _one_pass_cross_order(opp_ord_key, std::get<1>(rv), res);
|
||||
return rv;
|
||||
}
|
||||
|
||||
void FifoPriceLevelMatcher::on_order_update(
|
||||
const MDP3Order &upd_ord,
|
||||
Order* ord_ptr_hint) {
|
||||
// ord_ptr_hint is a hint, if its populated use, else find
|
||||
bts_assert(upd_ord.side == _side);
|
||||
bts_assert(upd_ord.price == _price);
|
||||
if(ord_ptr_hint == nullptr){
|
||||
auto map_it = _master_order_map->find(upd_ord.key);
|
||||
bts_assert(map_it != _master_order_map->end());
|
||||
ord_ptr_hint = &map_it->second;
|
||||
}
|
||||
FifoPriceLevelMatcher::Order& ord = *ord_ptr_hint;
|
||||
if (upd_ord.quantity.value() > ord.quantity.value()) {
|
||||
// remove it from the current location O(N) and add it to
|
||||
// the end. unlike qty decreases this is a lot more expensive
|
||||
remove_from_price_level(ord.key);
|
||||
_orders.push_back(&ord);
|
||||
// udpate value
|
||||
ord.quantity = upd_ord.quantity;
|
||||
} else
|
||||
ord.quantity = upd_ord.quantity;
|
||||
}
|
||||
void FifoPriceLevelMatcher::on_order_cancel(Order& order,
|
||||
std::vector<MatchEffect>* res){
|
||||
_logically_delete(order);
|
||||
OrderUpdate o = {
|
||||
order.key,
|
||||
OrderUpdate::Status::CANCELED,
|
||||
_sec,
|
||||
_side,
|
||||
MDP3Quantity(0),
|
||||
_price
|
||||
};
|
||||
if(res != nullptr)
|
||||
res->push_back(o);
|
||||
}
|
||||
void FifoPriceLevelMatcher::on_order_remove(MDP3OrderKey k,
|
||||
std::vector<MatchEffect>* res) {
|
||||
FifoPriceLevelMatcher::Order *ord_p = _find(k);
|
||||
bts_assert(ord_p != nullptr);
|
||||
on_order_cancel(*ord_p,res);
|
||||
}
|
||||
void FifoPriceLevelMatcher::remove_from_price_level(MDP3OrderKey k) {
|
||||
// puts a nullptr in place of where this order was
|
||||
// (perioric cleanup will physically delete the slot)
|
||||
auto it = std::find_if(_orders.begin(), _orders.end(),
|
||||
[&](Order* o) {
|
||||
return in_this_level(o) && o->key == k;
|
||||
});
|
||||
bts_assert(it != _orders.end());
|
||||
bts_assert(*it != nullptr);
|
||||
*it = nullptr;
|
||||
}
|
||||
|
||||
void FifoPriceLevelMatcher::cleanup() {
|
||||
_orders.erase(std::remove_if(_orders.begin(), _orders.end(),
|
||||
[](const Order* o) {
|
||||
return !in_this_level(o);
|
||||
}),
|
||||
_orders.end());
|
||||
}
|
||||
std::string FifoPriceLevelMatcher::print_mbo(size_t n_orders) const{
|
||||
std::ostringstream buff;
|
||||
auto[n_ords,qty] = n_orders_qty();
|
||||
buff << (side()==api::Side::SIDE_BUY ? "B" : "S") << "," << price();
|
||||
buff << "," << qty << "," << n_ords << ":";
|
||||
size_t i=0;
|
||||
for (Order* o : _orders){
|
||||
if(in_this_level(o)){
|
||||
if(++i>n_orders)
|
||||
break;
|
||||
buff << o->key << "," << o->quantity << ":";
|
||||
}
|
||||
}
|
||||
return buff.str();
|
||||
}
|
||||
} // namespace bts::spartan::mdp3
|
||||
+56
-34
@@ -31,16 +31,36 @@ class FifoPriceLevelMatcher {
|
||||
};
|
||||
struct Order {
|
||||
MDP3OrderKey key;
|
||||
api::Side side;
|
||||
md::IntPrice price;
|
||||
MDP3Quantity quantity;
|
||||
uint32_t priority = 0;
|
||||
IB ib;
|
||||
bool is_active = true;
|
||||
Order(MDP3OrderKey key,
|
||||
api::Side side,
|
||||
md::IntPrice price,
|
||||
MDP3Quantity qty, // working qty out there
|
||||
uint32_t priority, IB ib)
|
||||
: key(key), quantity(qty), priority(priority), ib(ib) {}
|
||||
Order(MDP3OrderKey key, MDP3Quantity qty, uint32_t priority)
|
||||
: Order(key, qty, priority, IB()) {}
|
||||
uint32_t priority,
|
||||
IB ib)
|
||||
: key(key),
|
||||
side(side),
|
||||
price(price),
|
||||
quantity(qty),
|
||||
priority(priority),
|
||||
ib(ib) {}
|
||||
Order(MDP3OrderKey key,
|
||||
api::Side side,
|
||||
md::IntPrice price,
|
||||
MDP3Quantity qty, // working qty out there
|
||||
uint32_t priority)
|
||||
: Order(
|
||||
key,
|
||||
side,
|
||||
price,
|
||||
qty,
|
||||
priority,
|
||||
IB()) {}
|
||||
// returns the remainder of this order, remainder of opp order
|
||||
// (ie the new qty NOT the filled qty, for that subtract from
|
||||
// original)
|
||||
@@ -50,9 +70,12 @@ class FifoPriceLevelMatcher {
|
||||
bts_assert(quantity.value() - r == cross_qty - c);
|
||||
auto filled = cross_qty - c;
|
||||
quantity = MDP3Quantity(r);
|
||||
if (ib.is_ib)
|
||||
if (ib.is_ib){
|
||||
ib.total_qty.value() = ib.total_qty.value() - filled;
|
||||
is_active = quantity.value() > 0;
|
||||
is_active = ib.total_qty.value() > 0;
|
||||
}
|
||||
else
|
||||
is_active = quantity.value() > 0;
|
||||
return std::make_tuple(r, c, filled);
|
||||
}
|
||||
MDP3Quantity total_qty() const {
|
||||
@@ -72,17 +95,21 @@ class FifoPriceLevelMatcher {
|
||||
};
|
||||
|
||||
private:
|
||||
// this is the order_map that the sec level matcher owns
|
||||
// for all the orders for the security. orders are created
|
||||
// here but added to the map mantained by the
|
||||
// sec level matcher
|
||||
std::map<MDP3OrderKey,FifoPriceLevelMatcher::Order>* _master_order_map;
|
||||
MDP3SecurityID _sec;
|
||||
md::IntPrice _price;
|
||||
api::Side _side;
|
||||
std::vector<FifoPriceLevelMatcher::Order> _orders;
|
||||
std::vector<FifoPriceLevelMatcher::Order> _aux_usage;
|
||||
std::vector<FifoPriceLevelMatcher::Order*> _orders;
|
||||
std::vector<FifoPriceLevelMatcher::Order*> _aux_usage;
|
||||
uint32_t _priority;
|
||||
Trace _trace;
|
||||
|
||||
// helpers ////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
private:
|
||||
// helpers /////////////////////////////////////////////////////////////////
|
||||
void _compress();
|
||||
Order *_find(MDP3OrderKey key);
|
||||
void _logically_delete(Order &o);
|
||||
void _add_order(const MDP3Order &ord);
|
||||
@@ -90,7 +117,7 @@ class FifoPriceLevelMatcher {
|
||||
// the remaining qty in the level and the cross order
|
||||
void _reload_icebergs();
|
||||
// crosses the order in the args with the orders at this level.
|
||||
// returns the tuple of total qty at this level and the new qty
|
||||
// returns the tuple of rem qty at this level and the rem qty
|
||||
// of the cross order
|
||||
std::tuple<MDP3Quantity, MDP3Quantity>
|
||||
_one_pass_cross_order(MDP3OrderKey cross_ord_key,
|
||||
@@ -101,11 +128,14 @@ class FifoPriceLevelMatcher {
|
||||
void _log_order(const MDP3Order& ord, const std::string& msg);
|
||||
public:
|
||||
// constructors ////////////////////////////////////////////////////////////
|
||||
FifoPriceLevelMatcher(MDP3SecurityID sec, md::IntPrice price,
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
FifoPriceLevelMatcher(std::map<MDP3OrderKey,FifoPriceLevelMatcher::Order>&,
|
||||
MDP3SecurityID sec, md::IntPrice price,
|
||||
api::Side side, const Trace&);
|
||||
FifoPriceLevelMatcher(const FifoPriceLevelMatcher &);
|
||||
FifoPriceLevelMatcher(FifoPriceLevelMatcher &&);
|
||||
// operator overloads //////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
FifoPriceLevelMatcher &operator=(const FifoPriceLevelMatcher &o);
|
||||
FifoPriceLevelMatcher &operator=(FifoPriceLevelMatcher &&o);
|
||||
bool operator==(const FifoPriceLevelMatcher &o) const {
|
||||
@@ -118,15 +148,8 @@ class FifoPriceLevelMatcher {
|
||||
}
|
||||
bool operator<(const FifoPriceLevelMatcher &o) const;
|
||||
// simple accessors ////////////////////////////////////////////////////////
|
||||
std::tuple<uint32_t, uint32_t> n_orders_qty(bool visible_only = true) const {
|
||||
auto [c, q] = std::make_tuple(0, 0);
|
||||
for (const auto &o : _orders)
|
||||
if (o.is_active) {
|
||||
c++;
|
||||
q += visible_only ? o.working_qty().value() : o.total_qty().value();
|
||||
}
|
||||
return std::make_tuple(c, q);
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::tuple<uint32_t, uint32_t> n_orders_qty(bool visible_only = true) const;
|
||||
MDP3Quantity quantity() const {
|
||||
return MDP3Quantity(std::get<1>(n_orders_qty()));
|
||||
}
|
||||
@@ -139,36 +162,35 @@ class FifoPriceLevelMatcher {
|
||||
double price_double() const { return _price.price(); }
|
||||
api::Side side() const { return _side; }
|
||||
Order *find_order(MDP3OrderKey key) { return _find(key); }
|
||||
std::tuple<MDP3OrderKey, MDP3Quantity, uint32_t> top_order() const {
|
||||
if (_orders.begin() == _orders.end())
|
||||
return std::make_tuple(MDP3OrderKey(0), MDP3Quantity(0), (uint32_t)0);
|
||||
else {
|
||||
const auto &ord = *_orders.begin();
|
||||
return std::make_tuple(ord.key, ord.quantity, ord.priority);
|
||||
}
|
||||
}
|
||||
BookLevel book_level() const {
|
||||
return BookLevel{_price, quantity(), static_cast<int>(n_orders())};
|
||||
}
|
||||
// used for testing
|
||||
void copy_values_to(std::vector<Order>& out_v) const;
|
||||
// mutators (silly reason) /////////////////////////////////////////////////
|
||||
void set_price_side(md::IntPrice p, api::Side s) {
|
||||
_price = p;
|
||||
_side = s;
|
||||
}
|
||||
const std::vector<Order> &orders() { return _orders; }
|
||||
// order interactions //////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// order with the same side and price as this price level
|
||||
void on_new_same_side_order(const MDP3Order &new_ord);
|
||||
// order update such that only the qty changed (other changes
|
||||
// are handled at a higher level)
|
||||
void on_order_update(const MDP3Order &upd_ord);
|
||||
void on_order_remove(MDP3OrderKey k,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
// new order such that it crosses this price level
|
||||
std::tuple<MDP3Quantity,MDP3Quantity> on_new_opp_order(
|
||||
MDP3OrderKey opp_ord_key,
|
||||
MDP3Quantity opp_ord_qty,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
void on_order_update(const MDP3Order& upd_ord,
|
||||
Order* ord_hint = nullptr);
|
||||
void on_order_cancel(Order& order,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
void on_order_remove(MDP3OrderKey k,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
void remove_from_price_level(MDP3OrderKey k);
|
||||
void cleanup();
|
||||
std::string print_mbo(size_t n_orders) const;
|
||||
};
|
||||
|
||||
|
||||
+141
-124
@@ -1,5 +1,6 @@
|
||||
#include "FifoSecurityMatcher.h"
|
||||
#include "../../common/InstrumentSnapshot.h"
|
||||
#include <experimental/map>
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
@@ -24,12 +25,6 @@ inline api::Side _opp_side(api::Side side) {
|
||||
: api::Side::SIDE_BUY;
|
||||
}
|
||||
|
||||
inline FifoPriceLevelMatcher *FifoSecurityMatcher::_top(api::Side side) {
|
||||
auto &bk = _book(side);
|
||||
auto it = bk.begin();
|
||||
return it != bk.end() ? &*it : nullptr;
|
||||
}
|
||||
|
||||
// O(lg(N)) on the price levels
|
||||
FifoPriceLevelMatcher *
|
||||
FifoSecurityMatcher::_find_price_level(api::Side side, md::IntPrice price) {
|
||||
@@ -41,123 +36,54 @@ FifoSecurityMatcher::_find_price_level(api::Side side, md::IntPrice price) {
|
||||
|
||||
std::tuple<FifoPriceLevelMatcher *, FifoPriceLevelMatcher::Order *>
|
||||
FifoSecurityMatcher::_find_order(MDP3OrderKey key) {
|
||||
for (auto &p_level : _bid_book) {
|
||||
auto o_ptr = p_level.find_order(key);
|
||||
if (o_ptr != nullptr)
|
||||
return std::make_tuple(&p_level, o_ptr);
|
||||
}
|
||||
for (auto &p_level : _ask_book) {
|
||||
auto o_ptr = p_level.find_order(key);
|
||||
if (o_ptr != nullptr)
|
||||
return std::make_tuple(&p_level, o_ptr);
|
||||
}
|
||||
auto map_it = _order_map.find(key);
|
||||
if(map_it != _order_map.end()){
|
||||
auto& ord = map_it->second;
|
||||
FifoPriceLevelMatcher* p_level = _find_price_level(ord.side,ord.price);
|
||||
return std::make_tuple(p_level,&ord);
|
||||
}
|
||||
return std::tuple<FifoPriceLevelMatcher *, FifoPriceLevelMatcher::Order *>(
|
||||
nullptr, nullptr);
|
||||
}
|
||||
|
||||
void FifoSecurityMatcher::_remove_empty_price_levels() {
|
||||
// this is expensive
|
||||
void FifoSecurityMatcher::_cleanup() {
|
||||
// remove orders from the price levels
|
||||
for (auto& p : _bid_book)
|
||||
p.cleanup();
|
||||
for (auto& p : _ask_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());
|
||||
}
|
||||
void FifoSecurityMatcher::_log_trace_order_state(const std::string& msg){
|
||||
for (const auto& kv : _trace.traced_orders){
|
||||
MDP3OrderKey k = kv.first;
|
||||
auto[pl,o] = _find_order(k);
|
||||
if(pl != nullptr && o != nullptr)
|
||||
LOG(OrderMan, Info,
|
||||
"Order state msg:{} eid:{} key:{} qty:{} ib_show_qty:{} ib_total_qty:{} "
|
||||
"price:{}",
|
||||
msg, _event_id, o->key, o->quantity, o->ib.show_qty, o->ib.total_qty,
|
||||
pl->price());
|
||||
}
|
||||
}
|
||||
void FifoSecurityMatcher::_log_trace_order(const MDP3Order& ord,
|
||||
const std::string& msg){
|
||||
if(_trace.log(ord.key,_event_id)){
|
||||
LOG(OrderMan, Info,
|
||||
"{} eid:{} key:{} side:{} qty:{} ib_qty:{} price:{} book:{}",
|
||||
msg, _event_id, ord.key, static_cast<int>(ord.side), ord.quantity,
|
||||
ord.iceberg_quantity.has_value() ? ord.iceberg_quantity.value()
|
||||
: MDP3Quantity(0),
|
||||
ord.price.price(), _print_book());
|
||||
_log_trace_order_state(msg);
|
||||
}
|
||||
}
|
||||
void FifoSecurityMatcher::_log_mbo(size_t n_levels, size_t n_orders){
|
||||
if(_trace.log(_event_id)){
|
||||
std::ostringstream buff;
|
||||
buff << "eid:" << _event_id << std::endl;
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
}
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
}
|
||||
auto msg = buff.str();
|
||||
LOG(OrderMan, Info, "{}", msg);
|
||||
}
|
||||
}
|
||||
std::string FifoSecurityMatcher::_print_book(size_t n_levels) const {
|
||||
// auto rem_if = [](const auto& p_lev) { return !p_lev.populated(); };
|
||||
std::ostringstream buff;
|
||||
auto add_to_buff = [&](const auto &pl) {
|
||||
std::string side = pl.side() == api::Side::SIDE_BUY ? "B" : "S";
|
||||
buff << side << ":" << pl.price_double() << ":" << pl.quantity().value()
|
||||
<< ":" << pl.n_orders() << std::endl;
|
||||
};
|
||||
// asks are stored lowest to highest
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
}
|
||||
// bids are scored highest to lowest
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
}
|
||||
return buff.str();
|
||||
// remove inactive orders from map
|
||||
std::experimental::erase_if(_order_map, [](const auto& p) {
|
||||
auto& ord = std::get<1>(p);
|
||||
return !ord.is_active;
|
||||
});
|
||||
}
|
||||
void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
|
||||
std::vector<MatchEffect>* res) {
|
||||
_log_trace_order(ord,"FifoSecurityMatcher::_on_new_order begin");
|
||||
bts_assert(res != nullptr || res == nullptr);
|
||||
// auto t = _find_order(ord.key);
|
||||
// bts_assert_msg(std::get<1>(t) == nullptr,
|
||||
// "repeat order_key:{}" + std::to_string(ord.key.value()));
|
||||
auto& opp_book = _book(_opp_side(ord.side));
|
||||
uint32_t rem = total_quantity(ord).value();
|
||||
bool level_swept = false;
|
||||
uint32_t new_ord_rem = total_quantity(ord).value();
|
||||
for (FifoPriceLevelMatcher &pl : opp_book) {
|
||||
if (_is_cross(ord, pl)){
|
||||
auto[pl_rem,ord_rem] = pl.on_new_opp_order(ord.key, MDP3Quantity(rem),res);
|
||||
rem = ord_rem.value();
|
||||
level_swept = level_swept || pl_rem.value()==0;
|
||||
auto t = pl.on_new_opp_order(ord.key, MDP3Quantity(new_ord_rem),res);
|
||||
new_ord_rem = std::get<1>(t).value();
|
||||
}
|
||||
else
|
||||
break;
|
||||
if (rem == 0)
|
||||
if (new_ord_rem == 0)
|
||||
break;
|
||||
bts_assert(pl.quantity().value() == 0);
|
||||
// stops crossing when the price level is not a cross or there
|
||||
// no qty left
|
||||
}
|
||||
// _remove_empty_price_levels adds up when there are a lot of levels
|
||||
if(level_swept)
|
||||
_remove_empty_price_levels();
|
||||
if (rem != 0) {
|
||||
if (new_ord_rem != 0) {
|
||||
// either the price did not cross into the opp market
|
||||
// or it cleared out the entire opposite side
|
||||
// in either case, it becomes a resting order
|
||||
@@ -165,7 +91,8 @@ void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
|
||||
_find_price_level(ord.side, ord.price);
|
||||
if (price_level == nullptr) {
|
||||
auto &same_side_book = _book(ord.side);
|
||||
auto p = same_side_book.emplace(_sec, ord.price, ord.side, _trace);
|
||||
// this is the only place where a new price level is created
|
||||
auto p = same_side_book.emplace(_order_map, _sec, ord.price, ord.side, _trace);
|
||||
bts_assert(std::get<1>(p));
|
||||
price_level = &*std::get<0>(p);
|
||||
}
|
||||
@@ -175,41 +102,40 @@ void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
|
||||
static_cast<uint32_t>(ord.iceberg_quantity.value().value());
|
||||
// this is not exactly correct
|
||||
uint32_t show = static_cast<uint32_t>(ord.quantity.value());
|
||||
uint32_t filled = tot-rem;
|
||||
ord_corrected.iceberg_quantity = MDP3Quantity(rem);
|
||||
uint32_t filled = tot-new_ord_rem;
|
||||
ord_corrected.iceberg_quantity = MDP3Quantity(new_ord_rem);
|
||||
uint32_t working;
|
||||
if(filled < show)
|
||||
working = show - filled;
|
||||
else
|
||||
working = std::min(rem, show);
|
||||
working = std::min(new_ord_rem, show);
|
||||
ord_corrected.quantity = MDP3Quantity(working);
|
||||
} else
|
||||
ord_corrected.quantity = MDP3Quantity(rem);
|
||||
ord_corrected.quantity = MDP3Quantity(new_ord_rem);
|
||||
price_level->on_new_same_side_order(ord_corrected);
|
||||
}
|
||||
//_log_trace_order(ord,"FifoSecurityMatcher::_on_new_order end");
|
||||
}
|
||||
|
||||
void FifoSecurityMatcher::_on_order_update(const MDP3Order &ord,
|
||||
std::vector<MatchEffect>* res) {
|
||||
_log_trace_order(ord,"FifoSecurityMatcher::_on_order_update begin");
|
||||
auto[price_level,ex_ord] = _find_order(ord.key);
|
||||
if(price_level == nullptr || ex_ord == nullptr){
|
||||
auto[price_level,curr_ord] = _find_order(ord.key);
|
||||
if(price_level == nullptr || curr_ord == nullptr){
|
||||
if(_ignore_ops_on_deleted_orders)
|
||||
return;
|
||||
}
|
||||
bts_assert(price_level != nullptr);
|
||||
bts_assert(ex_ord != nullptr);
|
||||
bts_assert(curr_ord != nullptr);
|
||||
// side can not be changed (or can it?)
|
||||
bts_assert(price_level->side() == ord.side);
|
||||
// check for price change
|
||||
if (price_level->price() != ord.price) {
|
||||
// remove from current level and add to another
|
||||
_on_order_remove(ord.key);
|
||||
price_level->remove_from_price_level(curr_ord->key);
|
||||
_on_new_order(ord, res);
|
||||
} else {
|
||||
// qty change - price_level will handle it
|
||||
price_level->on_order_update(ord);
|
||||
price_level->on_order_update(ord,curr_ord);
|
||||
}
|
||||
_log_trace_order(ord,"FifoSecurityMatcher::on_order_update end");
|
||||
}
|
||||
@@ -221,16 +147,19 @@ void FifoSecurityMatcher::_on_order_remove(MDP3OrderKey key,
|
||||
"FifoSecurityMatcher::_on_order_remove "
|
||||
"eid:{} key:{}",
|
||||
_event_id, key);
|
||||
auto[price_level,ex_ord] = _find_order(key);
|
||||
if(price_level == nullptr || ex_ord == nullptr){
|
||||
|
||||
auto[price_level,curr_ord] = _find_order(key);
|
||||
if(price_level == nullptr || curr_ord == nullptr)
|
||||
if(_ignore_ops_on_deleted_orders)
|
||||
return;
|
||||
}
|
||||
bts_assert(price_level != nullptr);
|
||||
bts_assert(ex_ord != nullptr);
|
||||
bts_assert(key == ex_ord->key);
|
||||
price_level->on_order_remove(key, res);
|
||||
_remove_empty_price_levels();
|
||||
bts_assert(curr_ord != nullptr);
|
||||
price_level->on_order_cancel(*curr_ord,res);
|
||||
// we can short circuit this by setting order's is_active
|
||||
// marker to false here, but its more consistent to let
|
||||
// the price level handlers take care of this
|
||||
//(which does make it O(lg(M*N)) instead of lg(N), for the
|
||||
// extra price level lookup)
|
||||
}
|
||||
// constructors ///////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -256,13 +185,17 @@ FifoSecurityMatcher::FifoSecurityMatcher(MDP3SecurityID security,
|
||||
FifoSecurityMatcher::FifoSecurityMatcher(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs args)
|
||||
:
|
||||
_order_map(),
|
||||
_bid_book(),
|
||||
_ask_book(),
|
||||
_search_tok(MDP3SecurityID{}, md::IntPrice(), api::Side::SIDE_BUY, Trace()),
|
||||
_search_tok(_order_map, MDP3SecurityID{},
|
||||
md::IntPrice(), api::Side::SIDE_BUY, Trace()),
|
||||
_sec(args.security_id),
|
||||
_event_id(args.event_id),
|
||||
_trace(),
|
||||
_ignore_ops_on_deleted_orders(args.ignore_ops_on_deleted_orders){
|
||||
_ignore_ops_on_deleted_orders(args.ignore_ops_on_deleted_orders),
|
||||
_clean_up_op_threshold(100000),
|
||||
_running_op_count(0) {
|
||||
if(args.orders != nullptr){
|
||||
const auto& orders = *args.orders;
|
||||
std::vector<MDP3Order> sorted_orders;
|
||||
@@ -281,6 +214,7 @@ FifoSecurityMatcher::FifoSecurityMatcher(
|
||||
LOG(OrderMan, Info, "FifoSecurityMatcher::cons end eid:{}", _event_id);
|
||||
_log_mbo(10,100);
|
||||
}
|
||||
_running_op_count = 0;
|
||||
}
|
||||
FifoSecurityMatcher::~FifoSecurityMatcher(){
|
||||
_log_trace_order_state("FifoSecurityMatcher::dest");
|
||||
@@ -291,13 +225,21 @@ Book FifoSecurityMatcher::book(api::Side side) const {
|
||||
auto i = 0;
|
||||
Book rv;
|
||||
for (const auto &p_level : _book(side)) {
|
||||
if (i < MDP3_MAX_BOOK_DEPTH)
|
||||
rv[i++] = p_level.book_level();
|
||||
else
|
||||
break;
|
||||
if(p_level.populated()){
|
||||
if (i < MDP3_MAX_BOOK_DEPTH)
|
||||
rv[i++] = p_level.book_level();
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
size_t FifoSecurityMatcher::total_n_orders(api::Side side) const{
|
||||
auto rv=0;
|
||||
for (const auto& p_level : _book(side))
|
||||
rv+=p_level.n_orders();
|
||||
return rv;
|
||||
}
|
||||
std::tuple<md::IntPrice, MDP3Quantity> FifoSecurityMatcher::book_level (
|
||||
api::Side side, size_t idx) const {
|
||||
size_t i = 0;
|
||||
@@ -319,6 +261,10 @@ void FifoSecurityMatcher::on_order_change(OrderChange const &o,
|
||||
_on_order_remove(o.order.key, res);
|
||||
break;
|
||||
}
|
||||
if(_running_op_count++ > _clean_up_op_threshold){
|
||||
_running_op_count = 0;
|
||||
_cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string>
|
||||
@@ -336,7 +282,7 @@ FifoSecurityMatcher::has_same_orders(const Orders &ord_map) {
|
||||
std::ostringstream buff;
|
||||
buff << "eid:" << _event_id << " n_ords:" << ord_map.size()
|
||||
<< std::endl;
|
||||
buff << "Book:" << _print_book(10) << std::endl;
|
||||
buff << "Book:" << print_book(10) << std::endl;
|
||||
if (ord == nullptr){
|
||||
buff << "Order with Key:" << exp_ord.key.value() << " not found"
|
||||
<< std::endl;
|
||||
@@ -375,4 +321,75 @@ FifoSecurityMatcher::has_same_orders(const Orders &ord_map) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// logging and printing ///////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::string FifoSecurityMatcher::print_book(size_t n_levels) const {
|
||||
// auto rem_if = [](const auto& p_lev) { return !p_lev.populated(); };
|
||||
std::ostringstream buff;
|
||||
auto add_to_buff = [&](const auto &pl) {
|
||||
std::string side = pl.side() == api::Side::SIDE_BUY ? "B" : "S";
|
||||
buff << side << ":" << pl.price_double() << ":" << pl.quantity().value()
|
||||
<< ":" << pl.n_orders() << std::endl;
|
||||
};
|
||||
// asks are stored lowest to highest
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
}
|
||||
// bids are scored highest to lowest
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
add_to_buff(pl);
|
||||
}
|
||||
return buff.str();
|
||||
}
|
||||
void FifoSecurityMatcher::_log_trace_order_state(const std::string& msg){
|
||||
for (const auto& kv : _trace.traced_orders){
|
||||
MDP3OrderKey k = kv.first;
|
||||
auto[pl,o] = _find_order(k);
|
||||
if(pl != nullptr && o != nullptr)
|
||||
LOG(OrderMan, Info,
|
||||
"Order state msg:{} eid:{} key:{} qty:{} ib_show_qty:{} ib_total_qty:{} "
|
||||
"price:{}",
|
||||
msg, _event_id, o->key, o->quantity, o->ib.show_qty, o->ib.total_qty,
|
||||
pl->price());
|
||||
}
|
||||
}
|
||||
void FifoSecurityMatcher::_log_trace_order(const MDP3Order& ord,
|
||||
const std::string& msg){
|
||||
if(_trace.log(ord.key,_event_id)){
|
||||
LOG(OrderMan, Info,
|
||||
"{} eid:{} key:{} side:{} qty:{} ib_qty:{} price:{} book:{}",
|
||||
msg, _event_id, ord.key, static_cast<int>(ord.side), ord.quantity,
|
||||
ord.iceberg_quantity.has_value() ? ord.iceberg_quantity.value()
|
||||
: MDP3Quantity(0),
|
||||
ord.price.price(), print_book());
|
||||
_log_trace_order_state(msg);
|
||||
}
|
||||
}
|
||||
void FifoSecurityMatcher::_log_mbo(size_t n_levels, size_t n_orders){
|
||||
if(_trace.log(_event_id)){
|
||||
std::ostringstream buff;
|
||||
buff << "eid:" << _event_id << std::endl;
|
||||
size_t i=0;
|
||||
for (const auto &pl : _ask_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
}
|
||||
i=0;
|
||||
for (const auto &pl : _bid_book){
|
||||
if(++i>n_levels)
|
||||
break;
|
||||
buff << pl.print_mbo(n_orders) << std::endl;
|
||||
}
|
||||
auto msg = buff.str();
|
||||
LOG(OrderMan, Info, "{}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -19,7 +19,15 @@ public:
|
||||
Orders const* orders = nullptr;
|
||||
};
|
||||
private:
|
||||
// ask prices are sorted lowest to highets and and bid prices
|
||||
// for orders we need a structure to provide fast access
|
||||
// and where adding and removing elements does not invalidate
|
||||
// iterators (or cached pointers)
|
||||
// https://en.cppreference.com/w/cpp/container#Iterator_invalidation
|
||||
// while map (trees) are slower than unordered_map (pure hashtables),
|
||||
// pointers get invalidated when they are rehashed (since we dont
|
||||
// have a key collision, would it be rehashed?)
|
||||
std::map<MDP3OrderKey,FifoPriceLevelMatcher::Order> _order_map;
|
||||
// ask prices are sorted lowest to highest and and bid prices
|
||||
// are sorted lowest to highest
|
||||
boost::container::flat_set<FifoPriceLevelMatcher> _bid_book, _ask_book;
|
||||
FifoPriceLevelMatcher _search_tok;
|
||||
@@ -27,12 +35,12 @@ public:
|
||||
const size_t _event_id;
|
||||
const Trace _trace;
|
||||
const bool _ignore_ops_on_deleted_orders;
|
||||
const size_t _clean_up_op_threshold;
|
||||
size_t _running_op_count;
|
||||
|
||||
private:
|
||||
// helpers /////////////////////////////////////////////////////////////////
|
||||
// returns the top of the side in the arg; nullptr
|
||||
// if the side does not have any prices. complexity is
|
||||
// O(1)
|
||||
FifoPriceLevelMatcher *_top(api::Side);
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// complexity: O(lg(n))
|
||||
FifoPriceLevelMatcher *_find_price_level(api::Side, md::IntPrice price);
|
||||
boost::container::flat_set<FifoPriceLevelMatcher> &_book(api::Side side) {
|
||||
@@ -42,44 +50,43 @@ public:
|
||||
_book(api::Side side) const {
|
||||
return side == api::Side::SIDE_BUY ? _bid_book : _ask_book;
|
||||
}
|
||||
// returns the price level and the order within that level.
|
||||
// complexity: O(n^2) (searching through each price level
|
||||
// and within a price level searching through the orders)
|
||||
// this could be improved to O(lg(n)), if order keys are stored
|
||||
// in a boost::flat_set pointing to their PriceLevel
|
||||
// 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
|
||||
// for price level in the book (flat_set)
|
||||
std::tuple<FifoPriceLevelMatcher *, FifoPriceLevelMatcher::Order *>
|
||||
_find_order(MDP3OrderKey key);
|
||||
// for performance reasons we should reuse empty price levels
|
||||
// (right now, for simplcity, we just remove)
|
||||
void _remove_empty_price_levels();
|
||||
void _log_trace_order_state(const std::string& msg);
|
||||
void _log_trace_order(const MDP3Order& ord, const std::string& msg);
|
||||
void _log_mbo(size_t n_levels,size_t n_orders);
|
||||
std::string _print_book(size_t n_levels=3) const;
|
||||
void _cleanup();
|
||||
// order interaction
|
||||
void _on_new_order(const MDP3Order&, std::vector<MatchEffect>* = nullptr);
|
||||
void _on_order_remove(MDP3OrderKey,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
void _on_order_update(const MDP3Order&, std::vector<MatchEffect>* = nullptr);
|
||||
void _on_order_remove(MDP3OrderKey,
|
||||
std::vector<MatchEffect>* = nullptr);
|
||||
public:
|
||||
// cons ////////////////////////////////////////////////////////////////////
|
||||
// cons/dest ///////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
FifoSecurityMatcher(MDP3SecurityID security, size_t event_id = 0);
|
||||
FifoSecurityMatcher(MDP3SecurityID security,
|
||||
size_t event_id,
|
||||
Orders const &);
|
||||
FifoSecurityMatcher(FifoSecurityMatcher::FifoSecurityMatcherArgs);
|
||||
// destructor //////////////////////////////////////////////////////////////
|
||||
~FifoSecurityMatcher();
|
||||
// SecurityMatcher implementation //////////////////////////////////////////
|
||||
Book book(api::Side) const override;
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
Book book(api::Side) const override;
|
||||
std::tuple<md::IntPrice, MDP3Quantity> book_level(
|
||||
api::Side side, size_t idx) const override;
|
||||
void on_order_change(OrderChange const &,
|
||||
std::vector<MatchEffect>* = nullptr) override ;
|
||||
// For testing
|
||||
std::optional<std::string> has_same_orders(Orders const &);
|
||||
std::string print_book() const{
|
||||
return _print_book(1000);
|
||||
}
|
||||
std::optional<std::string> has_same_orders(Orders const &);
|
||||
size_t total_n_orders(api::Side) const;
|
||||
// debugging logging //////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
public:
|
||||
std::string print_book(size_t n_levels=3) const;
|
||||
private:
|
||||
void _log_trace_order_state(const std::string& msg);
|
||||
void _log_trace_order(const MDP3Order& ord, const std::string& msg);
|
||||
void _log_mbo(size_t n_levels,size_t n_orders);
|
||||
};
|
||||
} // namespace bts::spartan::mdp3
|
||||
+159
-33
@@ -35,7 +35,7 @@ namespace bts::spartan::mdp3::tests {
|
||||
|
||||
class RandomGenerator{
|
||||
public:
|
||||
std::random_device rd;
|
||||
std::random_device rd;
|
||||
double random_double(double min, double max){
|
||||
//Standard mersenne_twister_engine seeded with rd()
|
||||
std::mt19937 gen(rd());
|
||||
@@ -78,7 +78,9 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
|
||||
double price = 100.0;
|
||||
api::Side side = api::Side::SIDE_BUY;
|
||||
Trace trace;
|
||||
FifoPriceLevelMatcher price_level(MDP3SecurityID(0),
|
||||
std::map<MDP3OrderKey, FifoPriceLevelMatcher::Order> master_ord_map;
|
||||
FifoPriceLevelMatcher price_level(master_ord_map,
|
||||
MDP3SecurityID(0),
|
||||
md::IntPrice_from_double<7>(price),
|
||||
side,
|
||||
trace);
|
||||
@@ -96,7 +98,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
|
||||
REQUIRE(price_level.n_orders() == 100);
|
||||
REQUIRE(price_level.quantity().value() == 5050);
|
||||
}
|
||||
SECTION("remove") {
|
||||
SECTION("cancel") {
|
||||
create_book(100);
|
||||
REQUIRE(price_level.n_orders() == 100);
|
||||
REQUIRE(price_level.quantity().value() == 5050);
|
||||
@@ -117,12 +119,14 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
|
||||
REQUIRE(price_level.quantity().value() == 0);
|
||||
}
|
||||
SECTION("full_fill") {
|
||||
std::vector<FifoPriceLevelMatcher::Order> out_ords;
|
||||
create_book(100);
|
||||
// take out the first 20 completely
|
||||
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(210));
|
||||
REQUIRE(price_level.n_orders() == 80);
|
||||
REQUIRE(price_level.quantity().value() == 4840);
|
||||
REQUIRE(price_level.orders().at(79).key.value() == 100);
|
||||
REQUIRE(price_level.quantity().value() == 4840);
|
||||
price_level.copy_values_to(out_ords);
|
||||
REQUIRE(out_ords.at(79).key.value() == 100);
|
||||
}
|
||||
SECTION("partial_fill") {
|
||||
create_book(100);
|
||||
@@ -132,27 +136,33 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
|
||||
REQUIRE(price_level.quantity().value() == 4857);
|
||||
}
|
||||
SECTION("qty_decrease") {
|
||||
std::vector<FifoPriceLevelMatcher::Order> out_ords;
|
||||
create_book(100);
|
||||
auto &ord_59 = price_level.orders().at(59);
|
||||
price_level.copy_values_to(out_ords);
|
||||
auto &ord_59 = out_ords.at(59);
|
||||
REQUIRE(ord_59.quantity.value() == 60);
|
||||
auto ord_59_mod = create_order(ord_59.key.value(), 60,
|
||||
api::Side::SIDE_BUY, 0, price, 20);
|
||||
price_level.on_order_update(ord_59_mod);
|
||||
// stays at the same level
|
||||
REQUIRE(price_level.orders().at(59).quantity.value() == 20);
|
||||
REQUIRE(price_level.orders().at(59).key.value() == 60);
|
||||
price_level.copy_values_to(out_ords);
|
||||
REQUIRE(out_ords.at(59).quantity.value() == 20);
|
||||
REQUIRE(out_ords.at(59).key.value() == 60);
|
||||
REQUIRE(price_level.n_orders() == 100);
|
||||
REQUIRE(price_level.quantity().value() == 5010);
|
||||
}
|
||||
SECTION("qty_increase") {
|
||||
std::vector<FifoPriceLevelMatcher::Order> out_ords;
|
||||
create_book(100);
|
||||
auto &ord_59 = price_level.orders().at(59);
|
||||
price_level.copy_values_to(out_ords);
|
||||
auto &ord_59 = out_ords.at(59);
|
||||
REQUIRE(ord_59.quantity.value() == 60);
|
||||
auto ord_59_mod = create_order(ord_59.key.value(), 60,
|
||||
api::Side::SIDE_BUY, 0, price, 61);
|
||||
price_level.on_order_update(ord_59_mod);
|
||||
// goes to the end
|
||||
REQUIRE(price_level.orders().at(99).quantity.value() == 61);
|
||||
price_level.copy_values_to(out_ords);
|
||||
REQUIRE(out_ords.at(99).quantity.value() == 61);
|
||||
REQUIRE(price_level.n_orders() == 100);
|
||||
REQUIRE(price_level.quantity().value() == 5051);
|
||||
}
|
||||
@@ -462,7 +472,7 @@ TEST_CASE("fifo_matching_engine_book", "[matcher]") {
|
||||
|
||||
TEST_CASE("perf", "[matcher]") {
|
||||
RandomGenerator rnd_gen;
|
||||
const size_t n_levels = 50;
|
||||
const size_t n_levels = 500;
|
||||
const double tick_size = 0.01;
|
||||
const double top_bid = 100.0;
|
||||
const MDP3SecurityID sec_id = MDP3SecurityID(1);
|
||||
@@ -481,7 +491,7 @@ TEST_CASE("perf", "[matcher]") {
|
||||
else
|
||||
price = (top_bid+tick_size) + (p_lev*tick_size);
|
||||
ord.price = md::IntPrice_from_double<7>(price);
|
||||
ord.quantity = MDP3Quantity(rnd_gen.random_int(1,20));
|
||||
ord.quantity = MDP3Quantity(rnd_gen.random_int(5,20));
|
||||
};
|
||||
auto add_n_orders = [&](std::vector<OrderChange>& out_v,
|
||||
size_t n_orders,
|
||||
@@ -492,25 +502,22 @@ TEST_CASE("perf", "[matcher]") {
|
||||
ord_change.change = OrderChange::ChangeType::added;
|
||||
fill_ord(ord_change.order, MDP3OrderKey(start_k++));
|
||||
out_v.push_back(ord_change);
|
||||
}
|
||||
};
|
||||
auto cxl_n_orders = [](FifoSecurityMatcher& sec_m,
|
||||
size_t start_key, size_t n_ords){
|
||||
OrderChange ord_change;
|
||||
ord_change.change = OrderChange::ChangeType::removed;
|
||||
auto k = start_key;
|
||||
for(size_t i=0;i<n_ords;i++){
|
||||
ord_change.order.key = MDP3OrderKey(k++);
|
||||
sec_m.on_order_change(ord_change);
|
||||
}
|
||||
};
|
||||
SECTION("add orders") {
|
||||
size_t n_orders = 10000, inc = 10000, n_runs = 10;
|
||||
|
||||
//size_t n_orders = 10000, inc = 10000, n_runs = 1;
|
||||
size_t n_orders = 1000000, inc = 10000, n_runs = 3;
|
||||
SECTION("add orders") {
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
FifoSecurityMatcher sec_m(MDP3SecurityID(100));
|
||||
FifoSecurityMatcher sec_m(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs{
|
||||
MDP3SecurityID(100),
|
||||
0,
|
||||
false
|
||||
});
|
||||
std::vector<OrderChange> aux;
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
auto t0 = TimeUtil::GetCurrentTime();
|
||||
for (const auto& oc : aux)
|
||||
sec_m.on_order_change(oc);
|
||||
@@ -518,15 +525,134 @@ TEST_CASE("perf", "[matcher]") {
|
||||
auto nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
TimeUtil::TimestampToNanoseconds(t0);
|
||||
std::cout << "Adding " << aux.size() << " took " << 1.0*nsecs/1000000 << " msecs" << std::endl;
|
||||
//std::cout << sec_m.print_book() << std::endl;
|
||||
// deleting the orders now
|
||||
t0 = TimeUtil::GetCurrentTime();
|
||||
cxl_n_orders(sec_m,0,n_orders);
|
||||
t1 = TimeUtil::GetCurrentTime();
|
||||
nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
size_t comp_n_orders =
|
||||
sec_m.total_n_orders(api::Side::SIDE_BUY) +
|
||||
sec_m.total_n_orders(api::Side::SIDE_SELL);
|
||||
REQUIRE(comp_n_orders == n_orders);
|
||||
n_orders += inc;
|
||||
}
|
||||
}
|
||||
SECTION("cxl_orders") {
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
FifoSecurityMatcher sec_m(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs{
|
||||
MDP3SecurityID(100),
|
||||
0,
|
||||
false
|
||||
});
|
||||
std::vector<OrderChange> aux;
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
for (const auto& oc : aux)
|
||||
sec_m.on_order_change(oc);
|
||||
auto t0 = TimeUtil::GetCurrentTime();
|
||||
for (auto& oc : aux){
|
||||
oc.change = OrderChange::ChangeType::removed;
|
||||
sec_m.on_order_change(oc);
|
||||
}
|
||||
auto t1 = TimeUtil::GetCurrentTime();
|
||||
auto nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
TimeUtil::TimestampToNanoseconds(t0);
|
||||
std::cout << "Removing " << n_orders << " took " << 1.0*nsecs/1000000 << " msecs" << std::endl;
|
||||
n_orders += inc;
|
||||
size_t comp_n_orders =
|
||||
sec_m.total_n_orders(api::Side::SIDE_BUY) +
|
||||
sec_m.total_n_orders(api::Side::SIDE_SELL);
|
||||
REQUIRE(comp_n_orders == 0);
|
||||
}
|
||||
}
|
||||
SECTION("update_dec_orders") {
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
FifoSecurityMatcher sec_m(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs{
|
||||
MDP3SecurityID(100),
|
||||
0,
|
||||
false
|
||||
});
|
||||
std::vector<OrderChange> aux;
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
for (const auto& oc : aux)
|
||||
sec_m.on_order_change(oc);
|
||||
auto t0 = TimeUtil::GetCurrentTime();
|
||||
for (auto& oc : aux){
|
||||
oc.order.quantity = MDP3Quantity(oc.order.quantity.value()-1);
|
||||
oc.change = OrderChange::ChangeType::updated;
|
||||
sec_m.on_order_change(oc);
|
||||
}
|
||||
auto t1 = TimeUtil::GetCurrentTime();
|
||||
auto nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
TimeUtil::TimestampToNanoseconds(t0);
|
||||
std::cout << "Updaing (qty dec) " << n_orders << " took " << 1.0*nsecs/1000000 << " msecs" << std::endl;
|
||||
size_t comp_n_orders =
|
||||
sec_m.total_n_orders(api::Side::SIDE_BUY) +
|
||||
sec_m.total_n_orders(api::Side::SIDE_SELL);
|
||||
REQUIRE(comp_n_orders == n_orders);
|
||||
n_orders += inc;
|
||||
}
|
||||
}
|
||||
SECTION("update_inc_orders") {
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
FifoSecurityMatcher sec_m(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs{
|
||||
MDP3SecurityID(100),
|
||||
0,
|
||||
false
|
||||
});
|
||||
std::vector<OrderChange> aux;
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
for (const auto& oc : aux)
|
||||
sec_m.on_order_change(oc);
|
||||
auto t0 = TimeUtil::GetCurrentTime();
|
||||
for (auto& oc : aux){
|
||||
oc.order.quantity = MDP3Quantity(oc.order.quantity.value()+1);
|
||||
oc.change = OrderChange::ChangeType::updated;
|
||||
sec_m.on_order_change(oc);
|
||||
}
|
||||
auto t1 = TimeUtil::GetCurrentTime();
|
||||
auto nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
TimeUtil::TimestampToNanoseconds(t0);
|
||||
std::cout << "Updaing (qty inc) " << n_orders << " took " << 1.0*nsecs/1000000 << " msecs" << std::endl;
|
||||
size_t comp_n_orders =
|
||||
sec_m.total_n_orders(api::Side::SIDE_BUY) +
|
||||
sec_m.total_n_orders(api::Side::SIDE_SELL);
|
||||
REQUIRE(comp_n_orders == n_orders);
|
||||
n_orders += inc;
|
||||
}
|
||||
}
|
||||
SECTION("order_price_change_no_cross") {
|
||||
for(size_t i=0;i<n_runs;i++){
|
||||
FifoSecurityMatcher sec_m(
|
||||
FifoSecurityMatcher::FifoSecurityMatcherArgs{
|
||||
MDP3SecurityID(100),
|
||||
0,
|
||||
false
|
||||
});
|
||||
std::vector<OrderChange> aux;
|
||||
// takes 22 secs to add 100,000 orders straght up
|
||||
add_n_orders(aux, n_orders, 0);
|
||||
for (const auto& oc : aux)
|
||||
sec_m.on_order_change(oc);
|
||||
auto t0 = TimeUtil::GetCurrentTime();
|
||||
for (auto& oc : aux){
|
||||
oc.change = OrderChange::ChangeType::updated;
|
||||
double inc = tick_size;
|
||||
if(oc.order.side == api::Side::SIDE_BUY)
|
||||
inc = -1*inc;
|
||||
oc.order.price =
|
||||
md::IntPrice_from_double<7>(oc.order.price.price() + inc),
|
||||
sec_m.on_order_change(oc);
|
||||
}
|
||||
auto t1 = TimeUtil::GetCurrentTime();
|
||||
auto nsecs = TimeUtil::TimestampToNanoseconds(t1) -
|
||||
TimeUtil::TimestampToNanoseconds(t0);
|
||||
std::cout << "Updating (price change no cross) " << n_orders << " took " << 1.0*nsecs/1000000 << " msecs" << std::endl;
|
||||
size_t comp_n_orders =
|
||||
sec_m.total_n_orders(api::Side::SIDE_BUY) +
|
||||
sec_m.total_n_orders(api::Side::SIDE_SELL);
|
||||
REQUIRE(comp_n_orders == n_orders);
|
||||
n_orders += inc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user