From 33dad3db73025dd3f95cba49d2f7c5e98d195ce2 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Sun, 10 Jan 2021 18:02:25 -0500 Subject: [PATCH] More book tweaks. --- .../cmds/spartan/domain/SecuritySnapshot.cpp | 2 +- .../sparkd/cmds/spartan/domain/book/Book.cc | 20 ++++--- .../sparkd/cmds/spartan/domain/book/Book.cpp | 52 ------------------- .../sparkd/cmds/spartan/domain/book/Book.h | 5 +- .../md/common/tests/book_merging_tests.cpp | 20 +++---- 5 files changed, 28 insertions(+), 71 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/domain/SecuritySnapshot.cpp b/src/server/sparkd/cmds/spartan/domain/SecuritySnapshot.cpp index 6d70072e6..853203ea0 100644 --- a/src/server/sparkd/cmds/spartan/domain/SecuritySnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/domain/SecuritySnapshot.cpp @@ -238,7 +238,7 @@ ImpliedBookSide const &SecuritySnapshot::implied_book(Side s) const { } BookSide SecuritySnapshot::merged_book(Side s) const { - return book(s).merge(s,implied_book(s)); + return BookSide::merge(s,book(s),implied_book(s)); } template diff --git a/src/server/sparkd/cmds/spartan/domain/book/Book.cc b/src/server/sparkd/cmds/spartan/domain/book/Book.cc index d5d2217cb..cd9ba4416 100644 --- a/src/server/sparkd/cmds/spartan/domain/book/Book.cc +++ b/src/server/sparkd/cmds/spartan/domain/book/Book.cc @@ -3,15 +3,15 @@ namespace bts::spartan::mdp3 { -template +template template -BookSideT BookSideT::merge(Side s, OtherSide const &other_book) const { +void BookSideT::merge_with(Side s, OtherSide const &other_book) { boost::container::small_vector< BookSide::value_type, max_depth() + OtherSide::max_depth()> merged; - auto comp = [s] (auto const &l1, auto const &l2) { - return l1.price.is_better(s,l2.price); + auto comp = [s](auto const &l1, auto const &l2) { + return l1.price.is_better(s, l2.price); }; // Copy this to merged output @@ -42,11 +42,17 @@ BookSideT BookSideT::merge(Side s, OtherSide const &other_book) const { } } - // Copy the result from our work array .S - BookSide result; std::copy( merged.begin(), merged.begin() + std::min(merged.size(), size()), - result._book.begin()); + _book.begin()); +} + +template +template +BookSideT +BookSideT::merge(Side s, BookSideT const &b1, OtherBook const &b2) { + auto result = BookSideT{b1}; + result.merge_with(s, b2); return result; } diff --git a/src/server/sparkd/cmds/spartan/domain/book/Book.cpp b/src/server/sparkd/cmds/spartan/domain/book/Book.cpp index d20b34d57..0c5dea546 100644 --- a/src/server/sparkd/cmds/spartan/domain/book/Book.cpp +++ b/src/server/sparkd/cmds/spartan/domain/book/Book.cpp @@ -1,56 +1,4 @@ #include "Book.h" -#include -#include "server/sparkd/cmds/spartan/domain/Price.h" namespace bts::spartan::mdp3 { - -// void merge_books(Side s, BookSide const &b, BookSide const &ib, BookSide *out) { -// boost::container::small_vector< -// BookSide::value_type, -// BookSide::max_depth() + ImpliedBookSide::max_depth()> -// merged; - -// using Comp = std::function; -// auto comp = s == Side::Buy -// ? Comp([](BookLevel const &l1, BookLevel const &l2) { -// return l1.price > l2.price; -// }) -// : Comp([](BookLevel const &l1, BookLevel const &l2) { -// return l1.price < l2.price; -// }); - -// // Copy direct book to merged -// for (auto const &l : b) -// if (l.quantity.value() == 0) -// break; -// else -// merged.push_back(l); - -// // Now for each entry in the implied book find either a matching -// // entry or the entry where the level should be inserted -// for (auto &il : ib) { -// if (il.quantity.value() == 0) -// break; - -// auto it = std::lower_bound(merged.begin(), merged.end(), il, comp); - -// if (it == merged.end()) { -// // level is worse than the worst level so far, append -// merged.push_back(il); -// } else if (it->price == il.price) { -// // level is the same as an existing level, merge -// it->quantity.value() += il.quantity.value(); -// it->n_orders += il.n_orders; -// } else { -// // level is better than what was there before, insert -// merged.insert(it, il); -// } -// } - -// // Copy the result to the output book -// std::copy( -// merged.begin(), merged.begin() + std::min(merged.size(), out->size()), -// out->begin()); -// } - } diff --git a/src/server/sparkd/cmds/spartan/domain/book/Book.h b/src/server/sparkd/cmds/spartan/domain/book/Book.h index 20ed5f118..6d6e20478 100644 --- a/src/server/sparkd/cmds/spartan/domain/book/Book.h +++ b/src/server/sparkd/cmds/spartan/domain/book/Book.h @@ -49,7 +49,10 @@ class BookSideT { } template - BookSideT merge(Side s, OtherBook const &b) const; + void merge_with(Side s, OtherBook const &b) ; + + template + static BookSideT merge(Side s, BookSideT const&,OtherBook const &) ; private: Container _book; diff --git a/src/server/sparkd/cmds/spartan/md/common/tests/book_merging_tests.cpp b/src/server/sparkd/cmds/spartan/md/common/tests/book_merging_tests.cpp index 1b62e1229..6ba44c7ca 100644 --- a/src/server/sparkd/cmds/spartan/md/common/tests/book_merging_tests.cpp +++ b/src/server/sparkd/cmds/spartan/md/common/tests/book_merging_tests.cpp @@ -8,12 +8,12 @@ TEST_CASE("spartan_books", "[book_merging]") { SECTION("trivial") { BookSide b; ImpliedBookSide ib; - auto out = b.merge(Side::Buy, ib); + auto out = BookSide::merge(Side::Buy, b,ib); for (auto &l : out) REQUIRE(l.quantity.value() == 0); b[0] = BookLevel{{1, -9}, Quantity{1}, 1}; - out = b.merge(Side::Buy,ib); + out = BookSide::merge(Side::Buy,b,ib); for (size_t i = 0; i < b.size(); i++) { REQUIRE(b[i] == out[i]); @@ -28,7 +28,7 @@ TEST_CASE("spartan_books", "[book_merging]") { b[0] = BookLevel{{2, -9}, Quantity{1}, 1}; ib[0] = BookLevel{{1, -9}, Quantity{2}, 3}; - out = b.merge(Side::Buy, ib); + out = BookSide::merge(Side::Buy, b,ib); REQUIRE(out[0] == b[0]); REQUIRE(out[1] == ib[0]); REQUIRE(out[2].quantity.value() == 0); @@ -43,14 +43,14 @@ TEST_CASE("spartan_books", "[book_merging]") { b[1] = BookLevel{{2, -9}, Quantity{1}, 3}; ib[0] = BookLevel{{3, -9}, Quantity{2}, 2}; - out = b.merge(Side::Buy, ib); + out = BookSide::merge(Side::Buy,b,ib); REQUIRE(out[0] == b[0]); REQUIRE(out[1] == ib[0]); REQUIRE(out[2] == b[1]); REQUIRE(out[3].quantity.value() == 0); } - SECTION("merge_buy") { + SECTION("merge_with_buy") { BookSide b; ImpliedBookSide ib; BookSide out; @@ -59,7 +59,7 @@ TEST_CASE("spartan_books", "[book_merging]") { b[1] = BookLevel{{3, -9}, Quantity{1}, 3}; ib[0] = BookLevel{{3, -9}, Quantity{2}, 2}; - out = b.merge(Side::Buy, ib); + out = BookSide::merge(Side::Buy,b,ib); REQUIRE(out[0] == b[0]); REQUIRE(out[1] == (BookLevel{{3, -9}, Quantity{3}, 5})); REQUIRE(out[2].quantity.value() == 0); @@ -73,7 +73,7 @@ TEST_CASE("spartan_books", "[book_merging]") { b[0] = BookLevel{{2, -9}, Quantity{1}, 1}; ib[0] = BookLevel{{4, -9}, Quantity{2}, 3}; - out = b.merge(Side::Sell, ib); + out = BookSide::merge(Side::Sell,b,ib); REQUIRE(out[0] == b[0]); REQUIRE(out[1] == ib[0]); REQUIRE(out[2].quantity.value() == 0); @@ -88,14 +88,14 @@ TEST_CASE("spartan_books", "[book_merging]") { b[1] = BookLevel{{4, -9}, Quantity{1}, 3}; ib[0] = BookLevel{{3, -9}, Quantity{2}, 2}; - out = b.merge(Side::Sell, ib); + out = BookSide::merge(Side::Sell,b,ib); REQUIRE(out[0] == b[0]); REQUIRE(out[1] == ib[0]); REQUIRE(out[2] == b[1]); REQUIRE(out[3].quantity.value() == 0); } - SECTION("merge_sell") { + SECTION("merge_with_sell") { BookSide b; ImpliedBookSide ib; BookSide out; @@ -104,7 +104,7 @@ TEST_CASE("spartan_books", "[book_merging]") { b[1] = BookLevel{{4, -9}, Quantity{1}, 3}; ib[0] = BookLevel{{3, -9}, Quantity{2}, 2}; - out = b.merge(Side::Sell, ib); + out = BookSide::merge(Side::Sell,b,ib); REQUIRE(out[0] == (BookLevel{{3, -9}, Quantity{3}, 3})); REQUIRE(out[1] == b[1]); REQUIRE(out[2].quantity.value() == 0);