Moved the matcher tests into its own file

This commit is contained in:
Taha Afzal committed 2020-09-29 11:09:42 -04:00
1 parent f83d57fc0c
commit a5a1f87528
3 files changed
+448 -405

No files matched your search

@@ -12,6 +12,19 @@ namespace bts::spartan::mdp3 {
class FifoSecurityMatcher : public SecurityMatcher {
public:
struct FifoSecurityMatcherArgs{
const size_t event_id = 0;
const bool allow_ops_on_deleted_orders = true;
Orders const* orders = nullptr;
FifoSecurityMatcherArgs(size_t eid,
bool allow_ops_on_deleted_orders,
Orders const* orders)
:
event_id(eid),
allow_ops_on_deleted_orders(allow_ops_on_deleted_orders),
orders(orders)
{}
};
private:
// ask prices are sorted lowest to highets and and bid prices
// are sorted lowest to highest
@@ -0,0 +1,435 @@
#include <catch/catch.hpp>
#include "server/sparkd/cmds/spartan/md/mdp3/parsing/MDP3Parser.h"
#include "lib/utils/TempFile.h"
#include "lib/utils/overloaded.h"
#include "lib/utils/type_name.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.h"
#include "server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.h"
#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h"
#include "server/sparkd/cmds/spartan/md/mdp3/feed_control/AB_Arbitrageur.h"
#include "server/sparkd/cmds/spartan/md/mdp3/feed_control/IFeedController.h"
#include "server/sparkd/cmds/spartan/md/mdp3/handler/ChannelHandler.h"
#include "server/sparkd/cmds/spartan/md/mdp3/handler/IChannelObserver.h"
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/BookSnapshot.h"
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.h"
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/IncrementalQueue.h"
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.h"
#include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h"
#include "server/sparkd/cmds/spartan/util/PCapReader.h"
#include <fmt/format.h>
#include <boost/date_time/posix_time/conversion.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/process.hpp>
namespace bts::spartan::mdp3::tests {
MDP3Order create_order(uint64_t key, int32_t sec, api::Side side,
uint64_t priority, double price, uint32_t qty,
FifoPriceLevelMatcher::IB ib = {false, MDP3Quantity(0),
MDP3Quantity(0)}) {
auto rv = MDP3Order{MDP3OrderKey(key),
MDP3SecurityID(sec),
side,
MDP3OrderPriority(priority),
md::IntPrice_from_double<7>(price),
MDP3Quantity(qty),
ib.is_ib ? std::make_optional<MDP3Quantity>(ib.total_qty)
: std::nullopt};
// if(ib.is_ib)
// rv.iceberg_quantity = std::make_optional<MDP3Quantity>(ib.total_qty);
return rv;
}
MDP3Order create_empty_order(uint64_t key) {
return create_order(key,0,api::Side::SIDE_BUY,0,0.0,0);
}
TEST_CASE("fifo_matching_engine_single_level", "[matcher]") {
auto sec_key = 10;
double price = 100.0;
api::Side side = api::Side::SIDE_BUY;
Trace trace;
FifoPriceLevelMatcher price_level(md::IntPrice_from_double<7>(price), side, trace);
auto create_book = [&](int n_orders) -> void {
for (int i = 1; i <= n_orders; i++) {
auto ord_key = static_cast<uint64_t>(i);
auto o = create_order(ord_key, sec_key, side, ord_key, price,
static_cast<uint32_t>(i));
price_level.on_new_same_side_order(o);
}
};
SECTION("add") {
// add 100 orders
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
}
SECTION("remove") {
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
// remove last 10 orders, 91-100
for (int i = 91; i <= 100; i++) {
auto ord_key = static_cast<uint64_t>(i);
price_level.on_order_remove(MDP3OrderKey(ord_key));
}
REQUIRE(price_level.n_orders() == 90);
REQUIRE(price_level.quantity().value() == 4095);
}
SECTION("sweep") {
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));
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
}
SECTION("full_fill") {
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);
}
SECTION("partial_fill") {
create_book(100);
// take out the firdt 20 completely
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(193));
REQUIRE(price_level.n_orders() == 81);
REQUIRE(price_level.quantity().value() == 4857);
}
SECTION("qty_decrease") {
create_book(100);
auto &ord_59 = price_level.orders().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);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5010);
}
SECTION("qty_increase") {
create_book(100);
auto &ord_59 = price_level.orders().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);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5051);
}
SECTION("iceberg_simple") {
auto k = 0;
auto price = 100.0;
auto o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, 100.0, 1);
price_level.on_new_same_side_order(o);
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 2);
price_level.on_new_same_side_order(o);
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 3);
price_level.on_new_same_side_order(o);
// iceberg, show qty=2, total_qty=100
o = create_order(
k++, sec_key, api::Side::SIDE_BUY, 0, price, 2,
FifoPriceLevelMatcher::IB{true, MDP3Quantity(2), MDP3Quantity(100)});
price_level.on_new_same_side_order(o);
// non ib, to get the visible qty to 15
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 7);
price_level.on_new_same_side_order(o);
REQUIRE(price_level.quantity().value() == 15);
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));
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));
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));
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));
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
REQUIRE(price_level.total_quantity().value() == 0);
}
}
TEST_CASE("fifo_matching_engine_book", "[matcher]") {
auto verify_book_level = [](const BookLevel &bl, double price, uint32_t qty,
uint32_t n_orders) {
REQUIRE(bl.price == md::IntPrice_from_double<7>(price));
REQUIRE(bl.n_orders == n_orders);
REQUIRE(bl.quantity.value() == qty);
REQUIRE(bl.n_orders == n_orders);
};
auto sec_key = 10;
FifoSecurityMatcher sec_me;
auto order_key = 0;
auto priority = 0;
auto add_to_book = [&](uint32_t n_orders, api::Side side,
double price) -> void {
for (uint32_t i = 1; i <= n_orders; i++) {
auto o =
create_order(order_key++, sec_key, side, priority++, price, i);
sec_me.on_order_change(OrderChange{MDP3Order{},o,OrderChange::added});
}
};
SECTION("add") {
// bids
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// verify bids
{
Book bid_bk = sec_me.book(api::Side::SIDE_BUY);
double exp_price = 15.09;
for (int i = 0; i < n_levels; i++) {
auto exp_n_orders = 100 - i * 10;
auto exp_qty =
(static_cast<double>(exp_n_orders) / 2) * (1 + exp_n_orders);
verify_book_level(bid_bk.at(i), exp_price, exp_qty, exp_n_orders);
exp_price -= 0.01;
}
}
// verify asks
{
Book ask_bk = sec_me.book(api::Side::SIDE_SELL);
double exp_price = 15.10;
for (int i = 0; i < n_levels; i++) {
auto exp_n_orders = (i + 1) * 10;
auto exp_qty =
(static_cast<double>(exp_n_orders) / 2) * (1 + exp_n_orders);
verify_book_level(ask_bk.at(i), exp_price, exp_qty, exp_n_orders);
exp_price += 0.01;
}
}
}
SECTION("remove") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// remove one order at a time from the second level
// 90 orders starting with id 360
auto b = 360;
auto init_n_orders = 90;
auto init_qty = 4095;
auto remvd = 0;
for (auto i = 0; i < 90; i++) {
auto id = MDP3OrderKey(i + b);
sec_me.on_order_change(OrderChange{
MDP3Order{},
create_empty_order(id.value()),
OrderChange::removed
});
auto qty = i + 1;
remvd += qty;
auto now_qty = init_qty - remvd;
auto now_n_orders = init_n_orders - (i + 1);
Book bk = sec_me.book(api::Side::SIDE_BUY);
if (now_qty == 0)
// whole level got removed
// now level 1 should be 15.07
verify_book_level(bk.at(1), 15.07, 3240, 80);
else
verify_book_level(bk.at(1), 15.08, now_qty, now_n_orders);
}
Book bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.07, 3240, 80);
}
SECTION("modify") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// modify qty of all orders to 1
auto b = 360;
for (auto i = 0; i < 90; i++) {
auto id = i + b;
auto ord =
create_order(id, sec_key, api::Side::SIDE_BUY, id, 15.08, 1);
sec_me.on_order_change(OrderChange{
create_empty_order(ord.key.value()),
ord,
OrderChange::updated
});
}
Book bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.08, 90, 90);
// now change all these prices to move to a diff level
for (auto i = 0; i < 90; i++) {
auto id = i + b;
auto ord =
create_order(id, sec_key, api::Side::SIDE_BUY, id, 15.09, 1);
sec_me.on_order_change(OrderChange{
create_empty_order(ord.key.value()),
ord,
OrderChange::updated
});
}
// 15.08 is gone
bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.07, 3240, 80);
verify_book_level(bk.at(0), 15.09, 5140, 190);
}
SECTION("crosses") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// send a new sell at 15.09 to take the first 10 orders
auto ord =
create_order(10000, sec_key, api::Side::SIDE_SELL, 10000, 15.09, 55);
sec_me.on_order_change(OrderChange{MDP3Order{},ord,OrderChange::added});
auto bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(0), 15.09, 4995, 90);
// now completely sweep 2 complete levels and make the rest a working
// order
ord = create_order(10001, sec_key, api::Side::SIDE_SELL, 10001, 15.08,
10000);
sec_me.on_order_change(OrderChange{MDP3Order{},ord,OrderChange::added});
bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(0), 15.07, 3240, 80);
auto bk_ask = sec_me.book(api::Side::SIDE_SELL);
verify_book_level(bk_ask.at(0), 15.08, 910, 1);
}
SECTION("IB_cross") {
// create 10 level book and cross the first 3 levels completely.
// verify the resting IB is correct
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
auto bid_bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bid_bk.at(0), 15.09, 5050, 100);
// send a new sell at 15.07 to take the first 3 levels
auto cross_ib =
create_order(10000, sec_key, api::Side::SIDE_SELL, 10000, 15.07, 13,
FifoPriceLevelMatcher::IB{true, MDP3Quantity(13),
MDP3Quantity(12500)});
sec_me.on_order_change(OrderChange{MDP3Order{},cross_ib,OrderChange::added});
bid_bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bid_bk.at(0), 15.06, 2485, 70);
auto ask_bk = sec_me.book(api::Side::SIDE_SELL);
verify_book_level(ask_bk.at(0), 15.07, 13, 1);
}
}
} // namespace bts::spartan::mdp3::tests
@@ -260,409 +260,4 @@ TEST_CASE("spartan_md", "[spartan_md]") {
}
}
MDP3Order create_order(uint64_t key, int32_t sec, api::Side side,
uint64_t priority, double price, uint32_t qty,
FifoPriceLevelMatcher::IB ib = {false, MDP3Quantity(0),
MDP3Quantity(0)}) {
auto rv = MDP3Order{MDP3OrderKey(key),
MDP3SecurityID(sec),
side,
MDP3OrderPriority(priority),
md::IntPrice_from_double<7>(price),
MDP3Quantity(qty),
ib.is_ib ? std::make_optional<MDP3Quantity>(ib.total_qty)
: std::nullopt};
// if(ib.is_ib)
// rv.iceberg_quantity = std::make_optional<MDP3Quantity>(ib.total_qty);
return rv;
}
MDP3Order create_empty_order(uint64_t key) {
return create_order(key,0,api::Side::SIDE_BUY,0,0.0,0);
}
TEST_CASE("fifo_matching_engine_single_level", "[spartan_md]") {
auto sec_key = 10;
double price = 100.0;
api::Side side = api::Side::SIDE_BUY;
Trace trace;
FifoPriceLevelMatcher price_level(md::IntPrice_from_double<7>(price), side, trace);
auto create_book = [&](int n_orders) -> void {
for (int i = 1; i <= n_orders; i++) {
auto ord_key = static_cast<uint64_t>(i);
auto o = create_order(ord_key, sec_key, side, ord_key, price,
static_cast<uint32_t>(i));
price_level.on_new_same_side_order(o);
}
};
SECTION("add") {
// add 100 orders
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
}
SECTION("remove") {
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
// remove last 10 orders, 91-100
for (int i = 91; i <= 100; i++) {
auto ord_key = static_cast<uint64_t>(i);
price_level.on_order_remove(MDP3OrderKey(ord_key));
}
REQUIRE(price_level.n_orders() == 90);
REQUIRE(price_level.quantity().value() == 4095);
}
SECTION("sweep") {
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));
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
}
SECTION("full_fill") {
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);
}
SECTION("partial_fill") {
create_book(100);
// take out the firdt 20 completely
price_level.on_new_opp_order(MDP3OrderKey(100001), MDP3Quantity(193));
REQUIRE(price_level.n_orders() == 81);
REQUIRE(price_level.quantity().value() == 4857);
}
SECTION("qty_decrease") {
create_book(100);
auto &ord_59 = price_level.orders().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);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5010);
}
SECTION("qty_increase") {
create_book(100);
auto &ord_59 = price_level.orders().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);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5051);
}
SECTION("iceberg_simple") {
auto k = 0;
auto price = 100.0;
auto o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, 100.0, 1);
price_level.on_new_same_side_order(o);
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 2);
price_level.on_new_same_side_order(o);
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 3);
price_level.on_new_same_side_order(o);
// iceberg, show qty=2, total_qty=100
o = create_order(
k++, sec_key, api::Side::SIDE_BUY, 0, price, 2,
FifoPriceLevelMatcher::IB{true, MDP3Quantity(2), MDP3Quantity(100)});
price_level.on_new_same_side_order(o);
// non ib, to get the visible qty to 15
o = create_order(k++, sec_key, api::Side::SIDE_BUY, 0, price, 7);
price_level.on_new_same_side_order(o);
REQUIRE(price_level.quantity().value() == 15);
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));
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));
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));
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));
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
REQUIRE(price_level.total_quantity().value() == 0);
}
}
TEST_CASE("fifo_matching_engine_book", "[spartan_md]") {
auto verify_book_level = [](const BookLevel &bl, double price, uint32_t qty,
uint32_t n_orders) {
REQUIRE(bl.price == md::IntPrice_from_double<7>(price));
REQUIRE(bl.n_orders == n_orders);
REQUIRE(bl.quantity.value() == qty);
REQUIRE(bl.n_orders == n_orders);
};
auto sec_key = 10;
FifoSecurityMatcher sec_me;
auto order_key = 0;
auto priority = 0;
auto add_to_book = [&](uint32_t n_orders, api::Side side,
double price) -> void {
for (uint32_t i = 1; i <= n_orders; i++) {
auto o =
create_order(order_key++, sec_key, side, priority++, price, i);
sec_me.on_order_change(OrderChange{MDP3Order{},o,OrderChange::added});
}
};
SECTION("add") {
// bids
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// verify bids
{
Book bid_bk = sec_me.book(api::Side::SIDE_BUY);
double exp_price = 15.09;
for (int i = 0; i < n_levels; i++) {
auto exp_n_orders = 100 - i * 10;
auto exp_qty =
(static_cast<double>(exp_n_orders) / 2) * (1 + exp_n_orders);
verify_book_level(bid_bk.at(i), exp_price, exp_qty, exp_n_orders);
exp_price -= 0.01;
}
}
// verify asks
{
Book ask_bk = sec_me.book(api::Side::SIDE_SELL);
double exp_price = 15.10;
for (int i = 0; i < n_levels; i++) {
auto exp_n_orders = (i + 1) * 10;
auto exp_qty =
(static_cast<double>(exp_n_orders) / 2) * (1 + exp_n_orders);
verify_book_level(ask_bk.at(i), exp_price, exp_qty, exp_n_orders);
exp_price += 0.01;
}
}
}
SECTION("remove") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// remove one order at a time from the second level
// 90 orders starting with id 360
auto b = 360;
auto init_n_orders = 90;
auto init_qty = 4095;
auto remvd = 0;
for (auto i = 0; i < 90; i++) {
auto id = MDP3OrderKey(i + b);
sec_me.on_order_change(OrderChange{
MDP3Order{},
create_empty_order(id.value()),
OrderChange::removed
});
auto qty = i + 1;
remvd += qty;
auto now_qty = init_qty - remvd;
auto now_n_orders = init_n_orders - (i + 1);
Book bk = sec_me.book(api::Side::SIDE_BUY);
if (now_qty == 0)
// whole level got removed
// now level 1 should be 15.07
verify_book_level(bk.at(1), 15.07, 3240, 80);
else
verify_book_level(bk.at(1), 15.08, now_qty, now_n_orders);
}
Book bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.07, 3240, 80);
}
SECTION("modify") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// modify qty of all orders to 1
auto b = 360;
for (auto i = 0; i < 90; i++) {
auto id = i + b;
auto ord =
create_order(id, sec_key, api::Side::SIDE_BUY, id, 15.08, 1);
sec_me.on_order_change(OrderChange{
create_empty_order(ord.key.value()),
ord,
OrderChange::updated
});
}
Book bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.08, 90, 90);
// now change all these prices to move to a diff level
for (auto i = 0; i < 90; i++) {
auto id = i + b;
auto ord =
create_order(id, sec_key, api::Side::SIDE_BUY, id, 15.09, 1);
sec_me.on_order_change(OrderChange{
create_empty_order(ord.key.value()),
ord,
OrderChange::updated
});
}
// 15.08 is gone
bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(1), 15.07, 3240, 80);
verify_book_level(bk.at(0), 15.09, 5140, 190);
}
SECTION("crosses") {
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
// send a new sell at 15.09 to take the first 10 orders
auto ord =
create_order(10000, sec_key, api::Side::SIDE_SELL, 10000, 15.09, 55);
sec_me.on_order_change(OrderChange{MDP3Order{},ord,OrderChange::added});
auto bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(0), 15.09, 4995, 90);
// now completely sweep 2 complete levels and make the rest a working
// order
ord = create_order(10001, sec_key, api::Side::SIDE_SELL, 10001, 15.08,
10000);
sec_me.on_order_change(OrderChange{MDP3Order{},ord,OrderChange::added});
bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bk.at(0), 15.07, 3240, 80);
auto bk_ask = sec_me.book(api::Side::SIDE_SELL);
verify_book_level(bk_ask.at(0), 15.08, 910, 1);
}
SECTION("IB_cross") {
// create 10 level book and cross the first 3 levels completely.
// verify the resting IB is correct
auto n_levels = 10;
// bids
for (struct {
int i = 0;
double p = 15;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_BUY, lp.p);
// asks
for (struct {
int i = 0;
double p = 15.10;
void inc() {
i++;
p += 0.01;
}
} lp;
lp.i < n_levels; lp.inc())
add_to_book((lp.i + 1) * 10, api::Side::SIDE_SELL, lp.p);
auto bid_bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bid_bk.at(0), 15.09, 5050, 100);
// send a new sell at 15.07 to take the first 3 levels
auto cross_ib =
create_order(10000, sec_key, api::Side::SIDE_SELL, 10000, 15.07, 13,
FifoPriceLevelMatcher::IB{true, MDP3Quantity(13),
MDP3Quantity(12500)});
sec_me.on_order_change(OrderChange{MDP3Order{},cross_ib,OrderChange::added});
bid_bk = sec_me.book(api::Side::SIDE_BUY);
verify_book_level(bid_bk.at(0), 15.06, 2485, 70);
auto ask_bk = sec_me.book(api::Side::SIDE_SELL);
verify_book_level(ask_bk.at(0), 15.07, 13, 1);
}
}
} // namespace bts::spartan::mdp3::tests