diff --git a/.vscode/launch.json b/.vscode/launch.json index 5987a5819..d4a93a50a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -111,7 +111,7 @@ "type": "cppdbg", "request": "launch", "program": "/home/bts/spark/build/ninja_debug/src/server/sparkd/sparkd", - "args": ["test", "-a", "SyntheticBookMerge"], + "args": ["test", "-a", "[synthetic]"], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/src/server/sparkd/", "environment": [], diff --git a/src/client/spark_core/spark_core.csproj b/src/client/spark_core/spark_core.csproj index f216df05c..4f70a77f5 100644 --- a/src/client/spark_core/spark_core.csproj +++ b/src/client/spark_core/spark_core.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index e2a2d9222..18cd5363a 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -83,67 +83,78 @@ TimeStamp copy_depth( struct Level { double price; uint16_t size; - void down(uint16_t q) { - bts_assert(q <= size); - size -= q; - } - bool active() const { return size > 0; } }; struct DepthTraversal { std::array levels; size_t head; api::Side side; - double mult; - DepthTraversal(api::Side side, double mult, const PriceSize* depth, size_t depth_count) : levels(), head(0), side(side), mult(mult) { - // levels is zero initialized + double qty, price_mult; + DepthTraversal(api::Side side, double qty, double price_mult, const PriceSize* depth, size_t depth_count) : + levels(), head(0), side(side), qty(qty), price_mult(price_mult) { + // Copy prices into mutable storage for (size_t x = 0; x < depth_count; ++x) { - /// every level has a size of atleast one - /// The sizes are scaled, meaning adjusted to the mult. This simplifies - /// things, however at the cost of rouding off the fractions. So ex if mult is - /// 2 and there is a size of 7, it would be rounded off to 3. The remaining 1 - /// is not considered. Also if there are sizes less than mult they are - /// floored to 0 and ignored. A better solution would be to count them and - /// move till their sum exceed the mult levels[x].price = depth[x].price(); - levels[x].size = scale_size(depth[x].size(), mult); + levels[x].size = depth[x].size(); } } - uint16_t scale_size(uint16_t size, double m) { - double dval = std::ceil( (double)size / m ); - return (uint16_t)(dval); + double try_take1() { + if (levels[head].size >= qty) { + levels[head].size -= qty; + return levels[head].price * price_mult; + } + 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(); + } + } + return (price / qty) * price_mult; + } } - bool active() const { return head < levels.size() && levels[head].size > 0; } - double down(uint16_t qty) { - bts_assert(active()); - bts_assert(qty <= levels[head].size); - double rv = levels[head].price; - levels[head].down(qty); - if (!levels[head].active()) { ++head; } - return rv; - } - uint16_t normalized_head_size() const { return active()? levels[head].size : 0; } }; -void copy_depth(size_t count, api::Side, std::vector& leg_travs, PriceSize* out_price) { +void copy_depth(size_t count, api::Side, std::vector& leg_travs, Level* out_price) { size_t out_index = 0; - while(out_index < count && sane::true_for_all(leg_travs, [](const DepthTraversal& dt) { return dt.active(); })) { - double price_at_this_level = 0.0; - uint16_t min_head = sane::min(leg_travs, [](const DepthTraversal& dt) { return dt.normalized_head_size(); }); + bool first = true; + while(out_index < count) { + double price = 0.0; for (auto& dt : leg_travs) { - double leg_price = dt.down(min_head); - price_at_this_level += leg_price * dt.mult * (dt.side == api::SIDE_BUY ? 1.0 : -1.0); + double temp = dt.try_take1() * (dt.side == api::SIDE_BUY ? 1.0 : -1.0); + price += temp; + } + if (first) { + first = false; + out_price[out_index].price = price; + out_price[out_index].size = 1; + } + else if (price == out_price[out_index].price) { + out_price[out_index].size += 1; + } + else if (os::is_nan(price)) { + // We've run out of book so we nan out any remaining levels and finish + for (size_t x=out_index+1; x < count; ++x) { + out_price[x].price = os::nan(); + bts_assert_d(out_price[x].size == 0); + out_price[x].size = 0; + } + return; + } + else { + out_index += 1; + if (out_index < count) { + out_price[out_index].price = price; + out_price[out_index].size = 1; + } } - out_price[out_index] = PriceSize::from_double<7>(price_at_this_level, min_head); - ++out_index; } - // Zero remaining requested - for (size_t x = out_index; x < count; ++x) { - out_price[x] = PriceSize::NO_MARKET; - } -} - -api::Side flip_side(api::Side s) { - return s == api::SIDE_BUY ? api::SIDE_SELL : api::SIDE_BUY; } TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bid, PriceSize* ask) { @@ -161,11 +172,19 @@ TimeStamp copy_depth(const SyntheticStrategy& synth, size_t count, PriceSize* bi const auto& leg = synth.security.synthetic().legs(x); const auto& eff_bid = leg.side() == api::SIDE_BUY ? bids : asks; const auto& eff_ask = leg.side() == api::SIDE_BUY ? asks : bids; - bid_leg_travs.push_back(DepthTraversal(leg.side(), leg.quantity(), &eff_bid[0], eff_bid.size())); - ask_leg_travs.push_back(DepthTraversal(leg.side(), leg.quantity(), &eff_ask[0], eff_ask.size())); + bid_leg_travs.push_back(DepthTraversal(leg.side(), leg.quantity(), leg.price_multiplier(), &eff_bid[0], eff_bid.size())); + ask_leg_travs.push_back(DepthTraversal(leg.side(), leg.quantity(), leg.price_multiplier(), &eff_ask[0], eff_ask.size())); + } + std::array scratch {}; + copy_depth(count, api::SIDE_BUY, bid_leg_travs, scratch.data()); + for (size_t x = 0; x < count; ++x) { + bid[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size); + } + memset(scratch.data(), 0, sizeof(scratch)); + copy_depth(count, api::SIDE_SELL, ask_leg_travs, scratch.data()); + for (size_t x = 0; x < count; ++x) { + ask[x] = PriceSize::from_double<7>(scratch[x].price, scratch[x].size); } - copy_depth(count, api::SIDE_BUY, bid_leg_travs, bid); - copy_depth(count, api::SIDE_SELL, ask_leg_travs, ask); return rv; } diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index 4f4045750..15be3fead 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -107,15 +107,15 @@ public: return HandleReadResult::NO_CHANGE; } - HandleReadResult get_depth(TimeStamp& time, size_t copydepth, PriceSize* bids, PriceSize* asks) const { - bts_assert_d(copydepth > 0 && copydepth <= COCONUT_MAX_DEPTH); - if (!is_initialized()) return HandleReadResult::NO_CHANGE; + HandleReadResult get_depth(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { + bts_assert_d(copydepth <= COCONUT_MAX_DEPTH); + if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE; switch(_type) { case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE; case HANDLETYPE_DEPTH: if (book.hot->timestamp > time || book.hot->timestamp == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(book.hot->timestamp, book.hot->book, copydepth, bids, asks); + TimeStamp temp = copy_depth(book.hot->timestamp, book.hot->book, std::max(copydepth, COCONUT_MAX_DEPTH), bids, asks); if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; return HandleReadResult::SUCCESS; @@ -126,7 +126,7 @@ public: case HANDLETYPE_SYNTHETIC: auto t = last_market_update(); if (t > time || t == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(*synthetic.def, copydepth, bids, asks); + TimeStamp temp = copy_depth(*synthetic.def, std::max(copydepth, COCONUT_MAX_DEPTH), bids, asks); if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; return HandleReadResult::SUCCESS; diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index a5973e86d..5d5e77de1 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -50,7 +50,7 @@ void setup_market(MDProducerHandle producer, const vector& bids, const vecto void expect_market(MDConsumerHandle handle, const vector bids, const vector& asks) { array raw_bids,raw_asks; TimeStamp t; - handle.get_depth(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + handle.get_depth(t, max(bids.size(), asks.size()), raw_bids.data(), raw_asks.data()); vector src_bids, src_asks; sane::map(raw_bids, src_bids, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()};}); sane::map(raw_asks, src_asks, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()}; }); @@ -82,6 +82,7 @@ tuple,vector> make_securities(c auto l = ps->mutable_legs()->Add(); l->mutable_security()->CopyFrom(instr.consumer().get_normalized_security()); l->set_quantity(get(ldesc)); + l->set_price_multiplier(get(ldesc)); l->set_side(get(ldesc)); synthetic->legs.push_back(instr.consumer()); legs.push_back(instr); @@ -90,13 +91,11 @@ tuple,vector> make_securities(c } TEST_CASE("SyntheticBookMerge", "[md][synthetic]") { - shared_ptr synthetic; vector test_instr; tie(synthetic, test_instr) = make_securities({ {1.0, api::SIDE_BUY}, {1.0, api::SIDE_SELL }}); auto es_buy1 = test_instr[0]; auto es_sell1 = test_instr[1]; - MDConsumerHandle synthetic_handle{synthetic}; // Simplest case @@ -128,9 +127,50 @@ TEST_CASE("SyntheticBookMerge", "[md][synthetic]") { expect_market(synthetic_handle, {}, {}); } - // Test available size=fraction of leg quantity } +TEST_CASE("SyntheticBook+1-2", "[md][synthetic]") { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ {1.0, api::SIDE_BUY}, {2.0, api::SIDE_SELL }}); + auto es_buy1 = test_instr[0]; + auto es_sell2 = test_instr[1]; + MDConsumerHandle synthetic_handle{synthetic}; + + // Test available size=fraction of leg quantity + SECTION("buy 1 sell 2") { + setup_market(es_buy1.producer(), + {{190,1}, {180,11}, {170,4}, {160,10}}, + {{200,2}, {210,6}, {220,8}, {230,12}}); + setup_market(es_sell2.producer(), + {{90,5}, {80,9}, {70,11}, {60,10}, {50,1}}, + {{100,16}, {110,8}, {120,12}, {130,5}}); + expect_market(synthetic_handle, + {{-10,1}, {-20,7}, {-40,4}, {-70,4}, {-80,2}, {-100,2}}, + {{20,2}, {40,1}, {50,4}, {70,1}, {80,4}, {90,1}, {100,3}, {110,1}, {120,1}}); + } +} +TEST_CASE("SyntheticBook+2-2", "[md][synthetic]") { + shared_ptr synthetic; + vector test_instr; + tie(synthetic, test_instr) = make_securities({ {2.0, api::SIDE_BUY}, {2.0, api::SIDE_SELL }}); + auto es_buy2 = test_instr[0]; + auto es_sell2 = test_instr[1]; + MDConsumerHandle synthetic_handle{synthetic}; + + setup_market(es_buy2.producer(), + {{2000.0, 10}, {1999.75, 5}, {1999.5,7}}, + {{2000.25, 15}, {2000.5, 5}, {2001.0,10}}); + setup_market(es_sell2.producer(), + {{2003.0, 5}, {2002.75, 15}, {2002.5,12}}, + {{2003.25, 12}, {2003.5, 3}, {2003.75,5}}); + + expect_market(synthetic_handle, + {{-6.5,5}, {-7.0,1}, {-7.5,1}, {-8.0,1}, {-8.5,2}}, + {{-5.50,2}, {-5.25,1}, {-5.00,4}, {-4.75,1}, {-4.5,2}, {-3.00,5}}); +} + + TEST_CASE("Synthetic3Legs", "[md][synthetic]") { shared_ptr synthetic; vector test_instr;