SP-87 Splitter algo
This commit is contained in:
1 parent
226fcc279b
commit
0dcdbd540e
9 files changed
+800
-5
No files matched your search
@@ -32,3 +32,4 @@ trace_data/
|
||||
/src/lib/orderman/perf_analysis/last_pretty_log.csv
|
||||
/src/lib/orderman/perf_analysis/log.csv
|
||||
/doc/user/public/*
|
||||
.ionide
|
||||
@@ -101,6 +101,7 @@ message CustomOrderDefinition
|
||||
OCOOrderDefinition oco = 25;
|
||||
PricerOrderDefinition pricer = 26;
|
||||
CompositeOrderDefinition composite = 27;
|
||||
SplitterOrderDefinition splitter = 29;
|
||||
|
||||
DynamicOrderDefinition dynamic = 1000;
|
||||
}
|
||||
@@ -204,6 +205,12 @@ message CompositeOrderDefinition {
|
||||
uint32 algo_template_key = 1;
|
||||
CustomOrderDefinition definition = 2;
|
||||
}
|
||||
message SplitterOrderDefinition {
|
||||
uint32 max_order_qty = 1;
|
||||
uint32 exposed_qty = 2;
|
||||
// uint32 target_qty = 3;
|
||||
CustomOrderDefinition child = 10;
|
||||
}
|
||||
message DynamicOrderDefinition {
|
||||
string dynamic_order_type = 1;
|
||||
map<string, DynamicOrderParam> order_params = 2;
|
||||
@@ -253,9 +260,10 @@ message OrderState {
|
||||
TriggerOrderState trigger = 21;
|
||||
CoverOrderState cover = 22;
|
||||
OffsetOrderState offset = 24;
|
||||
QueueHolderState queue_holder = 30;
|
||||
OCOOrderState oco = 25;
|
||||
PricerOrderState pricer = 26;
|
||||
QueueHolderState queue_holder = 30;
|
||||
SplitterOrderState splitter = 31;
|
||||
|
||||
DynamicOrderState dynamic = 255; // do not use anything bigger than 255, or things will break
|
||||
}
|
||||
@@ -334,6 +342,7 @@ message OCOOrderState {}
|
||||
message PricerOrderState {}
|
||||
message DynamicOrderState {}
|
||||
message QueueHolderState {}
|
||||
message SplitterOrderState {}
|
||||
|
||||
enum OrderStatus {
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ class IntPrice {
|
||||
#endif
|
||||
|
||||
public:
|
||||
IntPrice() : _mantissa(0), _exponent(127) {/*nan*/}
|
||||
IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {}
|
||||
IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {}
|
||||
constexpr IntPrice() : _mantissa(0), _exponent(127) {/*nan*/}
|
||||
constexpr IntPrice(int64_t mantissa, int8_t exponent) : _mantissa(mantissa), _exponent(exponent) {}
|
||||
constexpr IntPrice(const IntPrice& other) : _mantissa(other._mantissa), _exponent(other._exponent) {}
|
||||
|
||||
bool operator==(const IntPrice& other) const {
|
||||
if(_exponent == other._exponent) {
|
||||
|
||||
@@ -77,7 +77,7 @@ public:
|
||||
extern PriceSize NO_MARKET;
|
||||
static_assert(sizeof(PriceSize) == 16, "PriceSize should be small");
|
||||
|
||||
template <uint8_t PRECISION>
|
||||
template <uint8_t PRECISION=11>
|
||||
PriceSize PriceSize_from_double(double price, Size size) {
|
||||
if (size == 0 || os::is_nan(price)) return NO_MARKET;
|
||||
return { IntPrice_from_double<PRECISION>(price), size };
|
||||
|
||||
@@ -153,6 +153,9 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
case api::orderman::OrderState::kDynamic:
|
||||
result.mutable_dynamic();
|
||||
break;
|
||||
case api::orderman::OrderState::kSplitter:
|
||||
result.mutable_splitter();
|
||||
break;
|
||||
case api::orderman::OrderState::CUSTOM_NOT_SET:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "lib/orderman/orders/algo/algo_orders/TopAlgo.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/IcebergAlgo.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/LeanAlgo.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/SplitterAlgo.h"
|
||||
#include "lib/orderman/orders/book_algos/OCOOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/OffsetOrder.h"
|
||||
#include "lib/orderman/orders/book_algos/PricerOrder.h"
|
||||
@@ -208,6 +209,10 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk,
|
||||
case CustomOrderDefinition::kComposite:
|
||||
order_ptr.reset(new orderman::CompositeOrder());
|
||||
break;
|
||||
case CustomOrderDefinition::kSplitter:
|
||||
order_ptr.reset(
|
||||
new orderman::AlgoWrapperOrder(std::make_unique<splitter::SplitterAlgo>()));
|
||||
break;
|
||||
case CustomOrderDefinition::CUSTOM_NOT_SET:
|
||||
bts_assert(false);
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
#include "SplitterAlgo.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/orderman/order_factory/IOrderFactory.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
||||
#include "lib/orderman/orders/algo/IAlgoServices.h"
|
||||
// #include "google/btree/btree_map.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
namespace splitter {
|
||||
|
||||
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(); }
|
||||
};
|
||||
|
||||
struct SplitterAlgo::Impl {
|
||||
IAlgoServices* algo = nullptr;
|
||||
ChildTemplate* child_template = nullptr;
|
||||
|
||||
uint32_t clip = 0;
|
||||
uint32_t exposed_qty = 0;
|
||||
uint32_t target_qty = 0;
|
||||
|
||||
bool is_terminated = false;
|
||||
|
||||
std::vector<ChildOrderState> orders;
|
||||
};
|
||||
|
||||
SplitterAlgo::SplitterAlgo() : me(new Impl()) { }
|
||||
SplitterAlgo::~SplitterAlgo() {}
|
||||
|
||||
OrderOpResult SplitterAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) {
|
||||
// Validate and store parameters
|
||||
|
||||
auto& def = ibl.definition();
|
||||
bts_assert(def.has_custom_def());
|
||||
bts_assert(def.custom_def().has_splitter());
|
||||
auto& splitter = def.custom_def().splitter();
|
||||
|
||||
me->clip = splitter.max_order_qty();
|
||||
me->exposed_qty = splitter.exposed_qty();
|
||||
if (me->clip <= 0 || me->exposed_qty <= 0) {
|
||||
return OrderOpResult{ORDER_ERROR_INVALID_ORDER_DEFINITON,
|
||||
"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);
|
||||
|
||||
me->orders.reserve(me->exposed_qty / me->clip + 1);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void SplitterAlgo::do_launch(IAlgoServices& ias) {
|
||||
me->algo = &ias;
|
||||
me->target_qty = ias.quantity().value();
|
||||
// launches initial child orders
|
||||
sync_orders();
|
||||
}
|
||||
void SplitterAlgo::do_launch_attached(IAlgoServices &, OrderKey, ChildState) {
|
||||
//todo
|
||||
}
|
||||
void SplitterAlgo::do_update(const api::orderman::Update& update) {
|
||||
// Update # child orders
|
||||
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();
|
||||
}
|
||||
me->target_qty = update.quantity();
|
||||
|
||||
sync_orders();
|
||||
}
|
||||
void SplitterAlgo::on_child_change(OrderKey key, ChildState const & state) {
|
||||
for (auto& child : me->orders) {
|
||||
if (child.key == key) {
|
||||
child.state = state;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sync_orders();
|
||||
}
|
||||
|
||||
struct MinMax {
|
||||
uint32_t min, max;
|
||||
};
|
||||
|
||||
// TEST case:
|
||||
// total = 300, clip = 100
|
||||
// constraints: 50,50,50,50
|
||||
void compute_desired_sizes(uint32_t total, uint32_t clip, const std::vector<MinMax>& constraints, std::vector<Quantity>* results) {
|
||||
bts_assert(results);
|
||||
results->clear();
|
||||
|
||||
uint32_t irreducible = 0;
|
||||
for (const auto& mm : constraints) irreducible += mm.min;
|
||||
|
||||
auto citer = constraints.begin();
|
||||
while(total > 0) {
|
||||
auto current = std::min(total, clip);
|
||||
// fit to constraint
|
||||
if (citer != constraints.end()) {
|
||||
irreducible -= citer->min;
|
||||
if (current > citer->max) current = citer->max;
|
||||
if (current < citer->min) current = citer->min;
|
||||
|
||||
// I can't be higher than (total - irreducible)
|
||||
if (current > (total - irreducible)) current = (total-irreducible);
|
||||
citer++;
|
||||
}
|
||||
results->push_back(Quantity{current});
|
||||
total -= current;
|
||||
}
|
||||
}
|
||||
|
||||
void get_constraints(const std::vector<ChildOrderState>& orders, std::vector<MinMax>* constraints) {
|
||||
bts_assert(constraints);
|
||||
for (const auto& order : orders) {
|
||||
//Note: to maintain queue position set max to current quantity. This will cause more orders to be added to the end if size increases
|
||||
constraints->push_back(MinMax {
|
||||
order.state.total_fills.value(),
|
||||
order.state.status == ChildStatus::WORKING ?
|
||||
std::numeric_limits<uint32_t>::max() : order.state.total_fills.value()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bool SplitterAlgo::is_complete() const {
|
||||
uint32_t fills = 0;
|
||||
for (const auto& o : me->orders) { fills += o.state.total_fills.value(); }
|
||||
return fills >= me->target_qty;
|
||||
}
|
||||
|
||||
void SplitterAlgo::terminate() const {
|
||||
algo().complete();
|
||||
//?? Do we need to clean up children?
|
||||
for (const auto& o : me->orders) {
|
||||
if (o.state.status == ChildStatus::WORKING) {
|
||||
algo().cancel_child(o.key);
|
||||
}
|
||||
}
|
||||
me->is_terminated = true;
|
||||
}
|
||||
|
||||
void SplitterAlgo::sync_orders() {
|
||||
if (me->is_terminated) return;
|
||||
if (is_complete()) {
|
||||
terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
std::sort(me->orders.begin(), me->orders.end(), [](auto& a, auto b) { return b.min_qty() < a.min_qty(); });
|
||||
std::vector<MinMax> constraints;
|
||||
get_constraints(me->orders, &constraints);
|
||||
|
||||
std::vector<Quantity> desired;
|
||||
compute_desired_sizes(me->exposed_qty, me->clip, constraints, &desired);
|
||||
|
||||
auto oiter = me->orders.begin();
|
||||
// iterate to the current number of orders, don't consider orders that we're adding
|
||||
auto order_count = me->orders.size();
|
||||
auto price = algo().price();
|
||||
for (const auto& qty : desired) {
|
||||
if (order_count == 0) {
|
||||
// Ran out of existing orders, time to add more
|
||||
auto key = algo().launch_child(*me->child_template, price, qty);
|
||||
// Assume the order begins successfully. Hopefully we get back some sort of NACK otherwise
|
||||
me->orders.push_back({ key, {} });
|
||||
me->orders.back().state.price = price;
|
||||
me->orders.back().state.quantity = Quantity { qty };
|
||||
me->orders.back().state.status = ChildStatus::WORKING;
|
||||
}
|
||||
else {
|
||||
// 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) {
|
||||
Update update;
|
||||
update.set_quantity(qty.value());
|
||||
update.set_price(price.value());
|
||||
algo().update_child(oiter->key, update);
|
||||
}
|
||||
}
|
||||
oiter++;
|
||||
order_count -= 1;
|
||||
}
|
||||
}
|
||||
// cancel any orders past desired
|
||||
if (desired.size() < me->orders.size()) {
|
||||
for (size_t x = desired.size(); x < me->orders.size(); ++x) {
|
||||
auto& order= me->orders[x];
|
||||
if (order.state.status == ChildStatus::WORKING) {
|
||||
order.state.status = ChildStatus::CANCELED;
|
||||
algo().cancel_child(order.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IAlgoServices& SplitterAlgo::algo() const { bts_assert(me->algo); return *me->algo; }
|
||||
|
||||
}}} //namespace
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "lib/orderman/orders/algo/IAlgoOrder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
namespace splitter {
|
||||
|
||||
class SplitterAlgo : public IAlgoOrder
|
||||
{
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> me;
|
||||
|
||||
public:
|
||||
SplitterAlgo();
|
||||
~SplitterAlgo();
|
||||
|
||||
OrderOpResult do_before_launch(IBeforeLaunchAlgoServices &) override;
|
||||
void do_launch(IAlgoServices &) override;
|
||||
void do_launch_attached(IAlgoServices &, OrderKey, ChildState) override;
|
||||
void do_update(api::orderman::Update const &) override;
|
||||
void on_child_change(OrderKey, ChildState const &) override;
|
||||
|
||||
private:
|
||||
IAlgoServices& algo() const;
|
||||
|
||||
void sync_orders();
|
||||
bool is_complete() const;
|
||||
void terminate() const;
|
||||
};
|
||||
|
||||
|
||||
}}} //namespace
|
||||
@@ -0,0 +1,526 @@
|
||||
|
||||
#include "catch/catch.hpp"
|
||||
#include "../SplitterAlgo.h"
|
||||
#include "AlgoTestDriver.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <tuple>
|
||||
|
||||
using namespace std;
|
||||
using namespace bts::orderman::splitter;
|
||||
using namespace bts::api::orderman;
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
namespace test {
|
||||
|
||||
/*
|
||||
|
||||
- max/exposed, assert correct # of orders on the market
|
||||
- completes on target_qty reached
|
||||
- completes on target_qty exceeded
|
||||
- send fills, check order state
|
||||
- send fills, then change target_qty
|
||||
|
||||
*/
|
||||
|
||||
constexpr auto some_price = md::IntPrice { 128000, -3 };
|
||||
|
||||
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 result;
|
||||
result.expected_orders = expected;
|
||||
result.target = Quantity {target};
|
||||
result.sod.set_max_order_qty(max_size);
|
||||
result.sod.set_exposed_qty(exposed);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
||||
for (const auto& tcase : cases) {
|
||||
auto expected = tcase.expected_orders.value();
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
*def.mutable_custom_def()->mutable_splitter() = tcase.sod;
|
||||
AlgoTestDriver<SplitterAlgo> state(def, some_price, tcase.target);
|
||||
|
||||
REQUIRE(state.before_launch().ok());
|
||||
state.launch();
|
||||
REQUIRE(state.child_orders().size() == expected);
|
||||
REQUIRE(state.state() == AlgoState::RUNNING);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
for (const auto& tcase : cases) {
|
||||
auto expected = tcase.expected_orders.value();
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
*def.mutable_custom_def()->mutable_splitter() = tcase.sod;
|
||||
AlgoTestDriver<SplitterAlgo> state(def, some_price, tcase.target);
|
||||
|
||||
REQUIRE(state.before_launch().ok());
|
||||
state.launch();
|
||||
REQUIRE(state.child_orders().size() == expected);
|
||||
REQUIRE(state.state() == AlgoState::RUNNING);
|
||||
|
||||
SECTION("Fill completely") {
|
||||
auto orders = state.child_orders();
|
||||
// Fill the order
|
||||
size_t filled = 0;
|
||||
size_t iterations = 0;
|
||||
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);
|
||||
filled += order.cstate.quantity.value();
|
||||
if (filled >= tcase.target.value()) {
|
||||
REQUIRE(state.state() == AlgoState::COMPLETED);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
REQUIRE(state.state() == AlgoState::RUNNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SECTION("fill one by one") {
|
||||
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) {
|
||||
++iterations;
|
||||
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});
|
||||
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 {
|
||||
REQUIRE(state.state() == AlgoState::RUNNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("updates", "[algo][splitter]") {
|
||||
// Start in state 0, update to state 1
|
||||
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)
|
||||
});
|
||||
/// 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)
|
||||
});
|
||||
|
||||
|
||||
for (const auto& tcase : cases) {
|
||||
auto case_init = std::get<0>(tcase);
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
*def.mutable_custom_def()->mutable_splitter() = case_init.sod;
|
||||
AlgoTestDriver<SplitterAlgo> state(def, some_price, case_init.target);
|
||||
|
||||
REQUIRE(state.before_launch().ok());
|
||||
state.launch();
|
||||
REQUIRE(state.state() == AlgoState::RUNNING);
|
||||
REQUIRE(state.child_orders().size() == case_init.expected_orders.value());
|
||||
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});
|
||||
}
|
||||
|
||||
Update update;
|
||||
auto case_next = std::get<2>(tcase);
|
||||
update.set_price(some_price.price());
|
||||
update.set_quantity(case_next.target.value());
|
||||
*def.mutable_custom_def()->mutable_splitter() = case_next.sod;
|
||||
*update.mutable_custom() = def.custom_def();
|
||||
|
||||
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;
|
||||
REQUIRE(working == case_next.expected_orders.value());
|
||||
for (const auto& o : state.child_orders()) {
|
||||
if (o.cstate.status == ChildStatus::WORKING) {
|
||||
REQUIRE(o.cstate.quantity.value() <= case_next.sod.exposed_qty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
TEST_CASE("Reduction with fills", "[algo][splitter]") {
|
||||
// Fills represent a floor on how much a trade can be reduced
|
||||
// IF we have expose:400, clip: 100 we get
|
||||
// 100,100,100,100
|
||||
// Receive fills
|
||||
// 100/50, 100/50, 100/50, 100/50
|
||||
// Change expose to 300
|
||||
// naive: 100, 100, 100 but we *can't* reduce the last to 0
|
||||
// expected: 100, 100, 50, 50
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
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});
|
||||
|
||||
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); }
|
||||
|
||||
// Add fills
|
||||
for (const auto& order : state.child_orders()) {
|
||||
state.fill(order.key, {}, Quantity {50});
|
||||
}
|
||||
|
||||
// Reduce exposed quantity
|
||||
Update update;
|
||||
update.set_quantity(300);
|
||||
update.mutable_custom()->mutable_splitter()->set_exposed_qty(300);
|
||||
state.do_update(update);
|
||||
|
||||
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(); }
|
||||
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()); }
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Reduction with fills 2", "[algo][splitter]") {
|
||||
// Fills represent a floor on how much a trade can be reduced
|
||||
// IF we have expose:400, clip: 100 we get
|
||||
// 100,100,100,100
|
||||
// Receive fills
|
||||
// 100/35, 100/45, 100/55, 100/65
|
||||
// Change expose to 300
|
||||
// naive: 100, 100, 100 but we *can't* reduce the last to 0
|
||||
// expected: 100, 100, 65, 35
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
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});
|
||||
|
||||
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); }
|
||||
|
||||
// Add fills
|
||||
state.fill({}, Quantity {35});
|
||||
state.fill({}, Quantity {45});
|
||||
state.fill({}, Quantity {55});
|
||||
state.fill({}, Quantity {65});
|
||||
|
||||
// Reduce exposed quantity
|
||||
Update update;
|
||||
update.set_quantity(300);
|
||||
update.mutable_custom()->mutable_splitter()->set_exposed_qty(300);
|
||||
state.do_update(update);
|
||||
|
||||
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(); }
|
||||
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()); }
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Reduction with fills 3", "[algo][splitter]") {
|
||||
// Fills represent a floor on how much a trade can be reduced
|
||||
// IF we have expose:400, clip: 100 we get
|
||||
// 100,100,100,100
|
||||
// Receive fills
|
||||
// 100/35, 100/45, 100/55, 100/65
|
||||
// Change expose to 201
|
||||
// naive: 100, 100, 1 but we *can't* reduce the last to 0
|
||||
// expected: 66, 55, 45, 35
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
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});
|
||||
|
||||
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); }
|
||||
|
||||
// Add fills
|
||||
state.fill({}, Quantity {35});
|
||||
state.fill({}, Quantity {45});
|
||||
state.fill({}, Quantity {55});
|
||||
state.fill({}, Quantity {65});
|
||||
|
||||
// Reduce exposed quantity
|
||||
Update update;
|
||||
update.set_quantity(201);
|
||||
update.mutable_custom()->mutable_splitter()->set_exposed_qty(201);
|
||||
state.do_update(update);
|
||||
|
||||
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(); }
|
||||
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()); }
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("reduce to completion", "[algo][splitter]") {
|
||||
// Fills represent a floor on how much a trade can be reduced
|
||||
// IF we have expose:400, clip: 100 we get
|
||||
// 100,100,100,100
|
||||
// Receive fills
|
||||
// 100/100, 100/100, 100/0, 100/00
|
||||
// Change expose to 200
|
||||
// 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();
|
||||
splitter->set_max_order_qty(100);
|
||||
splitter->set_exposed_qty(400);
|
||||
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); }
|
||||
|
||||
// Add fills
|
||||
state.fill({}, Quantity{100});
|
||||
state.fill({}, Quantity{100});
|
||||
|
||||
// Reduce exposed quantity
|
||||
Update update;
|
||||
update.set_quantity(200);
|
||||
def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(200);
|
||||
state.do_update(update);
|
||||
|
||||
REQUIRE(state.state() == AlgoState::COMPLETED);
|
||||
size_t total = 0;
|
||||
for (const auto& order : state.child_orders()) {
|
||||
if (order.cstate.status == ChildStatus::WORKING) {
|
||||
total += order.cstate.quantity.value();
|
||||
}
|
||||
else {
|
||||
total += order.cstate.total_fills.value();
|
||||
}
|
||||
}
|
||||
REQUIRE(total == 200);
|
||||
}
|
||||
|
||||
TEST_CASE("reduce to completion 2", "[algo][splitter]") {
|
||||
// Fills represent a floor on how much a trade can be reduced
|
||||
// IF we have expose:400, clip: 100 we get
|
||||
// 100,100,100,100
|
||||
// Receive fills
|
||||
// 100/100, 100/100, 100/0, 100/00
|
||||
// Change expose to 200
|
||||
// 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();
|
||||
splitter->set_max_order_qty(100);
|
||||
splitter->set_exposed_qty(400);
|
||||
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); }
|
||||
|
||||
// Add fills
|
||||
state.fill({}, Quantity{100});
|
||||
state.fill({}, Quantity{50});
|
||||
state.fill({}, Quantity{50});
|
||||
|
||||
// Reduce exposed quantity
|
||||
Update update;
|
||||
update.set_quantity(200);
|
||||
state.do_update(update);
|
||||
|
||||
REQUIRE(state.state() == AlgoState::COMPLETED);
|
||||
}
|
||||
|
||||
TEST_CASE("Async Fills", "[algo][splitter]") {
|
||||
// OK, NOW it gets tricky because we're going to test the asyncronous stuff
|
||||
|
||||
// Start with 4 orders size 20 outstanding (80 total exposed)
|
||||
// Change exposure to 64 so one of the four will be reduced to 5
|
||||
// Before the change is confirmed fill the one that is reducing by 7
|
||||
// which would constitute an overfill
|
||||
// Algo must reduce a second order to compensate
|
||||
|
||||
api::orderman::OrderDefinition def;
|
||||
def.set_side(api::SIDE_BUY);
|
||||
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});
|
||||
|
||||
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); }
|
||||
|
||||
// Set up some instant fills
|
||||
bool applied_fills = false;
|
||||
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});
|
||||
});
|
||||
|
||||
Update update;
|
||||
update.set_price(state.price().value());
|
||||
update.set_quantity(state.quantity().value());
|
||||
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
|
||||
state.do_update(update);
|
||||
|
||||
// expected result:
|
||||
// 7 fills, exposed_qty 65 = 58 active
|
||||
uint32_t active = 0;
|
||||
for (const auto& o : state.child_orders()) active += o.active_qty();
|
||||
REQUIRE(active == 58);
|
||||
}
|
||||
|
||||
TEST_CASE("Increase then decrease", "[algo][splitter]") {
|
||||
// Start with 3 orders of size 100
|
||||
// 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
|
||||
// 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();
|
||||
splitter->set_max_order_qty(100);
|
||||
splitter->set_exposed_qty(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); }
|
||||
|
||||
Update update;
|
||||
update.set_price(state.price().value());
|
||||
update.set_quantity(state.quantity().value());
|
||||
update.mutable_custom()->mutable_splitter()->set_exposed_qty(400);
|
||||
|
||||
// Set up some instant fills
|
||||
bool applied_second_update = false;
|
||||
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);
|
||||
state.do_update(update);
|
||||
}
|
||||
});
|
||||
|
||||
state.do_update(update);
|
||||
|
||||
// expected result:
|
||||
// 350 exposed, NOT 450
|
||||
uint32_t quantity = 0;
|
||||
for (const auto& o : state.child_orders()) quantity += o.active_qty();
|
||||
REQUIRE(quantity == 350);
|
||||
}
|
||||
|
||||
|
||||
}}}//namespace
|
||||
Reference in new issue
Block a user