Merge branch 'spartan_tas' of bitbucket.org:bluetradesys/spark into spartan_tas

This commit is contained in:
Pedro Pinto committed 2020-08-24 08:37:41 -04:00
commit a2ea2239da
2 files changed
+99 -12

No files matched your search

@@ -125,7 +125,7 @@ void FifoSecurityMatcher::on_new_order(const MDP3Order& ord){
}
MDP3Order ord_corrected = ord;
ord_corrected.quantity = MDP3Quantity(rem);
price_level->on_new_same_side_order(ord);
price_level->on_new_same_side_order(ord_corrected);
}
}
@@ -377,6 +377,7 @@ TEST_CASE("fifo_matching_engine_book", "[spartan_md]") {
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);
};
@@ -400,16 +401,14 @@ TEST_CASE("fifo_matching_engine_book", "[spartan_md]") {
SECTION("add") {
// bids
auto n_levels = 10;
double price = 15.00, inc = 0.01;
for (int i=0;i<n_levels;i++){
add_to_book((i+1)*10,api::Side::SIDE_BUY, price);
price += inc;
}
// 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 (int i=0;i<n_levels;i++){
add_to_book((i+1)*10,api::Side::SIDE_SELL, price);
price += inc;
}
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);
@@ -418,7 +417,7 @@ TEST_CASE("fifo_matching_engine_book", "[spartan_md]") {
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 -= inc;
exp_price -= 0.01;
}
}
// verify asks
@@ -429,10 +428,98 @@ TEST_CASE("fifo_matching_engine_book", "[spartan_md]") {
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 += inc;
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_remove(id);
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_update(ord);
}
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_update(ord);
}
// 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_new_order(ord);
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_new_order(ord);
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);
}
}
} // namespace bts::spartan::mdp3::tests