Merge branch 'SP-87-Splitter' of bitbucket.org:bluetradesys/spark into SP-23-Dorman2

This commit is contained in:
Eric Harding committed 2019-07-03 14:16:45 -04:00
commit 9e7da44cce
3 files changed
+230 -135

No files matched your search

@@ -18,7 +18,6 @@ using namespace api::orderman;
struct ChildOrderState {
OrderKey key;
ChildState state;
uint32_t remaining() const { return state.quantity.value() - state.total_fills.value(); }
uint32_t min_qty() const { return state.total_fills.value(); }
};
@@ -55,10 +54,12 @@ OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) {
"max order quantity and exposed quantity must be greater than 0"};
}
// Create child template
auto child_def = def;
*child_def.mutable_custom_def() = splitter.child();
me->child_template = &ibl.make_child_template(child_def);
if (me->exposed_qty < me->target_qty) {
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
"Exposed quantity must be greater than or equal to target quantity"};
}
update_child_template(ibl);
me->orders.reserve(me->exposed_qty / me->clip + 1);
@@ -81,17 +82,27 @@ void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) {
bts_assert(false);
}
void SplitterAlgo::do_update(const api::orderman::Update& update) {
// Update # child orders
const CustomOrderDefinition* new_child_def = nullptr;
if (update.has_custom() && update.custom().has_splitter()) {
auto& splitter = update.custom().splitter();
if (splitter.max_order_qty() > 0) {
me->clip = splitter.max_order_qty();
}
me->exposed_qty = splitter.exposed_qty();
if (splitter.has_child()) {
update_child_template(algo());
new_child_def = &splitter.child();
}
}
me->target_qty = update.quantity();
sync_orders();
if (me->exposed_qty < me->target_qty) {
algo().cancel("Exposed quantity must be greater than or equal to target quantity");
return;
}
sync_orders(new_child_def);
}
void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) {
for (auto& child : me->orders) {
@@ -110,10 +121,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
@@ -124,6 +131,8 @@ void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector<MinM
uint32_t irreducible = 0;
for (const auto& mm : constraints) irreducible += mm.min;
// total > overfills ?
auto citer = constraints.begin();
while(total > 0) {
auto current = std::min(total, clip);
@@ -170,8 +179,18 @@ void SplitterAlgo::terminate() const {
}
me->is_terminated = true;
}
void SplitterAlgo::update_child_template(IBeforeLaunchAlgoServices& ibl) {
// Create child template
auto child_def = ibl.definition();
bts_assert(child_def.has_custom_def());
bts_assert(child_def.custom_def().has_splitter());
auto& splitter = child_def.custom_def().splitter();
*child_def.mutable_custom_def() = splitter.child();
me->child_template = &ibl.make_child_template(child_def);
}
void SplitterAlgo::sync_orders() {
void SplitterAlgo::sync_orders(const api::orderman::CustomOrderDefinition* new_child_def) {
if (me->is_terminated) return;
if (is_complete()) {
terminate();
@@ -203,10 +222,14 @@ void SplitterAlgo::sync_orders() {
// Check the current order's quantity and modify it to match desired qty
if (oiter->state.status == ChildStatus::WORKING) {
if (oiter->state.price != price ||
oiter->state.quantity != qty) {
oiter->state.quantity != qty ||
new_child_def != nullptr) {
Update update;
update.set_quantity(qty.value());
update.set_price(price.value());
if (new_child_def != nullptr) {
*update.mutable_custom() = *new_child_def;
}
algo().update_child(oiter->key, update);
}
}
@@ -45,10 +45,16 @@ public:
private:
IAlgoServices& algo() const;
void sync_orders();
void sync_orders(const api::orderman::CustomOrderDefinition* new_child_def = nullptr);
bool is_complete() const;
void terminate() const;
void update_child_template(IBeforeLaunchAlgoServices&);
};
// exposed for unit testing purposes.
struct MinMax {uint32_t min, max;};
void compute_desired_sizes(uint32_t , uint32_t ,
const std::vector<MinMax>& , std::vector<Quantity>* );
}}} //namespace
@@ -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 <ctime>
#include <optional>
#include <variant>
#include <tuple>
#include <variant>
#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<struct ExpectedCountTag,uint32_t>;
using ExpectedCount = utils::ValueWrapper<struct ExpectedCountTag, uint32_t>;
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<MinMax> constraints(rand() % 10);
std::vector<Quantity> 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<SimpleCase> 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<SimpleCase> 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,41 +132,41 @@ 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();
REQUIRE(filled == state.total_fills().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;
REQUIRE(filled == state.total_fills().value());
// Fills should never cause new orders to get added
@@ -130,8 +174,7 @@ TEST_CASE("completes on target", "[algo][splitter]") {
if (filled >= tcase.target.value()) {
REQUIRE(state.state() == AlgoState::COMPLETED);
break;
}
else {
} else {
REQUIRE(state.state() == AlgoState::RUNNING);
}
}
@@ -146,30 +189,22 @@ TEST_CASE("updates", "[algo][splitter]") {
std::vector<std::tuple<SimpleCase, std::vector<uint32_t>, 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);
@@ -180,15 +215,15 @@ 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());
}
}
uint32_t fills = 0;
for(auto q : std::get<1>(tcase)) {
state.fill({}, Quantity {q});
for (auto q : std::get<1>(tcase)) {
state.fill({}, Quantity{q});
fills += q;
}
@@ -204,9 +239,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());
}
@@ -224,21 +261,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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
@@ -250,12 +289,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<uint32_t> actual, expected {100,100,50,50};
for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); }
std::set<uint32_t> actual, expected{100, 100, 50, 50};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
REQUIRE(state.total_fills().value() == 200);
@@ -272,23 +315,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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;
@@ -299,12 +344,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<uint32_t> actual, expected {100,100,65,35};
for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); }
std::set<uint32_t> actual, expected{100, 100, 65, 35};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
}
@@ -319,23 +368,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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;
@@ -346,11 +397,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<uint32_t> actual, expected {66,55,45,35};
for (const auto& order : state.child_orders()) { actual.insert(order.cstate.quantity.value()); }
std::set<uint32_t> actual, expected{66, 55, 45, 35};
for (const auto &order : state.child_orders()) {
actual.insert(order.cstate.quantity.value());
}
REQUIRE(actual == expected);
}
@@ -364,17 +419,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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});
@@ -388,11 +445,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();
}
}
@@ -409,17 +465,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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});
@@ -445,41 +503,45 @@ 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<SplitterAlgo> state(def, some_price, Quantity {80});
AlgoTestDriver<SplitterAlgo> 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;
update.set_price(state.price().value());
update.set_quantity(state.quantity().value());
update.set_quantity(65);
update.mutable_custom()->mutable_splitter()->set_exposed_qty(65);
// 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);
}
@@ -488,23 +550,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<SplitterAlgo> state(def, some_price, Quantity {300});
AlgoTestDriver<SplitterAlgo> 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());
@@ -513,7 +577,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);
@@ -523,12 +587,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
} // namespace test
} // namespace orderman
} // namespace bts