Improved AlgoOrder API.

This commit is contained in:
Pedro Pinto committed 2019-04-18 08:59:47 -04:00
1 parent cb6621ac06
commit d240b75b95
3 files changed
+42 -7

No files matched your search

@@ -43,6 +43,8 @@ struct AlgoOrder::PImpl {
Map<SecurityKey, OrderServices::BookSubID> book_subs;
Map<OrderKey, ChildRecord> children;
bool has_timer_sub = false;
bool is_state_dirty = false;
Map<Edge, Action> transition_actions;
@@ -123,6 +125,10 @@ Quantity AlgoOrder::quantity() const {
return Quantity(self->state().quantity());
}
Quantity AlgoOrder::total_fills() const {
return Quantity(self->state().total_fills());
}
bool AlgoOrder::is_attaching() const {
return self->attach_target != null_order_key();
}
@@ -294,6 +300,21 @@ void AlgoOrder::unsubscribe_child_change(OrderKey k) {
child_record.is_subscribing = false;
}
void AlgoOrder::subscribe_timer()
{
if (!is_working())
return;
bts_assert(!self->has_timer_sub);
self->services->subscribe_to_time([this](auto const&) {on_timer();});
}
void AlgoOrder::unsubscribe_timer() {
if (!is_working())
return;
bts_assert(self->has_timer_sub);
self->services->unsubscribe_from_time();
}
// Private
void AlgoOrder::on_init_new(OrderDefinition const &def,
const PreActivationOrderServices &serv) {
@@ -631,6 +652,7 @@ void AlgoOrder::check_invariants() {
bts_assert(self->external_status() == ORDER_STATUS_WORKING);
bts_assert(self->book_subs.empty());
bts_assert(self->children.empty());
bts_assert(!self->has_timer_sub);
break;
case Status::WORKING:
@@ -647,6 +669,7 @@ void AlgoOrder::check_invariants() {
self->external_status() == ORDER_STATUS_ERROR ||
self->external_status() == ORDER_STATUS_REJECTED_BY_SPARK);
bts_assert(self->book_subs.empty());
bts_assert(!self->has_timer_sub);
bts_assert(!self->pending_update.has_value());
break;
}
@@ -687,6 +710,8 @@ void AlgoOrder::cleanup() {
c.second.order.cancel();
self->children.clear();
}
if (self->has_timer_sub)
self->services->unsubscribe_from_time();
}
void AlgoOrder::update_child_record(api::orderman::OrderState const &new_state,
+13 -1
View File
@@ -51,6 +51,9 @@ class AlgoOrder : private Order {
// from do_before_launch().
Quantity quantity() const;
// Returns the filled quantity for this order.
Quantity total_fills() const;
// Returns wheter this order is attaching to an existing order
bool is_attaching() const;
@@ -123,10 +126,16 @@ class AlgoOrder : private Order {
void subscribe_book(SecurityKey);
void unsubscribe_book(SecurityKey);
// Subscribe to child order updates for the specified child order
// Subscribe to child order updates for the specified child
// order. This is rarely needed since launch_child() by default
// automatically subscribes to new orders.
void subscribe_child_change(OrderKey);
void unsubscribe_child_change(OrderKey);
// Subscribe to a periodic timer
void subscribe_timer();
void unsubscribe_timer();
// --------------------------------------------------------------
// Events
// --------------------------------------------------------------
@@ -168,6 +177,9 @@ class AlgoOrder : private Order {
// otherwise.
virtual void do_update(api::orderman::Update const &) {}
// Implement this method to receive a periodical call
virtual void on_timer() {}
private:
friend class OrderFactory;
struct ReportStateGuard;
@@ -10,7 +10,6 @@ namespace orderman {
using namespace api::orderman;
struct CoverOrder::PImpl {
uint32_t fills_so_far = 0;
OrderKey child_key = null_order_key();
Price price_improvement_delta;
ChildTemplate *child_template = nullptr;
@@ -70,16 +69,14 @@ void CoverOrder::do_launch_attached(OrderKey k, ChildState st) {
void CoverOrder::on_child_change(OrderKey k, ChildState const &st) {
bts_assert(k == me->child_key); // We are not subscribed to hedge changes
update_fill_state(st.total_fills, st.last_price);
if (st.total_fills.value() > me->fills_so_far) {
auto hedge_qty = Quantity(st.total_fills.value() - me->fills_so_far);
if (st.total_fills.value() > total_fills().value()) {
auto hedge_qty = Quantity(st.total_fills.value() - total_fills().value());
auto hedge_price = securities::improve_price(
definition().side(), st.last_price, me->price_improvement_delta);
me->fills_so_far = st.total_fills.value();
launch_child(*me->hedge_template, hedge_price, hedge_qty,
false); // do no
false); // do not subscribe to hedge changes
LOG(OrderMan, Diag,
"CoverOrder {}, launched new hedge in response to child fill. Hedge: "
@@ -87,6 +84,7 @@ void CoverOrder::on_child_change(OrderKey k, ChildState const &st) {
order_key().value(), securities::side_name(definition().side()),
hedge_qty.value(), hedge_price.value());
}
update_fill_state(st.total_fills, st.last_price);
switch (st.status) {
case ChildStatus::WORKING: