SP-31 Dorman auto-scratch

This commit is contained in:
Eric Harding committed 2019-07-08 10:42:43 -04:00
1 parent 7e1463683f
commit 4ecbfe5f5b
5 files changed
+148 -46

No files matched your search

@@ -75,7 +75,7 @@ OrderOpResult DormanAlgo::do_before_launch(IBeforeLaunchAlgoServices& ibl) {
if (ibl.is_attaching()) {
return OrderOpResult{ORDER_ERROR_OP_NOT_SUPPORTED,
"Splitter does not support attach."};
"This algo does not support attach."};
}
return {/*ok*/};
@@ -95,10 +95,6 @@ void DormanAlgo::do_update(const api::orderman::Update& update) {
LOG(OrderMan, Diag, "Dorman order {} receiving update: {}",
algo().order_key().value(), utils::pb::to_string(update));
if (me->fills >= update.quantity()) {
algo().complete();
}
if (update.has_custom()) {
REQUIRE(update.custom().has_dynamic(), "Update cannot change order type");
auto params = to_dorman_params(update.custom().dynamic());
@@ -115,10 +111,14 @@ void DormanAlgo::do_update(const api::orderman::Update& update) {
update_child_template(algo(), child.value());
}
}
if (me->fills >= update.quantity()) {
algo().complete();
}
}
void DormanAlgo::on_child_change(OrderKey k, const ChildState& state) {
REQUIRE(k == me->child_id, "Unexpected update from illigitimate child");
void DormanAlgo::on_child_change(OrderKey key, const ChildState& state) {
REQUIRE(key == me->child_id, "Unexpected update from illigitimate child");
REQUIRE(state.total_fills.value() >= me->child_state.total_fills.value(), "Unexpected fill decrease.");
auto delta = state.total_fills.value() - me->child_state.total_fills.value();
@@ -128,6 +128,11 @@ void DormanAlgo::on_child_change(OrderKey k, const ChildState& state) {
}
me->child_state = state;
if (me->params.scratch &&
me->fills >= me->params.scratch.value().max_overfill) {
do_scratch();
}
if (me->fills >= algo().quantity().value()) {
algo().complete();
}
@@ -241,6 +246,21 @@ void DormanAlgo::pause_until(TimeStamp) {
// check paused_time in on_timer
}
void DormanAlgo::do_scratch() {
// When the # of fills exceeds the max overfill quantity
// We enter fills opposite fills at the fill price and terminate
OrderDefinition def = me->splitter_definition;
def.set_side(securities::other_side(me->side));
def.mutable_custom_def()->mutable_splitter()->set_exposed_qty(me->fills);
auto price = me->child_state.last_price;
auto scratch_id = algo().launch_child(def, price, Quantity { me->fills }, false);
algo().detach_child(scratch_id);
algo().complete();
}
//
//
// Parameter parsing and validation
@@ -79,6 +79,8 @@ private:
bool child_active() const;
void pause_until(TimeStamp);
void do_scratch();
};
template <class OK,class ERR>
@@ -59,6 +59,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter {
AlgoState state;
std::vector<ChildOrder> orders;
std::vector<ChildOrder> detached_orders;
std::map<SecurityKey, Book> books;
std::map<SecurityKey, bool> book_subs;
@@ -68,6 +69,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter {
std::vector<std::function<void(OrderKey)>> on_child_launching;
std::vector<std::function<void(const ChildOrder&)>> on_child_changing;
std::vector<std::function<void(const ChildOrder&, const ChildOrder&)>> on_child_changed;
bool allow_overfill = false;
Impl(IAlgoOrder& o) : algo(o) {}
@@ -165,6 +167,10 @@ const std::vector<ChildOrder>& AlgoTestDriverBase::child_orders() const {
return me->orders;
}
const std::vector<ChildOrder>& AlgoTestDriverBase::detached_orders() const {
return me->detached_orders;
}
AlgoState AlgoTestDriverBase::state() const {
return me->state;
}
@@ -173,7 +179,7 @@ void AlgoTestDriverBase::fill(OrderKey k, Price price, Quantity qty) {
auto guard = me->recusion_guard();
for (auto& child : me->orders) {
if (child.key == k) {
REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value());
if (!me->allow_overfill) REQUIRE(child.cstate.quantity.value() - child.cstate.total_fills.value() >= qty.value());
for (const auto& cb : me->on_child_changing) cb(child);
auto old = child;
@@ -237,6 +243,9 @@ void AlgoTestDriverBase::on_child_changing(std::function<void(const ChildOrder&)
void AlgoTestDriverBase::on_child_changed(std::function<void(const ChildOrder&, const ChildOrder&)>&& cb) {
me->on_child_changed.push_back(cb);
}
bool AlgoTestDriverBase::allow_overfill() const { return me->allow_overfill; }
void AlgoTestDriverBase::allow_overfill(bool value) { me->allow_overfill = value; }
//
// Utility
@@ -369,9 +378,17 @@ void AlgoTestDriverBase::cancel_child(OrderKey key) {
}
}
void AlgoTestDriverBase::detach_child(OrderKey key) {
std::remove_if(me->orders.begin(), me->orders.end(), [key](const auto& order) { return order.key == key; });
for (auto iter = me->orders.begin(); iter != me->orders.end(); ++iter) {
auto order = *iter;
if (order.key == key) {
me->detached_orders.push_back(order);
me->orders.erase(iter);
break;
}
}
}
void AlgoTestDriverBase::detach_all_children() {
for (const auto& order : me->orders) me->detached_orders.push_back(order);
me->orders.clear();
}
void AlgoTestDriverBase::cancel(std::string const &) {
@@ -73,6 +73,7 @@ class AlgoTestDriverBase : private IAlgoServices {
//
// Inspection methods
const std::vector<ChildOrder>& child_orders() const;
const std::vector<ChildOrder>& detached_orders() const;
AlgoState state() const;
// Fills for this specific order
@@ -84,7 +85,10 @@ class AlgoTestDriverBase : private IAlgoServices {
void on_child_changing(std::function<void(const ChildOrder&)>&&);
void on_child_changed(std::function<void(const ChildOrder&, const ChildOrder&)>&&);
//
// Tweak internal REQUIRE
bool allow_overfill() const;
void allow_overfill(bool=true);
private:
@@ -7,6 +7,7 @@
using namespace std;
using namespace bts::orderman::dorman;
using namespace bts::api::orderman;
namespace bts {
namespace orderman {
@@ -113,22 +114,31 @@ struct ExpectedSize {
};
constexpr auto some_price = md::IntPrice { 128000, -3 };
size_t child_exposed(const std::vector<ChildOrder>& child_orders) {
template <class CB>
size_t count_x(const std::vector<ChildOrder>& child_orders, CB fn) {
return std::accumulate(
child_orders.begin(),
child_orders.end(), 0,
[](uint32_t acc, const auto& child) {
if (child.cstate.status != ChildStatus::CANCELED) {
REQUIRE(child.def);
return
acc +
child.def.value().custom_def().splitter().exposed_qty()
+ child.cstate.total_fills.value();
}
else return acc;
[&](uint32_t acc, const auto& child) {
return acc + fn(child);
});
}
size_t count_exposed(const std::vector<ChildOrder>& child_orders) {
return count_x(child_orders, [](const ChildOrder& child) {
REQUIRE(child.def);
if (child.cstate.status == ChildStatus::CANCELED) return 0u;
else return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value();
});
}
size_t count_fills(const std::vector<ChildOrder>& child_orders) {
return count_x(child_orders, [](const ChildOrder& child) {
return child.def.value().custom_def().splitter().exposed_qty() + child.cstate.total_fills.value();
});
}
TEST_CASE("dorman_order_size", "[algo][dorman]") {
// create a table of various sizes
@@ -173,7 +183,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.algo().exposed_qty().value() == s.expected);
REQUIRE(s.expected == child_exposed(state.child_orders()));
REQUIRE(s.expected == count_exposed(state.child_orders()));
}
}
SECTION("Reduce qty on market shrink (immidiate)") {
@@ -187,7 +197,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
REQUIRE(state.algo().exposed_qty() == Quantity(400));
// Check that orders were created
REQUIRE(400 == child_exposed(state.child_orders()));
REQUIRE(400 == count_exposed(state.child_orders()));
for(const auto& ch : state.child_orders()) {
REQUIRE(ch.cstate.quantity == state.quantity());
}
@@ -197,7 +207,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
state.update_book(book);
REQUIRE(state.algo().exposed_qty() == Quantity(250));
REQUIRE(
250 == child_exposed(state.child_orders()));
250 == count_exposed(state.child_orders()));
}
SECTION("Increase qty w/ delay when market grows") {
@@ -212,7 +222,7 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
// Check that orders were created
REQUIRE(
400 == child_exposed(state.child_orders()));
400 == count_exposed(state.child_orders()));
// INCREASE market
book.bid()[0] = { some_price, 1001 };
@@ -220,16 +230,16 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
// NOTE: that the orders do not follow the market instantly
// so this is still 400
REQUIRE(
400 == child_exposed(state.child_orders()));
400 == count_exposed(state.child_orders()));
// Still 400 after the timer ticks but not enough time has passed
state.advance_time(std::chrono::seconds());
REQUIRE(
400 == child_exposed(state.child_orders()));
400 == count_exposed(state.child_orders()));
state.advance_time(std::chrono::seconds(input.steps.trail_seconds));
REQUIRE(
600 == child_exposed(state.child_orders()));
600 == count_exposed(state.child_orders()));
}
SECTION("Respect maximum") {
@@ -245,12 +255,11 @@ TEST_CASE("dorman_order_size", "[algo][dorman]") {
// Check that orders were created
REQUIRE(
input.steps.maximum == child_exposed(state.child_orders()));
input.steps.maximum == count_exposed(state.child_orders()));
}
}
TEST_CASE("Dorman fills", "[algo][dorman]") {
AlgoParams input;
input.steps.step_table = {
{500, 250},
@@ -289,29 +298,79 @@ TEST_CASE("Dorman fills", "[algo][dorman]") {
// Test cases for transitions w/ fills
//
// target = 500
// send 5 orders of size 100
// market move, new target = 450
// reduce order #5 to 50
// Before we confirm the move
// - fill #4 for 80
// :: algo needs to shed another 30 Quantity
SECTION("Complete by update") {
// target = 500, exposed = 650
// Fill 400
// Change target to 350
// expect completion
AlgoTestDriver<DormanAlgo> state(def, some_price, Quantity {500});
auto secid = SecurityKey(state.security().id());
Book book(secid);
book.bid()[0] = { some_price, 1999 };
state.update_book(book);
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.algo().exposed_qty() == Quantity(650));
REQUIRE(state.quantity() == Quantity {500});
// ^ same but fill #2 for 80
/*
NOTES:
vector<order_size> = f(fills, target, clip)
=> feed into diff
*/
// Fill 400
state.fill(price, Quantity { 400 });
Update update;
update.set_quantity(350);
state.do_update(update);
REQUIRE(state.state() == AlgoState::COMPLETED);
}
}
TEST_CASE("Dorman overfill", "[algo][dorman]") {
SECTION("auto-scratch OFF") {
}
AlgoParams input;
input.steps.step_table = {
{500, 250},
{1000, 450},
{2000, 650},
{3500, 750},
};
input.steps.maximum = 750;
input.steps.trail_seconds = 2;
input.clip_limit = 100;
SECTION("auto-scratch ON") {
input.scratch = {{ 500 }};
}
SECTION("auto-scratch OFF") {
// both sections will run
input.scratch = std::nullopt;
}
api::orderman::OrderDefinition def;
def.set_side(api::SIDE_BUY);
def.mutable_custom_def()->mutable_dynamic()->CopyFrom(to_dynamic_order_params(input));
auto price = Price { some_price.price() };
AlgoTestDriver<DormanAlgo> state(def, some_price, Quantity {100});
state.allow_overfill();
auto secid = SecurityKey(state.security().id());
Book book(secid);
book.bid()[0] = { some_price, 1999 };
state.update_book(book);
REQUIRE(state.before_launch().ok());
state.launch();
REQUIRE(state.algo().exposed_qty() == Quantity(650));
REQUIRE(state.quantity() == Quantity {100});
REQUIRE(state.child_orders().size() == 1);
state.fill(state.child_orders().begin()->key, price, Quantity {500});
REQUIRE(state.state() == AlgoState::COMPLETED);
if (input.scratch) {
// check that we scratched
REQUIRE(state.detached_orders().size() > 0);
}
else {
// check that we *did not* scratch
REQUIRE(state.detached_orders().size() == 0);
}
}
TEST_CASE("Dorman profit-orders", "[algo][dorman]") {