SP-180, Lean Redeploying After Detach/Attach
This commit is contained in:
1 parent
bd0e697ea3
commit
5f75609d8d
6 files changed
+107
-43
No files matched your search
@@ -250,6 +250,7 @@ message OrderState {
|
||||
double price = 2;
|
||||
uint32 quantity = 3;
|
||||
uint32 filled_quantity=4;
|
||||
uint32 filled_at_attach_time_quantity = 34;
|
||||
double last_price = 5;
|
||||
double avg_price = 6;
|
||||
string last_msg = 7;
|
||||
|
||||
@@ -15,12 +15,13 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
using SynopsisPolicy = mercury::CacheAll<OrderStateUpdateEvent>;
|
||||
|
||||
OrderStateUpdateEvent()
|
||||
: key(null_order_key()), parent_key(null_order_key()) {}
|
||||
: key(null_order_key()), parent_key(null_order_key()) {}
|
||||
|
||||
OrderStateUpdateEvent(OrderKey k, OrderKey pk,
|
||||
std::shared_ptr<const api::orderman::OrderDefinition> const &def,
|
||||
api::orderman::OrderState const &state) {
|
||||
init(k, pk, def,state);
|
||||
OrderStateUpdateEvent(
|
||||
OrderKey k, OrderKey pk,
|
||||
std::shared_ptr<const api::orderman::OrderDefinition> const &def,
|
||||
api::orderman::OrderState const &state) {
|
||||
init(k, pk, def, state);
|
||||
}
|
||||
|
||||
void init(OrderKey k, OrderKey pk,
|
||||
@@ -35,6 +36,7 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
filled_quantity = state.filled_quantity();
|
||||
last_price = state.last_price();
|
||||
avg_price = state.avg_price();
|
||||
filled_at_attach_time_quantity = state.filled_at_attach_time_quantity();
|
||||
last_msg = state.last_msg();
|
||||
display_status = state.display_status();
|
||||
|
||||
@@ -55,8 +57,8 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
|
||||
OrderKey key;
|
||||
OrderKey parent_key;
|
||||
std::shared_ptr<const api::orderman::OrderDefinition> definition;
|
||||
|
||||
std::shared_ptr<const api::orderman::OrderDefinition> definition;
|
||||
|
||||
// common state
|
||||
api::orderman::OrderStatus status;
|
||||
double price;
|
||||
@@ -64,9 +66,10 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
uint32_t filled_quantity;
|
||||
double last_price;
|
||||
double avg_price;
|
||||
uint32_t filled_at_attach_time_quantity;
|
||||
std::string last_msg;
|
||||
api::orderman::OrderState::DisplayStatus display_status;
|
||||
|
||||
|
||||
api::orderman::OrderState::CustomCase custom;
|
||||
|
||||
// native order state
|
||||
@@ -86,11 +89,12 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
result.set_filled_quantity(filled_quantity);
|
||||
result.set_last_price(last_price);
|
||||
result.set_avg_price(avg_price);
|
||||
result.set_filled_at_attach_time_quantity(filled_at_attach_time_quantity);
|
||||
result.set_last_msg(last_msg);
|
||||
result.set_display_status(display_status);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
api::orderman::OrderState rebuild_state() const {
|
||||
api::orderman::OrderState result = quick_rebuild_state();
|
||||
switch (custom) {
|
||||
@@ -143,7 +147,7 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
break;
|
||||
case api::orderman::OrderState::kQueueHolder:
|
||||
result.mutable_queue_holder();
|
||||
break;
|
||||
break;
|
||||
case api::orderman::OrderState::kOco:
|
||||
result.mutable_oco();
|
||||
break;
|
||||
@@ -155,7 +159,7 @@ class OrderStateUpdateEvent : public mercury::Event<OrderStateUpdateEvent> {
|
||||
break;
|
||||
case api::orderman::OrderState::kProrata:
|
||||
result.mutable_prorata();
|
||||
break;
|
||||
break;
|
||||
case api::orderman::OrderState::kDynamic:
|
||||
result.mutable_dynamic();
|
||||
break;
|
||||
|
||||
@@ -45,7 +45,8 @@ struct AlgoWrapperOrder::PImpl {
|
||||
using Edge = std::pair<Status, Status>;
|
||||
using Action = std::function<void()>;
|
||||
|
||||
PImpl(std::unique_ptr<IAlgoOrder> &&algo) : algo(std::move(algo)) {}
|
||||
PImpl(std::unique_ptr<IAlgoOrder> &&algo)
|
||||
: algo(std::move(algo)), _my_key(null_order_key()) {}
|
||||
|
||||
std::unique_ptr<IAlgoOrder> algo;
|
||||
Status status = Status::INACTIVE;
|
||||
@@ -71,6 +72,7 @@ struct AlgoWrapperOrder::PImpl {
|
||||
std::optional<api::orderman::Update> pending_update;
|
||||
std::optional<std::pair<OrderStatus, std::string>> pending_terminate;
|
||||
OrderStatus external_status() const { return _state.status(); }
|
||||
bool in_do_launch_attached = false;
|
||||
|
||||
void set_external_status(OrderStatus s) {
|
||||
is_state_dirty = true;
|
||||
@@ -94,11 +96,19 @@ struct AlgoWrapperOrder::PImpl {
|
||||
}
|
||||
|
||||
void set_fill_quantities(Quantity total_fills, Price last_price,
|
||||
Price avg_price) {
|
||||
Price avg_price, bool set_attach_time_fills) {
|
||||
is_state_dirty = true;
|
||||
_state.set_filled_quantity(total_fills.value());
|
||||
_state.set_last_price(last_price.value());
|
||||
_state.set_avg_price(avg_price.value());
|
||||
if (set_attach_time_fills) {
|
||||
LOG(OrderMan, Diag,
|
||||
"AlgoWrapperOrder {} setting fill at attach time to {}",
|
||||
_my_key.value(), total_fills.value());
|
||||
_state.set_filled_at_attach_time_quantity(total_fills.value());
|
||||
} else
|
||||
LOG(OrderMan, Diag, "AlgoWrapperOrder {} settings fills to {}",
|
||||
_my_key.value(), total_fills.value());
|
||||
}
|
||||
|
||||
void set_ladder_visibility(bool is_visible) {
|
||||
@@ -107,8 +117,11 @@ struct AlgoWrapperOrder::PImpl {
|
||||
: OrderState::HIDDEN);
|
||||
}
|
||||
|
||||
void set_key(OrderKey k) { _my_key = k; }
|
||||
|
||||
private:
|
||||
OrderState _state;
|
||||
OrderKey _my_key;
|
||||
};
|
||||
|
||||
struct AlgoWrapperOrder::ReportStateGuard {
|
||||
@@ -116,6 +129,11 @@ struct AlgoWrapperOrder::ReportStateGuard {
|
||||
~ReportStateGuard() {
|
||||
if (owner.me->is_state_dirty) {
|
||||
owner.me->is_state_dirty = false;
|
||||
LOG(OrderMan, Diag,
|
||||
"AlgoWrapperOrder::ReportStateGuard. k: {} fills: fills at "
|
||||
"attach: {}",
|
||||
owner.key().value(), owner.me->state().filled_quantity(),
|
||||
owner.me->state().filled_at_attach_time_quantity());
|
||||
owner.me->services->report_state(owner.me->state());
|
||||
}
|
||||
}
|
||||
@@ -142,6 +160,9 @@ void AlgoWrapperOrder::on_init_new(OrderDefinition const &def,
|
||||
// Save our order definition
|
||||
me->definition = def;
|
||||
|
||||
// Save our key
|
||||
me->set_key(key());
|
||||
|
||||
// If we are consuming the attachment we remove it from our definition so
|
||||
// that derived orders can use our definition to create their children
|
||||
// without triggering an attachment.
|
||||
@@ -169,6 +190,7 @@ void AlgoWrapperOrder::on_init_recovered(
|
||||
OrderRecord const &rec, const PreActivationOrderServices &serv) {
|
||||
auto const &def = rec.def();
|
||||
me->definition = def;
|
||||
me->set_key(key());
|
||||
me->set_state(rec.state());
|
||||
me->security_db = &serv.sec_db;
|
||||
me->order_factory = &serv.order_factory;
|
||||
@@ -201,7 +223,6 @@ void AlgoWrapperOrder::on_init_recovered(
|
||||
|
||||
OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q,
|
||||
OrderServices &services) {
|
||||
ReportStateGuard _(*this);
|
||||
|
||||
bts_assert(!me->services);
|
||||
me->services = &services;
|
||||
@@ -218,16 +239,25 @@ OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q,
|
||||
me->set_price_quantity(p, q);
|
||||
for (auto &ct : me->child_templates)
|
||||
ct->builder.activate(*me->services);
|
||||
if (q.value() == 0)
|
||||
if (q.value() == 0) {
|
||||
ReportStateGuard _(*this);
|
||||
terminate(ORDER_STATUS_COMPLETED);
|
||||
}
|
||||
else if (is_attaching())
|
||||
// Do not report state until the attachment is complete, so
|
||||
// that we can correctly populate the
|
||||
// filled_at_attach_time_quantity field
|
||||
transition(Status::W_ATTACH);
|
||||
else
|
||||
else {
|
||||
ReportStateGuard _(*this);
|
||||
transition(Status::WORKING);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
case Status::COMPLETED:
|
||||
case Status::COMPLETED: {
|
||||
ReportStateGuard _(*this);
|
||||
return {};
|
||||
}
|
||||
|
||||
case Status::W_ATTACH:
|
||||
case Status::SHUTTING_DOWN:
|
||||
@@ -240,16 +270,20 @@ OrderOpResult AlgoWrapperOrder::on_activate(Price p, Quantity q,
|
||||
}
|
||||
|
||||
OrderOpResult AlgoWrapperOrder::on_update(Update const &update) {
|
||||
ReportStateGuard _(*this);
|
||||
|
||||
switch (me->status) {
|
||||
case Status::W_ATTACH:
|
||||
// We do not want to report state until the attach is complete
|
||||
// so there is no report guard here
|
||||
me->pending_update = update;
|
||||
return {};
|
||||
case Status::COMPLETED:
|
||||
case Status::SHUTTING_DOWN:
|
||||
case Status::SHUTTING_DOWN: {
|
||||
ReportStateGuard _(*this);
|
||||
return {};
|
||||
}
|
||||
case Status::WORKING: {
|
||||
ReportStateGuard _(*this);
|
||||
me->set_price_quantity(Price(update.price()),
|
||||
Quantity(update.quantity()));
|
||||
if (update.has_custom()) {
|
||||
@@ -268,8 +302,6 @@ OrderOpResult AlgoWrapperOrder::on_update(Update const &update) {
|
||||
}
|
||||
|
||||
OrderOpResult AlgoWrapperOrder::on_update(Price p, Quantity q) {
|
||||
ReportStateGuard _(*this);
|
||||
|
||||
Update up;
|
||||
up.set_price(p.value());
|
||||
up.set_quantity(q.value());
|
||||
@@ -368,8 +400,8 @@ void AlgoWrapperOrder::subscribe_book(SecurityKey k) {
|
||||
return;
|
||||
bts_assert(me->book_subs.find(k) == me->book_subs.end());
|
||||
|
||||
me->book_subs[k] = me->services->subscribe_to_book(
|
||||
k, [this, k](auto const &ev) {
|
||||
me->book_subs[k] =
|
||||
me->services->subscribe_to_book(k, [this, k](auto const &ev) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_book(k, ev.book);
|
||||
});
|
||||
@@ -384,18 +416,20 @@ void AlgoWrapperOrder::unsubscribe_book(SecurityKey k) {
|
||||
}
|
||||
|
||||
void AlgoWrapperOrder::subscribe_ticker(SecurityKey k) {
|
||||
if (!is_working()) return;
|
||||
if (!is_working())
|
||||
return;
|
||||
bts_assert(me->ticker_subs.find(k) == me->ticker_subs.end());
|
||||
|
||||
me->ticker_subs[k] = me->services->subscribe_to_ticker(
|
||||
k, [this,k](const auto& ev) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_ticker(k, ev.trades);
|
||||
});
|
||||
me->ticker_subs[k] =
|
||||
me->services->subscribe_to_ticker(k, [this, k](const auto &ev) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_ticker(k, ev.trades);
|
||||
});
|
||||
}
|
||||
|
||||
void AlgoWrapperOrder::unsubscribe_ticker(SecurityKey k) {
|
||||
if (!is_working()) return;
|
||||
if (!is_working())
|
||||
return;
|
||||
bts_assert(me->ticker_subs.find(k) != me->ticker_subs.end());
|
||||
if (auto it = me->ticker_subs.find(k); it != me->ticker_subs.end())
|
||||
me->services->unsubscribe_from_ticker(it->second);
|
||||
@@ -428,11 +462,10 @@ void AlgoWrapperOrder::subscribe_timer() {
|
||||
return;
|
||||
bts_assert(!me->has_timer_sub);
|
||||
me->has_timer_sub = true;
|
||||
me->services->subscribe_to_time(
|
||||
[this](auto const &) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_timer();
|
||||
});
|
||||
me->services->subscribe_to_time([this](auto const &) {
|
||||
ReportStateGuard _(*this);
|
||||
me->algo->on_timer();
|
||||
});
|
||||
}
|
||||
|
||||
void AlgoWrapperOrder::unsubscribe_timer() {
|
||||
@@ -542,7 +575,11 @@ void AlgoWrapperOrder::update_fill_state(Quantity q, Price p) {
|
||||
(me->state().filled_quantity() * me->state().avg_price() +
|
||||
(q.value() - previous_qty) * p.value()) /
|
||||
q.value();
|
||||
me->set_fill_quantities(q, p, new_avg_price);
|
||||
LOG(OrderMan, Diag,
|
||||
"AlgoWrapperOrder updated its fill state. Total fills: {} Is "
|
||||
"attaching: {}",
|
||||
q.value(), me->in_do_launch_attached);
|
||||
me->set_fill_quantities(q, p, new_avg_price, me->in_do_launch_attached);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,8 +649,9 @@ void AlgoWrapperOrder::on_attach_complete(
|
||||
key().value(), me->attach_target.value(), r.error_msg));
|
||||
}
|
||||
break;
|
||||
case Status::COMPLETED:
|
||||
case Status::COMPLETED: {
|
||||
break;
|
||||
}
|
||||
case Status::SHUTTING_DOWN:
|
||||
case Status::INACTIVE:
|
||||
case Status::WORKING:
|
||||
@@ -729,8 +767,17 @@ void AlgoWrapperOrder::setup_transitions() {
|
||||
_transition(Status::WORKING);
|
||||
auto &maybe_state = me->children[me->attach_target].state;
|
||||
bts_assert(maybe_state.has_value());
|
||||
|
||||
// This is ugly but its needed to make the API as simple as possible but
|
||||
// still be able to distinguish fills that were present at the attachment
|
||||
// time from fills that came later. update_fill_state() will look at this
|
||||
// boolean to decide how it should populate fills_at_attach_time
|
||||
me->in_do_launch_attached = true;
|
||||
me->algo->do_launch_attached(*this, me->attach_target,
|
||||
maybe_state.value());
|
||||
// Reset in_do_launch_attached back to false
|
||||
me->in_do_launch_attached = false;
|
||||
|
||||
if (me->status == Status::WORKING && me->pending_update.has_value()) {
|
||||
auto update = me->pending_update.value();
|
||||
me->pending_update.reset();
|
||||
@@ -876,7 +923,8 @@ void AlgoWrapperOrder::update_child_record(
|
||||
Price(new_state.avg_price()),
|
||||
Quantity(new_state.filled_quantity() - old_state.total_fills.value()),
|
||||
Price(new_state.last_price()),
|
||||
map_child_status(new_state.status())};
|
||||
map_child_status(new_state.status()),
|
||||
Quantity(new_state.filled_at_attach_time_quantity())};
|
||||
}
|
||||
|
||||
OrderKey AlgoWrapperOrder::index_and_launch_child(ChildOrder &&child, Price p,
|
||||
|
||||
@@ -17,6 +17,7 @@ struct ChildState {
|
||||
Quantity last_fills;
|
||||
Price last_price;
|
||||
ChildStatus status;
|
||||
Quantity fills_at_attach_time;
|
||||
};
|
||||
|
||||
class Book;
|
||||
@@ -61,7 +62,9 @@ class IAlgoOrder {
|
||||
// Override this method to activate your algo using an existing
|
||||
// working order as a child. Attached child orders are
|
||||
// automatically subscribed to, so there is no need to subscribe to
|
||||
// changes for the attached order.
|
||||
// changes for the attached order. Note that any fills reported
|
||||
// from this call through IAlgoServices::update_fill_state() will
|
||||
// be included in the fills_at_attach_time count field.
|
||||
virtual void do_launch_attached(IAlgoServices &, OrderKey child, ChildState) ;
|
||||
|
||||
// Implement this method to respond to updates to your algo. The
|
||||
|
||||
@@ -87,6 +87,12 @@ void CoverAlgo::on_covered_change(ChildState const &st) {
|
||||
switch (me->status) {
|
||||
case Status::WORKING:
|
||||
case Status::CANCELING:
|
||||
LOG(OrderMan, Diag,
|
||||
"Cover received child update. Child fills: {} Pre-Attach child "
|
||||
"fills: {}",
|
||||
st.total_fills.value(), st.fills_at_attach_time.value());
|
||||
if (me->hedged_fills.value() == 0)
|
||||
me->hedged_fills = st.fills_at_attach_time;
|
||||
if (st.total_fills.value() > me->hedged_fills.value()) {
|
||||
auto hedge_qty =
|
||||
Quantity(st.total_fills.value() - me->hedged_fills.value());
|
||||
@@ -130,7 +136,8 @@ void CoverAlgo::on_covered_change(ChildState const &st) {
|
||||
terminate_if_hedges_complete();
|
||||
break;
|
||||
case ChildStatus::CANCELED:
|
||||
LOG(OrderMan, Info, "CoverAlgo {}. Child was unexpectadly canceled, terminating",
|
||||
LOG(OrderMan, Info,
|
||||
"CoverAlgo {}. Child was unexpectadly canceled, terminating",
|
||||
algo().order_key().value());
|
||||
on_done();
|
||||
algo().terminate_error("Unexpected child cancel");
|
||||
@@ -283,13 +290,11 @@ IAlgoOrder::OnCancelResponse CoverAlgo::on_cancel() {
|
||||
return IAlgoOrder::OnCancelResponse::HONOR;
|
||||
}
|
||||
|
||||
void CoverAlgo::on_done()
|
||||
{
|
||||
void CoverAlgo::on_done() {
|
||||
me->status = Status::DONE;
|
||||
maybe_detach_hedges();
|
||||
}
|
||||
|
||||
|
||||
void CoverAlgo::maybe_detach_hedges() {
|
||||
if (!me->cancel_hedges_on_cancel)
|
||||
for (auto &k : me->hedges)
|
||||
|
||||
@@ -67,6 +67,9 @@ void TopAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k,
|
||||
me->algo = &algo;
|
||||
me->child_key = k;
|
||||
algo.subscribe_book(SecurityKey(algo.security().id()));
|
||||
LOG(OrderMan, Diag,
|
||||
"Top order attached to order {}. Child currently has {} fills",
|
||||
k.value(), st.total_fills);
|
||||
on_child_change(k, st);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user