diff --git a/.vscode/launch.json b/.vscode/launch.json index b7611bac1..63af09c1d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -209,7 +209,7 @@ "type": "cppdbg", "request": "launch", "program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd", - "args": ["test", "SPARK-955"], + "args": ["test", "SyntheticBook+1-2"], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/", "environment": [], diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index 6f11dcece..ca328334e 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -85,7 +85,7 @@ TimeStamp copy_depth( ///////////////////////////// struct Level { double price; - uint16_t size; + double size; }; struct DepthTraversal { std::array levels; @@ -100,28 +100,38 @@ struct DepthTraversal { levels[x].size = depth[x].size(); } } - double try_take1() { - if (head >= levels.size()) return os::nan(); - if (levels[head].size >= qty) { - levels[head].size -= qty; - return levels[head].price * price_mult; + double available_size() const { + double available = 0.0; + auto cur = head; + bool partial = false; + while(cur < levels.size() && available < 1) { + available += levels[cur].size / qty; + if (available < 1.0) partial = true; + ++cur; } - else { - double price = 0; - size_t count = 0; - while(count < qty) { - if (levels[head].size >= 1) { - price += levels[head].price; - levels[head].size -= 1; - count += 1; - } - else { - head++; - if (head >= levels.size()) return os::nan(); - } + if (partial && available >= 1.0) return 1.0; + return available; + } + double consume(double size) { + bts_assert_d(size <= available_size()); + double take = size * qty; + double price = 0.0; + while(take > 0) { + if (levels[head].size >= take) { + price += levels[head].price * price_mult * (take/qty); + levels[head].size -= take; + if (levels[head].size <= 0) ++head; + break; + } + else { + double available = levels[head].size; + price += levels[head].price * price_mult * (available/qty); + levels[head].size = 0; + ++head; + take -= available; } - return (price / qty) * price_mult; } + return price/size; } }; @@ -129,13 +139,20 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification& size_t out_index = 0; bool first = true; while(out_index < count) { - double price = 0.0; - for (auto& dt : leg_travs) { - double temp = dt.try_take1(); - price += temp; - } - if (os::is_nan(price)) { + double price = 0.0; + // Find the leg with the minimum available size + double size = std::numeric_limits().max(); + + for (const auto& dt : leg_travs) { + auto sz = dt.available_size(); + // If we can't get 1.0 from any leg we're done + if (sz <= size) size = sz; + if (size < 1.0) break; + } + size = (int64_t)size; + + if (size < 1.0) { // We've run out of book so we nan out any remaining levels and finish size_t start = first ? out_index : out_index+1; for (size_t x=start; x < count; ++x) { @@ -145,6 +162,13 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification& return; } + // Take that size from each leg + for (auto& dt : leg_travs) { + double temp = dt.consume(size); + price += temp; + } + bts_assert_d(!os::is_nan(price)); + // Round to tick size price = securities::checked_round_to_quote_price(spec, price, side); @@ -154,13 +178,13 @@ void copy_depth(size_t count, api::Side side, const api::ContractSpecification& } if (price == out_price[out_index].price) { - out_price[out_index].size += 1; + out_price[out_index].size += size; } else { out_index += 1; if (out_index < count) { out_price[out_index].price = price; - out_price[out_index].size = 1; + out_price[out_index].size = size; } } } diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index 8dd5f9e4c..f717c77db 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -263,6 +263,7 @@ TEST_CASE("SPARK-955", "[md][synthetic]") { expect_market(synthetic_handle, // If we do strict price this is right. If we're pessimistic we get {-4.25,2} { {-4.00, 1}, {-4.25, 1}, { -5.00, 3}, {-5.25, 3}, {-5.50,1}, {-5.75, 6} }, + // { {-4.25, 2}, { -5.00, 3}, {-5.25, 3}, {-5.50,1}, {-5.75, 6} }, { {5.00, 3}, {5.25, 1}, {6.00,3}, {6.50,6} }); } @@ -271,7 +272,7 @@ TEST_CASE("SPARK-954", "[md][synthetic]") { vector test_instr; SECTION("price multiplier = 0.9,1.1") { - tie(synthetic, test_instr) = make_securities({ {0.9, 0.025, api::SIDE_BUY}, {1.1, 0.025, api::SIDE_SELL } }); + tie(synthetic, test_instr) = make_securities({ {0.9, 0.25, api::SIDE_BUY}, {1.1, 0.25, api::SIDE_SELL } }); auto leg1 = test_instr[0]; auto leg2 = test_instr[1]; MDConsumerHandle synthetic_handle(synthetic); @@ -288,6 +289,7 @@ TEST_CASE("SPARK-954", "[md][synthetic]") { } } + TEST_CASE("Synthetic_net_change", "[md][synthetic]") { shared_ptr synthetic; vector test_instr;