tests for single price level

This commit is contained in:
Taha Afzal committed 2020-08-20 10:58:57 -04:00
1 parent 0d0af32b85
commit e6c2ceb931
5 files changed
+132 -17

No files matched your search

@@ -6,6 +6,11 @@ namespace bts::spartan::mdp3 {
// private helpers ////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void FifoPriceLevelMatcher::_compress(){
_orders.erase(
std::remove_if(_orders.begin(),
_orders.end(),
[](const Order& o) { return !o.is_active; }),
_orders.end());
}
FifoPriceLevelMatcher::Order*
@@ -91,11 +96,13 @@ void FifoPriceLevelMatcher::on_order_update(const MDP3Order& upd_ord){
}
else
ord_p->quantity = upd_ord.quantity;
_compress();
}
void FifoPriceLevelMatcher::on_order_remove(MDP3OrderKey k){
FifoPriceLevelMatcher::Order* ord_p = _find(k);
bts_assert(ord_p != nullptr);
_logically_delete(*ord_p);
_compress();
}
MDP3Quantity FifoPriceLevelMatcher::on_new_opp_order(MDP3Quantity opp_ord_qty){
auto rem = opp_ord_qty.value();
@@ -119,6 +126,7 @@ MDP3Quantity FifoPriceLevelMatcher::on_new_opp_order(MDP3Quantity opp_ord_qty){
if(rem == 0)
break;
}
_compress();
bts_assert(rem <= opp_ord_qty.value());
return MDP3Quantity(rem);
}
@@ -29,7 +29,6 @@ private:
api::Side _side;
MDP3OrderPriority _lowest_priority;
std::vector<FifoPriceLevelMatcher::Order> _orders;
// removes the holes
private:
// helpers /////////////////////////////////////////////////////////////////
void _compress();
@@ -85,6 +84,9 @@ public:
void set_price(md::IntPrice p){
_price = p;
}
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);
@@ -59,18 +59,13 @@ std::tuple<FifoPriceLevelMatcher*,FifoPriceLevelMatcher::Order*>
}
void FifoSecurityMatcher::_remove_empty_price_levels(){
_empty_price_levels.clear();
for (const auto& p: _bid_book)
if(!p.populated())
_empty_price_levels.emplace_back(api::Side::SIDE_BUY, p.price());
for (const auto& p: _ask_book)
if(!p.populated())
_empty_price_levels.emplace_back(api::Side::SIDE_SELL, p.price());
for (const auto& t : _empty_price_levels){
auto& bk = _book(std::get<0>(t));
_search_tok.set_price(std::get<1>(t));
bk.erase(_search_tok);
}
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());
_bid_book.erase(
std::remove_if(_ask_book.begin(), _ask_book.end(), rem_if),
_ask_book.end());
}
// constructors ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -78,7 +73,6 @@ FifoSecurityMatcher::FifoSecurityMatcher(const InstrumentSnapshot& snp)
:
_bid_book(),
_ask_book(),
_empty_price_levels(),
_search_tok(md::IntPrice(),
api::Side::SIDE_BUY)
{
@@ -14,9 +14,6 @@ private:
// are sorted lowest to highest
boost::container::flat_set<FifoPriceLevelMatcher> _bid_book,
_ask_book;
// auxiliary vector to help with not allocating memory when
// removing price levels
std::vector<std::tuple<api::Side,md::IntPrice>> _empty_price_levels;
FifoPriceLevelMatcher _search_tok;
private:
// helpers /////////////////////////////////////////////////////////////////
@@ -15,6 +15,7 @@
#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/md/mdp3/back_testing/FifoPriceLevelMatcher.h"
#include "server/sparkd/cmds/spartan/util/PCapReader.h"
#include <fmt/format.h>
@@ -258,4 +259,117 @@ TEST_CASE("spartan_md", "[spartan_md]") {
REQUIRE(maybe_snapshot.value().size());
}
}
MDP3Order create_order(uint64_t key,
int32_t sec,
api::Side side,
uint64_t priority,
double price,
uint32_t qty){
return MDP3Order {
MDP3OrderKey(key),
MDP3SecurityID(sec),
side,
MDP3OrderPriority(priority),
md::IntPrice_from_double<7>(price),
MDP3Quantity(qty)
};
}
TEST_CASE("fifo_matching_engine", "[spartan_md]") {
auto sec_key = 10;
double price = 100.0;
api::Side side = api::Side::SIDE_BUY;
FifoPriceLevelMatcher price_level(md::IntPrice_from_double<7>(price),
side);
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("single_level_add") {
// add 100 orders
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
}
SECTION("single_level_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("single_level_sweep") {
create_book(100);
REQUIRE(price_level.n_orders() == 100);
REQUIRE(price_level.quantity().value() == 5050);
price_level.on_new_opp_order(MDP3Quantity(5100));
REQUIRE(price_level.n_orders() == 0);
REQUIRE(price_level.quantity().value() == 0);
}
SECTION("single_level_full_fill") {
create_book(100);
// take out the firdt 20 completely
price_level.on_new_opp_order(MDP3Quantity(210));
REQUIRE(price_level.n_orders() == 80);
REQUIRE(price_level.quantity().value() == 4840);
REQUIRE(price_level.orders().at(79).key.value() == 100);
}
SECTION("single_level_partial_fill") {
create_book(100);
// take out the firdt 20 completely
price_level.on_new_opp_order(MDP3Quantity(193));
REQUIRE(price_level.n_orders() == 81);
REQUIRE(price_level.quantity().value() == 4857);
}
SECTION("single_level_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,
ord_59.priority.value(),
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("single_level_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,
ord_59.priority.value(),
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);
}
}
} // namespace bts::spartan::mdp3::tests