More book tweaks.

This commit is contained in:
Pedro Pinto committed 2021-01-10 18:02:25 -05:00
1 parent ef296d4b1c
commit 33dad3db73
5 files changed
+28 -71

No files matched your search

@@ -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 <class B>
@@ -3,15 +3,15 @@
namespace bts::spartan::mdp3 {
template<size_t SZ>
template <size_t SZ>
template <class OtherSide>
BookSideT<SZ> BookSideT<SZ>::merge(Side s, OtherSide const &other_book) const {
void BookSideT<SZ>::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<SZ> BookSideT<SZ>::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 <size_t SZ>
template <class OtherBook>
BookSideT<SZ>
BookSideT<SZ>::merge(Side s, BookSideT const &b1, OtherBook const &b2) {
auto result = BookSideT{b1};
result.merge_with(s, b2);
return result;
}
@@ -1,56 +1,4 @@
#include "Book.h"
#include <boost/container/small_vector.hpp>
#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<bool(BookLevel const &, BookLevel const &)>;
// 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());
// }
}
@@ -49,7 +49,10 @@ class BookSideT {
}
template <class OtherBook>
BookSideT merge(Side s, OtherBook const &b) const;
void merge_with(Side s, OtherBook const &b) ;
template <class OtherBook>
static BookSideT merge(Side s, BookSideT const&,OtherBook const &) ;
private:
Container _book;
@@ -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);