diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp index 786e2ad2d..f9d79c2db 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.cpp @@ -103,10 +103,6 @@ void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) { sync_orders(); } -struct MinMax { - uint32_t min, max; -}; - // TEST case: // total = 300, clip = 100 // constraints: 50,50,50,50 diff --git a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h index ff2d86598..927bd87f3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/SplitterAlgo.h @@ -28,5 +28,9 @@ private: void terminate() const; }; +// exposed for unit testing purposes. +struct MinMax {uint32_t min, max;}; +void compute_desired_sizes(uint32_t , uint32_t , + const std::vector& , std::vector* ); }}} //namespace diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp index adb06283b..5139d971a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/splitter_tests.cpp @@ -1,11 +1,13 @@ -#include "catch/catch.hpp" #include "../SplitterAlgo.h" #include "AlgoTestDriver.h" +#include "catch/catch.hpp" #include "lib/orderman/orderman_utils/Book.h" +#include #include -#include #include +#include +#include "lib/utils/bts_chrono.h" using namespace std; using namespace bts::orderman::splitter; @@ -25,33 +27,75 @@ namespace test { */ -constexpr auto some_price = md::IntPrice { 128000, -3 }; +constexpr auto some_price = md::IntPrice{128000, -3}; -using ExpectedCount = utils::ValueWrapper; +using ExpectedCount = utils::ValueWrapper; struct SimpleCase { ExpectedCount expected_orders; Quantity target; SplitterOrderDefinition sod; }; -SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, uint32_t target) { +SimpleCase mkSimple(ExpectedCount expected, uint32_t max_size, uint32_t exposed, + uint32_t target) { SimpleCase result; result.expected_orders = expected; - result.target = Quantity {target}; + result.target = Quantity{target}; result.sod.set_max_order_qty(max_size); result.sod.set_exposed_qty(exposed); return result; } +TEST_CASE("compute_desired_sizes fuzzing", "[algo][splitter]") { + srand((unsigned)time(0)); + + auto start = Timer::now(); + + while (sec_diff(Timer::now(),start) < 5) { + uint32_t total = rand() % 100 + 1; + uint32_t clip = rand() % 4 + 1; + std::vector constraints(rand() % 10); + std::vector result; + + uint32_t all_fills = 0; + for (auto &mm : constraints) { + uint32_t order_working = rand() % 2; + uint32_t fills = rand() % 2 * clip; + all_fills += fills; + mm = {fills, order_working ? fills + rand() % clip + 1 : fills}; + } + if (all_fills < total) { + compute_desired_sizes(total, clip, constraints, &result); + uint32_t exposed_qty = 0; + for (auto q : result) + exposed_qty += q.value(); + + bool clip_size_honored = true; + for (auto q : result) + clip_size_honored = clip_size_honored && (q.value() <= clip); + + if (exposed_qty != total || !clip_size_honored) { + std::cout << "total: " << total << "\n" + << "clip: " << clip << "\n{ "; + + for (auto mm : constraints) + std::cout << "{" << mm.min << "," << mm.max << "},"; + std::cout << " }\n"; + } + REQUIRE(clip_size_honored); + REQUIRE(exposed_qty == total); + } + } +} TEST_CASE("initial orders", "[algo][splitter]") { std::vector cases; - cases.push_back(mkSimple(ExpectedCount {10},10, 100, 50)); - cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto expected = tcase.expected_orders.value(); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -67,15 +111,15 @@ TEST_CASE("initial orders", "[algo][splitter]") { TEST_CASE("completes on target", "[algo][splitter]") { std::vector cases; - cases.push_back(mkSimple(ExpectedCount {3}, 30, 90, 50)); - cases.push_back(mkSimple(ExpectedCount {10}, 10, 100, 50)); - cases.push_back(mkSimple(ExpectedCount {11}, 10, 105, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 50)); - cases.push_back(mkSimple(ExpectedCount {5}, 10, 50, 150)); - cases.push_back(mkSimple(ExpectedCount {4}, 13, 50, 150)); - cases.push_back(mkSimple(ExpectedCount {12}, 13, 150, 50)); + cases.push_back(mkSimple(ExpectedCount{3}, 30, 90, 50)); + cases.push_back(mkSimple(ExpectedCount{10}, 10, 100, 50)); + cases.push_back(mkSimple(ExpectedCount{11}, 10, 105, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 50)); + cases.push_back(mkSimple(ExpectedCount{5}, 10, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{4}, 13, 50, 150)); + cases.push_back(mkSimple(ExpectedCount{12}, 13, 150, 50)); - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto expected = tcase.expected_orders.value(); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -88,48 +132,47 @@ TEST_CASE("completes on target", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); SECTION("Fill completely") { - auto orders = state.child_orders(); + auto orders = state.child_orders(); // Fill the order size_t filled = 0; size_t iterations = 0; - while(state.state() == AlgoState::RUNNING) { + while (state.state() == AlgoState::RUNNING) { // loop to allow more orders to be added ++iterations; REQUIRE(iterations < 999); - for (const auto& order : orders) { - state.fill(order.key, Price (some_price.price()), order.cstate.quantity); + for (const auto &order : orders) { + state.fill(order.key, Price(some_price.price()), + order.cstate.quantity); filled += order.cstate.quantity.value(); if (filled >= tcase.target.value()) { REQUIRE(state.state() == AlgoState::COMPLETED); break; - } - else { + } else { REQUIRE(state.state() == AlgoState::RUNNING); } } } } SECTION("fill one by one") { - const auto& orders = state.child_orders(); + const auto &orders = state.child_orders(); // Fill 1 per iteration. // Algo should add more orders if not enough are visible initially size_t filled = 0; size_t iterations = 0; - while(state.state() == AlgoState::RUNNING) { + while (state.state() == AlgoState::RUNNING) { ++iterations; - REQUIRE(iterations < 999);//not making progress - for (const auto& order : orders) { + REQUIRE(iterations < 999); // not making progress + for (const auto &order : orders) { if (order.cstate.status == ChildStatus::WORKING) { - auto price = Price {some_price.price()}; - state.fill(order.key, price, Quantity {1}); + auto price = Price{some_price.price()}; + state.fill(order.key, price, Quantity{1}); filled += 1; // Fills should never cause new orders to get added REQUIRE(state.child_orders().size() == expected); if (filled >= tcase.target.value()) { REQUIRE(state.state() == AlgoState::COMPLETED); break; - } - else { + } else { REQUIRE(state.state() == AlgoState::RUNNING); } } @@ -144,30 +187,22 @@ TEST_CASE("updates", "[algo][splitter]") { std::vector, SimpleCase>> cases; // reduce max order size, increase # orders - cases.push_back({ - mkSimple(ExpectedCount {10} /*order count*/, 10 /*max*/, 100 /*exposed*/, 50 /*target*/), - {}, - mkSimple(ExpectedCount {20}, 5, 100, 50) - }); + cases.push_back({mkSimple(ExpectedCount{10} /*order count*/, 10 /*max*/, + 100 /*exposed*/, 50 /*target*/), + {}, + mkSimple(ExpectedCount{20}, 5, 100, 50)}); /// reduce max order size, keep # orders constant - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {}, - mkSimple(ExpectedCount {10}, 5, 50, 50) - }); - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {5,5}, - mkSimple(ExpectedCount {8}, 5, 50, 40) - }); - cases.push_back({ - mkSimple(ExpectedCount {10}, 10, 100, 50), - {5,5}, - mkSimple(ExpectedCount {18}, 5, 100, 50) - }); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {}, + mkSimple(ExpectedCount{10}, 5, 50, 50)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{8}, 5, 50, 40)}); + cases.push_back({mkSimple(ExpectedCount{10}, 10, 100, 50), + {5, 5}, + mkSimple(ExpectedCount{18}, 5, 100, 50)}); - - for (const auto& tcase : cases) { + for (const auto &tcase : cases) { auto case_init = std::get<0>(tcase); api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); @@ -178,14 +213,14 @@ TEST_CASE("updates", "[algo][splitter]") { state.launch(); REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == case_init.expected_orders.value()); - for (const auto& o : state.child_orders()) { + for (const auto &o : state.child_orders()) { if (o.cstate.status == ChildStatus::WORKING) { REQUIRE(o.cstate.quantity.value() <= case_init.sod.exposed_qty()); } } - for(auto q : std::get<1>(tcase)) { - state.fill({}, Quantity {q}); + for (auto q : std::get<1>(tcase)) { + state.fill({}, Quantity{q}); } Update update; @@ -198,9 +233,11 @@ TEST_CASE("updates", "[algo][splitter]") { state.do_update(update); REQUIRE(state.state() == AlgoState::RUNNING); size_t working = 0; - for (const auto& c : state.child_orders()) if (c.cstate.status == ChildStatus::WORKING) working += 1; + for (const auto &c : state.child_orders()) + if (c.cstate.status == ChildStatus::WORKING) + working += 1; REQUIRE(working == case_next.expected_orders.value()); - for (const auto& o : state.child_orders()) { + for (const auto &o : state.child_orders()) { if (o.cstate.status == ChildStatus::WORKING) { REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty()); } @@ -218,21 +255,23 @@ TEST_CASE("Reduction with fills", "[algo][splitter]") { // expected: 100, 100, 50, 50 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - for (const auto& order : state.child_orders()) { - state.fill(order.key, {}, Quantity {50}); + for (const auto &order : state.child_orders()) { + state.fill(order.key, {}, Quantity{50}); } // Reduce exposed quantity @@ -244,12 +283,16 @@ TEST_CASE("Reduction with fills", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 300); - + // need this to be 100,100,50,50 not 100,100,100 - std::set actual, expected {100,100,50,50}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + std::set actual, expected{100, 100, 50, 50}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -264,23 +307,25 @@ TEST_CASE("Reduction with fills 2", "[algo][splitter]") { // expected: 100, 100, 65, 35 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - state.fill({}, Quantity {35}); - state.fill({}, Quantity {45}); - state.fill({}, Quantity {55}); - state.fill({}, Quantity {65}); + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); // Reduce exposed quantity Update update; @@ -291,12 +336,16 @@ TEST_CASE("Reduction with fills 2", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 300); - + // need this to be 100,100,50,50 not 100,100,100 - std::set actual, expected {100,100,65,35}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + std::set actual, expected{100, 100, 65, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -311,23 +360,25 @@ TEST_CASE("Reduction with fills 3", "[algo][splitter]") { // expected: 66, 55, 45, 35 api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills - state.fill({}, Quantity {35}); - state.fill({}, Quantity {45}); - state.fill({}, Quantity {55}); - state.fill({}, Quantity {65}); + state.fill({}, Quantity{35}); + state.fill({}, Quantity{45}); + state.fill({}, Quantity{55}); + state.fill({}, Quantity{65}); // Reduce exposed quantity Update update; @@ -338,11 +389,15 @@ TEST_CASE("Reduction with fills 3", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); size_t total = 0; - for (const auto& order : state.child_orders()) { total += order.cstate.quantity.value(); } + for (const auto &order : state.child_orders()) { + total += order.cstate.quantity.value(); + } REQUIRE(total == 201); - - std::set actual, expected {66,55,45,35}; - for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); } + + std::set actual, expected{66, 55, 45, 35}; + for (const auto &order : state.child_orders()) { + actual.insert(order.cstate.quantity.value()); + } REQUIRE(actual == expected); } @@ -356,17 +411,19 @@ TEST_CASE("reduce to completion", "[algo][splitter]") { // expected: algo transitions to copmleted state canceling 2 orders api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills state.fill({}, Quantity{100}); @@ -380,11 +437,10 @@ TEST_CASE("reduce to completion", "[algo][splitter]") { REQUIRE(state.state() == AlgoState::COMPLETED); size_t total = 0; - for (const auto& order : state.child_orders()) { + for (const auto &order : state.child_orders()) { if (order.cstate.status == ChildStatus::WORKING) { total += order.cstate.quantity.value(); - } - else { + } else { total += order.cstate.total_fills.value(); } } @@ -401,17 +457,19 @@ TEST_CASE("reduce to completion 2", "[algo][splitter]") { // expected: algo transitions to copmleted state canceling 2 orders api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(400); - AlgoTestDriver state(def, some_price, Quantity {300}); + AlgoTestDriver state(def, some_price, Quantity{300}); REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } // Add fills state.fill({}, Quantity{100}); @@ -437,25 +495,28 @@ TEST_CASE("Async Fills", "[algo][splitter]") { api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(20); splitter->set_exposed_qty(80); - AlgoTestDriver state(def, some_price, Quantity {80}); - + AlgoTestDriver state(def, some_price, Quantity{80}); + REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 4); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 20); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 20); + } // Set up some instant fills bool applied_fills = false; - state.on_child_changing([&](const auto&) { - if (applied_fills) return; + state.on_child_changing([&](const auto &) { + if (applied_fills) + return; applied_fills = true; // 7 fills on the last order which is has a size reduction in flight - state.fill(state.child_orders().rbegin()->key, {}, Quantity {7}); + state.fill(state.child_orders().rbegin()->key, {}, Quantity{7}); }); Update update; @@ -465,13 +526,14 @@ TEST_CASE("Async Fills", "[algo][splitter]") { // This should trigger 1 order to be reduced to size 5 from 20 // in the meantime we'll trigger fills of size 7 // This results in an overfill for the 5 lot order - // And the + // And the state.do_update(update); - // expected result: + // expected result: // 7 fills, exposed_qty 65 = 58 active uint32_t active = 0; - for (const auto& o : state.child_orders()) active += o.active_qty(); + for (const auto &o : state.child_orders()) + active += o.active_qty(); REQUIRE(active == 58); } @@ -480,23 +542,25 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { // Increase the size to 400 so we launch a 4th order // Then instantly send another update to 350 - // NOTE: this test is not very good due to limitations in the test driver + // NOTE: this test is not very good due to limitations in the test driver // I'm leaving it in because it passes but if it starts failing in the future // it's probably OK to disable it api::orderman::OrderDefinition def; def.set_side(api::SIDE_BUY); - auto* splitter = def.mutable_custom_def()->mutable_splitter(); + auto *splitter = def.mutable_custom_def()->mutable_splitter(); splitter->set_max_order_qty(100); splitter->set_exposed_qty(300); - AlgoTestDriver state(def, some_price, Quantity {300}); - + AlgoTestDriver state(def, some_price, Quantity{300}); + REQUIRE(state.before_launch().ok()); state.launch(); // check initial state REQUIRE(state.state() == AlgoState::RUNNING); REQUIRE(state.child_orders().size() == 3); - for(const auto& c : state.child_orders()) { REQUIRE(c.cstate.quantity.value() == 100); } + for (const auto &c : state.child_orders()) { + REQUIRE(c.cstate.quantity.value() == 100); + } Update update; update.set_price(state.price().value()); @@ -505,7 +569,7 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { // Set up some instant fills bool applied_second_update = false; - state.on_child_launching([&,update](const auto&) mutable { + state.on_child_launching([&, update](const auto &) mutable { if (!applied_second_update) { applied_second_update = true; update.mutable_custom()->mutable_splitter()->set_exposed_qty(350); @@ -515,12 +579,14 @@ TEST_CASE("Increase then decrease", "[algo][splitter]") { state.do_update(update); - // expected result: + // expected result: // 350 exposed, NOT 450 uint32_t quantity = 0; - for (const auto& o : state.child_orders()) quantity += o.active_qty(); + for (const auto &o : state.child_orders()) + quantity += o.active_qty(); REQUIRE(quantity == 350); } - -}}}//namespace \ No newline at end of file +} // namespace test +} // namespace orderman +} // namespace bts