Attach/Detach passes initial tests.

This commit is contained in:
Pedro Pinto committed 2019-03-05 15:44:01 -05:00
1 parent 345f6ed9fe
commit 299d46aa2c
6 files changed
+49 -120

No files matched your search

@@ -89,6 +89,7 @@ public:
private:
void move_here(OrderBuilder &src) {
bts_assert(!_order_services);
_order_services = src._order_services;
_parent_key = src._parent_key;
_definition = src._definition;
_reserve_sz = src._reserve_sz;
@@ -212,20 +212,6 @@ class OrderServices {
template <class CB>
void activate(Price p, Quantity q, CB const &on_order_op = ignore) {
bts_assert(_owner->def());
if (_owner->def()->attach_target_order_key() !=
null_order_key().value() &&
!_owner->supports_attachment()) {
auto msg = "Order does not support attachment";
on_order_op(OrderOpResult {api::orderman::ORDER_ERROR_INVALID_OP,msg});
api::orderman::OrderState state;
state.set_status(api::orderman::ORDER_STATUS_REJECTED_BY_SPARK);
state.set_last_msg("Order does not support attachment");
report_state(state);
return;
}
call_with_reentrancy_guard([p, q, on_order_op](OrderServices &self) {
on_order_op(self._owner->on_activate(p, q, self));
});
@@ -6,40 +6,28 @@ using namespace bts::api::orderman;
namespace bts {
namespace orderman {
bool should_forward_attachment(const api::orderman::CustomOrderDefinition &cdef) {
switch (cdef.custom_case()) {
case CustomOrderDefinition::kMarket:
case CustomOrderDefinition::kLimit:
case CustomOrderDefinition::kStop:
case CustomOrderDefinition::kStopLimit:
case CustomOrderDefinition::kSpreader:
return false;
bool can_consume_attachment(api::orderman::CustomOrderDefinition::CustomCase order_kind) {
switch (order_kind) {
case CustomOrderDefinition::kIceberg:
return
!cdef.iceberg().has_child() ||
!should_forward_attachment(cdef.iceberg().child());
case CustomOrderDefinition::kTimeBox:
case CustomOrderDefinition::kTick:
case CustomOrderDefinition::kLean:
case CustomOrderDefinition::kJoin:
case CustomOrderDefinition::kTop:
case CustomOrderDefinition::kTrigger:
case CustomOrderDefinition::kCover:
case CustomOrderDefinition::kOffset:
case CustomOrderDefinition::kOco:
case CustomOrderDefinition::kPricer:
case CustomOrderDefinition::kDynamic:
return false;
case CustomOrderDefinition::CUSTOM_NOT_SET:
bts_assert(false);
return true;
default:
return false;
}
return false;
}
bool should_forward_attachment(
const api::orderman::CustomOrderDefinition &cdef) {
}}
switch (cdef.custom_case()) {
case CustomOrderDefinition::kIceberg:
return
cdef.iceberg().has_child() &&
can_consume_attachment(cdef.iceberg().child().custom_case());
default:
return false;
}
}
} // namespace orderman
} // namespace bts
+28 -74
View File
@@ -16,7 +16,6 @@ using namespace api::orderman;
enum class IcebergOrder2::InternalStatus {
INACTIVE,
PENDING_ATTACH,
PENDING_ATTACH_FORWARDING,
PAUSED,
WORKING,
COMPLETED
@@ -66,15 +65,19 @@ void IcebergOrder2::on_init_new(OrderDefinition const &def,
me->def = def;
if (def.custom_def().has_dynamic()) {
me->def = def;
auto &ice = *me->def.mutable_custom_def()->mutable_iceberg();
auto &dice = def.custom_def().dynamic();
if (!def.attach_target_order_key())
me->def.set_attach_target_order_key(
dice.order_params().at("attach_target").as_uint32());
ice.set_show_quantity(dice.order_params().at("show_size").as_uint32());
ice.set_pause_between_orders_ms(
dice.order_params().at("pause_ms").as_uint32());
*ice.mutable_child() = dice.order_params().at("child").as_order_def();
}
LOG(OrderMan, Info,"on_init_new: {}", utils::pb::to_string(me->def));
// Initialize mutable iceberg component of our state report
me->order_state.mutable_iceberg();
@@ -85,18 +88,14 @@ void IcebergOrder2::on_init_new(OrderDefinition const &def,
"Show quantity must be greater than 0");
return;
}
auto attach_target = OrderKey(def.attach_target_order_key());
auto attach_target = OrderKey(me->def.attach_target_order_key());
if (!ib.has_child() && attach_target == null_order_key()) {
terminate(ORDER_STATUS_REJECTED_BY_SPARK,
"Iceberg order must have a child order definition or attach to "
"an existing order");
return;
}
if (attach_target == null_order_key())
initialize_child_builder(services.order_factory);
else {
// when attaching we do not use an order builder
}
initialize_child_builder(services.order_factory);
}
void IcebergOrder2::on_init_recovered(OrderRecord const &rec,
@@ -140,10 +139,9 @@ OrderOpResult IcebergOrder2::on_activate(Price p, Quantity q,
switch (me->internal_status) {
case InternalStatus::INACTIVE: {
me->child_builder.activate(services);
me->order_state.set_price(p.value());
me->order_state.set_quantity(q.value());
me->child_builder.activate(services);
if (q.value() == 0) {
terminate(ORDER_STATUS_COMPLETED);
@@ -171,12 +169,11 @@ OrderOpResult IcebergOrder2::on_activate(Price p, Quantity q,
case InternalStatus::WORKING:
case InternalStatus::PAUSED:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
bts_assert(false);
return report_and_return();
}
// compiler
// Compiler
bts_assert(false);
return report_and_return();
}
@@ -185,15 +182,18 @@ OrderOpResult IcebergOrder2::on_activate_attach() {
bts_assert(me->services);
bts_assert(me->internal_status == InternalStatus::INACTIVE);
bts_assert(me->def.attach_target_order_key() != null_order_key().value());
bts_assert(!me->child_builder.is_activated());
bts_assert(me->child_builder.is_activated());
auto attach_target = me->def.attach_target_order_key();
if (should_forward_attachment(me->def.custom_def())) {
// We are fowarding the attachment to our new child
// The first order we launch is going to handle the attachment
// while subsquent childen (if any) will not. So we do not use a
// OrderBuilder or send_next_child_order and instead build and
// launch the first child order directly below.
// while subsquent childen (if any) will not. So we do not use
// our OrderBuilder, which is primed for spawning subsquent
// children, and instead build and launch the first child order
// directly below.
auto attaching_child = make_child_def();
attaching_child.set_attach_target_order_key(attach_target);
me->active_child.order = ChildOrder{attaching_child, *me->services};
@@ -206,14 +206,15 @@ OrderOpResult IcebergOrder2::on_activate_attach() {
Price(me->order_state.price()), Quantity(to_send),
[this](auto const &op) { this->on_child_activate_completed(op); });
me->set_external_status(ORDER_STATUS_PENDING_WORKING);
LOG(OrderMan, Info,
"Iceberg order {} forwarding attachment to {} to first child"
" order {} with size {} price {}",
key().value(), attach_target, me->active_child.key.value(), to_send,
me->order_state.price());
transition(InternalStatus::PENDING_ATTACH_FORWARDING);
me->set_external_status(ORDER_STATUS_WORKING);
me->start_of_pause = TimeStamp();
transition(InternalStatus::WORKING);
} else {
// We are attaching directly
@@ -226,8 +227,7 @@ OrderOpResult IcebergOrder2::on_activate_attach() {
});
LOG(OrderMan, Info,
"Iceberg order {} attempting to attach to {} "
" order {}",
"Iceberg order {} attempting to attach to {} ",
key().value(), attach_target);
transition(InternalStatus::PENDING_ATTACH);
}
@@ -285,7 +285,6 @@ OrderOpResult IcebergOrder2::on_update(api::orderman::Update const &update) {
"Order is already completed");
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
return report_and_return(
ORDER_ERROR_INVALID_OP,
"Order cannot be modified while waiting for attach to complete");
@@ -306,7 +305,6 @@ orderman::OrderOpResult orderman::IcebergOrder2::on_cancel() {
case InternalStatus::PAUSED:
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
terminate(ORDER_STATUS_CANCELED);
return report_and_return();
case InternalStatus::COMPLETED:
@@ -330,11 +328,10 @@ orderman::OrderOpResult orderman::IcebergOrder2::on_detach() {
return report_and_return();
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH_FORWARDING:
me->active_child.reset();
terminate(ORDER_STATUS_CANCELED);
return report_and_return();
case InternalStatus::COMPLETED:
return report_and_return(ORDER_ERROR_INVALID_OP,
"Order is already completed");
@@ -356,20 +353,8 @@ void IcebergOrder2::on_child_order_update(OrderStateUpdateEvent const &ev) {
switch (me->internal_status) {
case InternalStatus::PENDING_ATTACH_FORWARDING:
case InternalStatus::WORKING:
// When we forward an attachment we use the first order update
// to fill in our own order definition, from the definition
// provided by the child that we forwarded the attachment to.
if (me->internal_status == InternalStatus::PENDING_ATTACH_FORWARDING) {
complete_def_from_child(*ev.definition);
initialize_child_builder(me->services->order_factory());
// now we are effectively in the WORKING state
transition(InternalStatus::WORKING);
}
if (ev.key != me->active_child.key) {
LOG(OrderMan, Info,
"Iceberg order {} received child update from unexpected child "
@@ -469,7 +454,6 @@ void IcebergOrder2::on_time() {
case InternalStatus::COMPLETED:
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
break;
}
}
@@ -486,7 +470,6 @@ void orderman::IcebergOrder2::on_child_activate_completed(OrderOpResult r) {
switch (me->internal_status) {
case InternalStatus::PAUSED:
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH_FORWARDING:
terminate(ORDER_STATUS_ERROR,
std::string("Child order operation failed: ") + r.error_msg);
report_and_return();
@@ -535,7 +518,6 @@ void IcebergOrder2::work() {
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
case InternalStatus::COMPLETED:
bts_assert(false);
break;
@@ -552,20 +534,15 @@ void IcebergOrder2::on_attach_complete(
case InternalStatus::PENDING_ATTACH:
if (r.ok()) {
LOG(OrderMan, Info, "Iceberg order {} successfully attached to {}",
key().value(), child.key().value());
// Cache the attached order
me->active_child.order = child;
me->active_child.key = child.key();
// Complete our definition by extracting some of the elements
// of the child we attached to
complete_def_from_child(child_def);
// Initialize our child builder with the definition of the
// next child we expect to create after the one we just
// attached to completes.
initialize_child_builder(me->services->order_factory());
// Now we are effectively in the working state
me->start_of_pause = TimeStamp();
me->set_external_status(ORDER_STATUS_WORKING);
transition(InternalStatus::WORKING);
@@ -584,7 +561,6 @@ void IcebergOrder2::on_attach_complete(
case InternalStatus::COMPLETED:
break;
case InternalStatus::PENDING_ATTACH_FORWARDING:
case InternalStatus::INACTIVE:
case InternalStatus::PAUSED:
case InternalStatus::WORKING:
@@ -608,7 +584,6 @@ void IcebergOrder2::pause() {
case InternalStatus::INACTIVE:
case InternalStatus::PAUSED:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
case InternalStatus::COMPLETED:
bts_assert(false);
break;
@@ -622,7 +597,6 @@ void IcebergOrder2::terminate(api::orderman::OrderStatus reason,
case InternalStatus::PAUSED:
case InternalStatus::WORKING:
case InternalStatus::PENDING_ATTACH:
case InternalStatus::PENDING_ATTACH_FORWARDING:
if (!me->active_child.is_empty()) {
me->active_child.order.cancel();
me->active_child.reset();
@@ -679,16 +653,7 @@ void IcebergOrder2::check_invariants() {
case InternalStatus::PENDING_ATTACH:
bts_assert(me->external_status() == ORDER_STATUS_PENDING_WORKING);
bts_assert(me->active_child.is_empty());
bts_assert(me->start_of_pause == TimeStamp());
bts_assert(!me->child_builder.is_activated());
bts_assert(me->order_state.filled_quantity() == 0);
bts_assert(me->order_state.quantity() > 0);
break;
case InternalStatus::PENDING_ATTACH_FORWARDING:
bts_assert(me->external_status() == ORDER_STATUS_PENDING_WORKING);
bts_assert(!me->active_child.is_empty());
bts_assert(me->start_of_pause == TimeStamp());
bts_assert(!me->child_builder.is_activated());
bts_assert(me->child_builder.is_activated());
bts_assert(me->order_state.filled_quantity() == 0);
bts_assert(me->order_state.quantity() > 0);
break;
@@ -703,9 +668,8 @@ api::orderman::OrderDefinition IcebergOrder2::make_child_def() {
// standard properties of the parent (side, security, TIF, etc)
auto child_def = me->def;
// make sure to skip the attach target if present in the parent
// since attach forwarding is handled specially
// since attach forwarding is handled separately
child_def.clear_attach_target_order_key();
// Copy the custom part of the child order definition to our child
*child_def.mutable_custom_def() = me->iceberg_def().child();
return child_def;
@@ -767,14 +731,6 @@ OrderOpResult IcebergOrder2::on_update(Price p, Quantity q) {
return on_update(up);
}
void IcebergOrder2::complete_def_from_child(
api::orderman::OrderDefinition const &child) {
me->def.set_security_key(child.security_key());
me->def.set_side(child.side());
me->def.set_time_in_force(child.time_in_force());
me->services->report_definition(me->def);
}
std::string orderman::IcebergOrder2::internal_status_name(InternalStatus st) {
switch (st) {
case InternalStatus::INACTIVE:
@@ -787,8 +743,6 @@ std::string orderman::IcebergOrder2::internal_status_name(InternalStatus st) {
return "WORKING";
case InternalStatus::PENDING_ATTACH:
return "PENDING_ATTACH";
case InternalStatus::PENDING_ATTACH_FORWARDING:
return "PENDING_ATTACH_FORWARDING";
}
bts_assert(false);
return "";
-3
View File
@@ -69,9 +69,6 @@ class Order {
// Order has been attached to
virtual void on_attached(api::orderman::OrderDefinition const &);
// Order types have to explicitly opt in to support attachment
virtual bool supports_attachment() {return false;}
protected:
virtual void warm_up_activate(OrderServicesData &, OrderServices &) {}
+3
View File
@@ -82,6 +82,9 @@ int Run::execute(std::vector<std::string> const &) {
param->set_param_name("pause_ms");
param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT);
param = desc.add_param_desc();
param->set_param_name("attach_target");
param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT);
param = desc.add_param_desc();
param->set_param_name("child");
param->mutable_param_type()->set_primitive(
DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF);