513 lines
15 KiB
C++
513 lines
15 KiB
C++
#include "BookOrderBase.h"
|
|
#include "lib/orderman/actors/order_manager/ChildOrder.h"
|
|
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/type_name.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
using namespace api::orderman;
|
|
|
|
enum class BookOrderBase::InternalStatus {
|
|
INACTIVE,
|
|
PENDING_ATTACH,
|
|
WAITING_FOR_MD,
|
|
WORKING,
|
|
COMPLETED
|
|
};
|
|
|
|
struct BookOrderBase::PImpl {
|
|
InternalStatus internal_status = InternalStatus::INACTIVE;
|
|
ChildOrder child;
|
|
OrderServices *services = nullptr;
|
|
OrderDefinition definition;
|
|
OrderState state;
|
|
Book book_at_inception;
|
|
Book last_book;
|
|
OrderState child_state;
|
|
OrderServices::BookSubID book_sid;
|
|
std::unique_ptr<api::orderman::Update> pending_update;
|
|
|
|
std::string type_name;
|
|
OrderStatus external_status() { return state.status(); }
|
|
void set_external_status(OrderStatus s) { state.set_status(s); }
|
|
};
|
|
|
|
void BookOrderBase::set_external_status(OrderStatus s, std::string const &msg) {
|
|
me->state.set_status(s);
|
|
me->state.set_last_msg(msg);
|
|
}
|
|
|
|
OrderStatus BookOrderBase::external_status() { return me->state.status(); }
|
|
|
|
BookOrderBase::BookOrderBase() : me(new PImpl()) {
|
|
me->type_name = utils::type_name(typeid(*this));
|
|
}
|
|
BookOrderBase::~BookOrderBase() {}
|
|
|
|
void BookOrderBase::on_init_new(OrderDefinition const &def,
|
|
const PreActivationOrderServices &) {
|
|
bts_assert(me->definition.has_custom_def());
|
|
|
|
// Save our order definition
|
|
me->definition = def;
|
|
|
|
auto result = do_on_init_new();
|
|
if (result.fail()) {
|
|
terminate(ORDER_STATUS_REJECTED_BY_SPARK, result.error_msg);
|
|
transition(InternalStatus::COMPLETED);
|
|
}
|
|
}
|
|
|
|
void BookOrderBase::on_init_recovered(OrderRecord const &rec,
|
|
const PreActivationOrderServices &) {
|
|
auto const &def = rec.def();
|
|
me->definition = def;
|
|
me->state = rec.state();
|
|
|
|
// Infer internal status from persisted external status
|
|
switch (me->external_status()) {
|
|
|
|
case ORDER_STATUS_WORKING:
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
terminate(ORDER_STATUS_REJECTED_BY_SPARK,
|
|
"System restarted with active algo order");
|
|
return;
|
|
|
|
// terminal states
|
|
case ORDER_STATUS_ERROR:
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_CANCELED:
|
|
terminate(rec.state().status(), rec.state().last_msg());
|
|
break;
|
|
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_INACTIVE:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
// these are illegal persisted states for this order type
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::on_activate(Price p, Quantity q,
|
|
OrderServices &services) {
|
|
bts_assert(!me->services);
|
|
me->services = &services;
|
|
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::INACTIVE: {
|
|
me->state.set_price(p.value());
|
|
me->state.set_quantity(q.value());
|
|
|
|
if (q.value() == 0) {
|
|
terminate(ORDER_STATUS_COMPLETED);
|
|
return report_and_return();
|
|
}
|
|
transition(InternalStatus::WAITING_FOR_MD);
|
|
break;
|
|
}
|
|
case InternalStatus::COMPLETED:
|
|
return report_and_return();
|
|
|
|
case InternalStatus::PENDING_ATTACH:
|
|
case InternalStatus::WORKING:
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
break;
|
|
}
|
|
|
|
bts_assert(false);
|
|
return report_and_return();
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::on_update(Update const &update) {
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
case InternalStatus::PENDING_ATTACH:
|
|
me->pending_update = std::make_unique<Update>(update);
|
|
return OrderOpResult{};
|
|
|
|
case InternalStatus::COMPLETED:
|
|
return OrderOpResult(ORDER_ERROR_INVALID_OP,
|
|
"Order is already completed");
|
|
|
|
case InternalStatus::WORKING:
|
|
return do_on_update(update);
|
|
case InternalStatus::INACTIVE:
|
|
break;
|
|
}
|
|
|
|
bts_assert(false);
|
|
return OrderOpResult{};
|
|
}
|
|
|
|
|
|
OrderOpResult BookOrderBase::on_update(Price p, Quantity q) {
|
|
Update up;
|
|
up.set_price(p.value());
|
|
up.set_quantity(q.value());
|
|
return on_update(up);
|
|
}
|
|
|
|
orderman::OrderOpResult BookOrderBase::on_detach() {
|
|
LOG(OrderMan, Info, "{} order with key {} received detach", me->type_name,key().value());
|
|
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
case InternalStatus::PENDING_ATTACH:
|
|
case InternalStatus::WORKING:
|
|
me->child = ChildOrder{};
|
|
terminate(ORDER_STATUS_CANCELED);
|
|
break;
|
|
|
|
case InternalStatus::COMPLETED:
|
|
break;
|
|
|
|
case InternalStatus::INACTIVE:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
return report_and_return();
|
|
}
|
|
|
|
orderman::OrderOpResult BookOrderBase::on_cancel() {
|
|
switch (me->internal_status) {
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
case InternalStatus::PENDING_ATTACH:
|
|
case InternalStatus::WORKING:
|
|
terminate(ORDER_STATUS_CANCELED);
|
|
return report_and_return();
|
|
|
|
case InternalStatus::COMPLETED:
|
|
return report_and_return();
|
|
|
|
case InternalStatus::INACTIVE:
|
|
break;
|
|
}
|
|
|
|
// compiler
|
|
bts_assert(false);
|
|
return OrderOpResult{};
|
|
}
|
|
|
|
void BookOrderBase::on_book_update(const BookUpdateEvent &ev) {
|
|
me->last_book = ev.book;
|
|
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::WAITING_FOR_MD: {
|
|
me->book_at_inception = ev.book;
|
|
|
|
auto ak = OrderKey(me->definition.attach_target_order_key());
|
|
if (ak != null_order_key() &&
|
|
!should_forward_attachment(me->definition.custom_def()))
|
|
transition(InternalStatus::PENDING_ATTACH);
|
|
else
|
|
transition(InternalStatus::WORKING);
|
|
report_and_return();
|
|
}
|
|
|
|
case InternalStatus::PENDING_ATTACH:
|
|
case InternalStatus::COMPLETED:
|
|
break;
|
|
|
|
case InternalStatus::WORKING:
|
|
do_on_book(ev.book);
|
|
break;
|
|
|
|
case InternalStatus::INACTIVE:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
bts_assert(false);
|
|
}
|
|
|
|
void BookOrderBase::unsubscribe_from_book()
|
|
{
|
|
bts_assert(!me->book_sid.empty());
|
|
me->services->unsubscribe_from_book(me->book_sid);
|
|
me->book_sid.clear();
|
|
}
|
|
|
|
void BookOrderBase::on_attach_complete(OrderOpResult r, ChildOrder const &child,
|
|
api::orderman::OrderDefinition const &,
|
|
api::orderman::OrderState const &state) {
|
|
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::PENDING_ATTACH:
|
|
if (r.ok()) {
|
|
|
|
LOG(OrderMan, Info, "{} order with key {} successfully attached to {}",
|
|
me->type_name, key().value(), child.key().value());
|
|
|
|
// Cache the attached order and its state
|
|
me->child = child;
|
|
me->child_state = state;
|
|
|
|
// transition
|
|
transition(InternalStatus::WORKING);
|
|
} else {
|
|
terminate(ORDER_STATUS_ERROR,
|
|
fmt::format(
|
|
"{} order with key {} failed to attach to {}: {}",
|
|
me->type_name, key().value(),
|
|
me->definition.attach_target_order_key(), r.error_msg));
|
|
}
|
|
break;
|
|
|
|
case InternalStatus::COMPLETED:
|
|
break;
|
|
|
|
case InternalStatus::INACTIVE:
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
case InternalStatus::WORKING:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
report_and_return();
|
|
}
|
|
|
|
void BookOrderBase::on_child_order_update(OrderStateUpdateEvent const &ev) {
|
|
switch (me->internal_status) {
|
|
case InternalStatus::WORKING:
|
|
me->child_state = ev.rebuild_state();
|
|
do_on_child_order_update();
|
|
return;
|
|
case InternalStatus::COMPLETED:
|
|
break;
|
|
case InternalStatus::INACTIVE:
|
|
case InternalStatus::PENDING_ATTACH:
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::report_and_return(api::orderman::OrderError e,
|
|
std::string const &msg) {
|
|
me->state.set_filled_quantity(me->child_state.filled_quantity());
|
|
me->state.set_last_price(me->child_state.last_price());
|
|
me->state.set_avg_price(me->child_state.avg_price());
|
|
me->services->report_state(me->state);
|
|
return OrderOpResult{e, msg};
|
|
}
|
|
|
|
void BookOrderBase::terminate(api::orderman::OrderStatus st,
|
|
std::string const &error) {
|
|
me->set_external_status(st);
|
|
if (error.size())
|
|
me->state.set_last_msg(error);
|
|
transition(InternalStatus::COMPLETED);
|
|
}
|
|
|
|
void BookOrderBase::transition(InternalStatus new_state) {
|
|
|
|
// From INACTIVE
|
|
if (me->internal_status == InternalStatus::INACTIVE) {
|
|
|
|
// To WAITING_FOR_MD
|
|
if (new_state == InternalStatus::WAITING_FOR_MD) {
|
|
me->book_sid = me->services->subscribe_to_book(
|
|
SecurityKey(me->definition.security_key()),
|
|
[this](auto const &ev) { this->on_book_update(ev); });
|
|
me->services->subscribe_to_child_updates(
|
|
[this](auto const &u) { this->on_child_order_update(u); });
|
|
me->set_external_status(ORDER_STATUS_WORKING);
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
|
|
// To COMPLETED
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// From WAITING_FOR_MD
|
|
if (me->internal_status == InternalStatus::WAITING_FOR_MD) {
|
|
|
|
// To PENDING_ATTACH
|
|
if (new_state == InternalStatus::PENDING_ATTACH) {
|
|
me->services->attach(
|
|
OrderKey(me->definition.attach_target_order_key()),
|
|
[this](auto result, auto const &child_order, auto const &child_def,
|
|
auto const &child_state) {
|
|
this->on_attach_complete(result, child_order, child_def,
|
|
child_state);
|
|
});
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
|
|
// To WORKING
|
|
if (new_state == InternalStatus::WORKING) {
|
|
_transition(new_state);
|
|
do_on_ready(false);
|
|
return;
|
|
}
|
|
|
|
// To COMPLETED
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
unsubscribe_from_book();
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// From PENDING_ATTACH
|
|
if (me->internal_status == InternalStatus::PENDING_ATTACH) {
|
|
|
|
// To WORKING
|
|
if (new_state == InternalStatus::WORKING) {
|
|
_transition(new_state);
|
|
do_on_ready(true);
|
|
|
|
// If we are still in the working state, and if we need to
|
|
// check because the do_on_ready() call could have
|
|
// transitioned us, and we have an external update pending,
|
|
// we send it along now.
|
|
if (me->internal_status == InternalStatus::WORKING &&
|
|
me->pending_update) {
|
|
auto r = do_on_update(*me->pending_update);
|
|
me->pending_update.reset();
|
|
if (r.fail() && me->internal_status == InternalStatus::WORKING) {
|
|
// The buffered update we received failed. We cannot
|
|
// reply to the original sender because that call stack
|
|
// is gone (this is when a continuation based response
|
|
// would be usefull) so we force a detach() and report
|
|
// the error
|
|
me->child = ChildOrder{};
|
|
terminate(
|
|
ORDER_STATUS_ERROR,
|
|
fmt::format("Buffered update failed: {}", r.error_msg));
|
|
return;
|
|
}
|
|
}
|
|
|
|
// If we are still in the working state here (and we might
|
|
// not be since we transfered control above) we now propagate
|
|
// the state we received at attach time as if received it as
|
|
// a regular child state update. This makes the child code
|
|
// easier since it doesn't have to distinguish the attach
|
|
// case from the regular one.
|
|
if (me->internal_status == InternalStatus::WORKING) {
|
|
do_on_child_order_update();
|
|
}
|
|
return;
|
|
}
|
|
|
|
// To COMPLETED
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
me->pending_update.reset();
|
|
unsubscribe_from_book();
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// From WORKING
|
|
if (me->internal_status == InternalStatus::WORKING) {
|
|
|
|
// To COMPLETED
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
if (!me->child.empty()) {
|
|
me->child.cancel();
|
|
me->child = ChildOrder{};
|
|
}
|
|
me->pending_update.reset();
|
|
unsubscribe_from_book();
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Everything else is verbotten
|
|
LOG(OrderMan, Fatal, "Illegal transition from {} to {}",
|
|
state_name(me->internal_status), state_name(new_state));
|
|
bts_assert(false);
|
|
}
|
|
|
|
void BookOrderBase::check_invariants() {
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::INACTIVE:
|
|
bts_assert(false);
|
|
return;
|
|
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
bts_assert(me->child.empty());
|
|
bts_assert(me->external_status() == ORDER_STATUS_WORKING);
|
|
bts_assert(me->child_state.status() == ORDER_STATUS_INACTIVE);
|
|
bts_assert(!me->book_sid.empty());
|
|
bts_assert(never_saw_md());
|
|
break;
|
|
|
|
case InternalStatus::PENDING_ATTACH:
|
|
bts_assert(me->child.empty());
|
|
bts_assert(me->external_status() == ORDER_STATUS_WORKING);
|
|
bts_assert(me->child_state.status() == ORDER_STATUS_INACTIVE);
|
|
bts_assert(!me->book_sid.empty());
|
|
bts_assert(!never_saw_md());
|
|
break;
|
|
|
|
case InternalStatus::WORKING:
|
|
bts_assert(me->external_status() == ORDER_STATUS_WORKING);
|
|
bts_assert(!never_saw_md());
|
|
break;
|
|
|
|
case InternalStatus::COMPLETED:
|
|
bts_assert(me->child.empty());
|
|
bts_assert(me->external_status() == ORDER_STATUS_CANCELED ||
|
|
me->external_status() == ORDER_STATUS_COMPLETED ||
|
|
me->external_status() == ORDER_STATUS_ERROR ||
|
|
me->external_status() == ORDER_STATUS_REJECTED_BY_SPARK);
|
|
bts_assert(me->book_sid.empty());
|
|
bts_assert(me->book_sid.empty());
|
|
bts_assert(!never_saw_md());
|
|
bts_assert(!me->pending_update);
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool BookOrderBase::never_saw_md() {
|
|
return me->book_at_inception.ask().size() == 0 &&
|
|
me->book_at_inception.bid().size() == 0 &&
|
|
me->last_book.ask().size() == 0 && me->last_book.bid().size() == 0;
|
|
}
|
|
|
|
void BookOrderBase::_transition(InternalStatus st) {
|
|
LOG(OrderMan, Info, "Transitioninig {} with key {} from {} to {}",
|
|
me->type_name, key().value(), state_name(me->internal_status),
|
|
state_name(st));
|
|
me->internal_status = st;
|
|
check_invariants();
|
|
}
|
|
|
|
log::CharPtrWrapper BookOrderBase::state_name(InternalStatus st) {
|
|
switch (st) {
|
|
case InternalStatus::WAITING_FOR_MD:
|
|
return {"WAITING_FOR_MD"};
|
|
case InternalStatus::COMPLETED:
|
|
return {"COMPLETED"};
|
|
case InternalStatus::PENDING_ATTACH:
|
|
return {"PENDING_ATTACH"};
|
|
case InternalStatus::INACTIVE:
|
|
return {"INACTIVE"};
|
|
case InternalStatus::WORKING:
|
|
return {"WORKING"};
|
|
}
|
|
bts_assert(false);
|
|
return {""};
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|