Function for John to be used for implied matching

This commit is contained in:
Taha Afzal committed 2020-10-11 18:31:27 -04:00
1 parent 85c3ba0fa3
commit 2400f5b91c
6 files changed
+71 -41

No files matched your search

@@ -66,6 +66,7 @@ void FifoPriceLevelMatcher::_reload_icebergs() {
std::tuple<MDP3Quantity, MDP3Quantity>
FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key,
MDP3Quantity cross_ord_qty,
bool add_agg_side_to_match_effect,
std::vector<MatchEffect>* res) {
// go through all the orders and see which ones get filled
// (orders are sorted by priority)
@@ -86,21 +87,23 @@ FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key,
MDP3Quantity(filled),
_price
};
MatcherUtils::validate_outgoing_update(passive_upd);
MatcherUtils::validate_outgoing_update(passive_upd);
res->push_back(passive_upd);
OrderUpdate agg_upd =
{
cross_ord_key,
cross_ord_qty.value() > 0 ?
OrderUpdate::Status::PARTIALLY_FILLED :
OrderUpdate::Status::FILLED,
_sec,
_opp_side(_side),
MDP3Quantity(filled),
_price
};
MatcherUtils::validate_outgoing_update(agg_upd);
res->push_back(agg_upd);
if(add_agg_side_to_match_effect){
OrderUpdate agg_upd =
{
cross_ord_key,
cross_ord_qty.value() > 0 ?
OrderUpdate::Status::PARTIALLY_FILLED :
OrderUpdate::Status::FILLED,
_sec,
_opp_side(_side),
MDP3Quantity(filled),
_price
};
MatcherUtils::validate_outgoing_update(agg_upd);
res->push_back(agg_upd);
}
}
if(_trace.log(o.key) || _trace.log(cross_ord_key))
LOG(OrderMan, Info,
@@ -214,12 +217,15 @@ void FifoPriceLevelMatcher::on_new_same_side_order(const MDP3Order &new_ord) {
std::tuple<MDP3Quantity,MDP3Quantity> FifoPriceLevelMatcher::on_new_opp_order(
MDP3OrderKey opp_ord_key,
MDP3Quantity cross_qty,
bool add_agg_side_to_match_effect,
std::vector<MatchEffect>* res) {
auto rv = _one_pass_cross_order(opp_ord_key, cross_qty, res);
auto rv = _one_pass_cross_order(opp_ord_key, cross_qty,
add_agg_side_to_match_effect, res);
// continue until either the crossing order is consumed
// or the price level is consumed
while (std::get<0>(rv).value() > 0 && std::get<1>(rv).value() > 0)
rv = _one_pass_cross_order(opp_ord_key, std::get<1>(rv), res);
rv = _one_pass_cross_order(opp_ord_key, std::get<1>(rv),
add_agg_side_to_match_effect, res);
return rv;
}
@@ -39,6 +39,7 @@ class FifoPriceLevelMatcher {
std::tuple<MDP3Quantity, MDP3Quantity>
_one_pass_cross_order(MDP3OrderKey cross_ord_key,
MDP3Quantity cross_ord_qty,
bool add_agg_side_to_match_effect,
std::vector<MatchEffect>* res);
void _log_order(const MatcherOrder& ord,
const std::string& msg);
@@ -99,6 +100,7 @@ class FifoPriceLevelMatcher {
std::tuple<MDP3Quantity,MDP3Quantity> on_new_opp_order(
MDP3OrderKey opp_ord_key,
MDP3Quantity opp_ord_qty,
bool add_agg_side_to_match_effect,
std::vector<MatchEffect>* = nullptr);
void on_order_update(const MDP3Order& upd_ord,
MatcherOrder* ord_hint = nullptr);
@@ -48,6 +48,13 @@ FifoSecurityMatcher::_find_order(MDP3OrderKey key) {
nullptr, nullptr);
}
FifoPriceLevelMatcher* FifoSecurityMatcher::_top(api::Side side){
for(auto& plevel : _book(side))
if(plevel.populated())
return &plevel;
return nullptr;
}
// this is expensive
void FifoSecurityMatcher::_cleanup() {
// remove orders from the price levels
@@ -74,7 +81,8 @@ void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord,
uint32_t new_ord_rem = total_quantity(ord).value();
for (FifoPriceLevelMatcher &pl : opp_book) {
if (_is_cross(ord, pl)){
auto t = pl.on_new_opp_order(ord.key, MDP3Quantity(new_ord_rem),res);
auto t = pl.on_new_opp_order(ord.key, MDP3Quantity(new_ord_rem),
true, res);
new_ord_rem = std::get<1>(t).value();
}
else
@@ -285,8 +293,19 @@ void FifoSecurityMatcher::on_order_change(OrderChange const &o,
}
}
MDP3Quantity FifoSecurityMatcher::fill(MDP3OrderKey, api::Side, md::IntPrice, MDP3Quantity, std::vector<MatchEffect>*, bool) {
// todo
MDP3Quantity FifoSecurityMatcher::fill(MDP3OrderKey aggr_key,
api::Side side,
md::IntPrice agg_price,
MDP3Quantity qty,
std::vector<MatchEffect>* res,
bool add_aggr_fill){
FifoPriceLevelMatcher* top = _top(_opp_side(side));
if(top != nullptr){
bts_assert(top->price()==agg_price);
auto t = top->on_new_opp_order(aggr_key,qty,add_aggr_fill,res);
return std::get<1>(t);
}
return MDP3Quantity(0);
}
std::optional<std::string>
@@ -28,7 +28,7 @@ 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 lowest to highest
// are sorted highest to lowest
boost::container::flat_set<FifoPriceLevelMatcher> _bid_book, _ask_book;
FifoPriceLevelMatcher _search_tok;
const MDP3SecurityID _sec;
@@ -55,6 +55,7 @@ public:
// for price level in the book (flat_set)
std::tuple<FifoPriceLevelMatcher *, MatcherOrder *>
_find_order(MDP3OrderKey key);
FifoPriceLevelMatcher* _top(api::Side side);
void _cleanup();
// order interaction
void _on_new_order(const MDP3Order&, std::vector<MatchEffect>* = nullptr);
@@ -74,20 +75,22 @@ public:
////////////////////////////////////////////////////////////////////////////
Book book(api::Side) const 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
// fills must be generated independently (there are several passive legs,
// but only one aggressor security). The 'add_aggr_fill' param would be false in
// this case, and the 'qty' param would be limited by either a thinner market in
// another leg, by the volume in this market's top price level, or by the aggressor
// order's quantity itself.
// Particular quantities of passive fills need to be generated when
// an aggressor order is filled against an implied market, while the aggressor
// fills must be generated independently (there are several passive legs,
// but only one aggressor security). The 'add_aggr_fill' param would be false in
// this case, and the 'qty' param would be limited by either a thinner market in
// another leg, by the volume in this market's top price level, or by the aggressor
// order's quantity itself.
//
// In the case of direct market fills, both aggressor and passive fills
// should be generated (of course, so 'add_aggr_fill' would be true).
// However, the fill qty may also be constricted to a certain number of
// price levels. This is because the market with the best price doesn't
// necessarily have the 2nd best price as well.
MDP3Quantity fill(MDP3OrderKey aggr_key, api::Side side, md::IntPrice price, MDP3Quantity qty, std::vector<MatchEffect>* res, bool add_aggr_fill);
// In the case of direct market fills, both aggressor and passive fills
// should be generated (of course, so 'add_aggr_fill' would be true).
// However, the fill qty may also be constricted to a certain number of
// price levels. This is because the market with the best price doesn't
// necessarily have the 2nd best price as well.
MDP3Quantity fill(MDP3OrderKey aggr_key, api::Side side, md::IntPrice price,
MDP3Quantity qty,
std::vector<MatchEffect>* res, bool add_aggr_fill);
std::tuple<md::IntPrice, MDP3Quantity> book_level(
api::Side side, size_t idx) const override;
void on_order_change(OrderChange const &,
@@ -11,7 +11,7 @@ struct Trace{
public:
const std::map<MDP3OrderKey,bool> traced_orders =
{
{MDP3OrderKey(765159167395),true}
{MDP3OrderKey(765164629193),true}
};
const std::map<size_t,bool> traced_events = {};
const std::map<size_t,bool> ignored_events = {};
@@ -91,7 +91,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(5100));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(5100),false);
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
}
@@ -99,7 +99,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
std::vector<MatcherOrder> out_ords;
create_book(100);
// take out the first 20 completely
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(210));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(210),false);
REQUIRE(price_level.n_orders() == 80);
REQUIRE(price_level.quantity().value() == 4840);
price_level.copy_values_to(out_ords);
@@ -108,7 +108,7 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
SECTION("partial_fill") {
create_book(100);
// take out the firdt 20 completely
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(193));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(193),false);
REQUIRE(price_level.n_orders() == 81);
REQUIRE(price_level.quantity().value() == 4857);
}
@@ -164,25 +164,25 @@ TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
REQUIRE(price_level.total_quantity().value() == 113);
// cross with a 10 lot, this should result in
// {id=4,qty=5},{id=3,qty=2,total_qty=98}
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(10));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(10),false);
auto [n_ords, vis_qty] = price_level.n_orders_qty(true);
REQUIRE(n_ords == 2);
REQUIRE(vis_qty == 7);
auto total_qty = price_level.total_quantity();
REQUIRE(total_qty.value() == 103);
// another 5 lot should only leave the IB {id=3,qty=2,total_qty=98}
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(5));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(5),false);
REQUIRE(price_level.n_orders() == 1);
REQUIRE(price_level.quantity().value() == 2);
REQUIRE(price_level.total_quantity().value() == 98);
// cross the remaining 98 IB
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(49));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(49),false);
REQUIRE(price_level.n_orders() == 1);
REQUIRE(price_level.quantity().value() == 1);
REQUIRE(price_level.total_quantity().value() == 49);
// 1 lots
for (auto i = 0; i < 49; i++)
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(1));
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(1),false);
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
REQUIRE(price_level.total_quantity().value() == 0);