758 lines
21 KiB
C++
758 lines
21 KiB
C++
#include "BookOrderBase.h"
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/orderman/actors/order_manager/ChildOrder.h"
|
|
#include "lib/orderman/orderman_utils/orderman_utils.h"
|
|
#include "lib/orderman/orders/PreActivationOrderServices.h"
|
|
#include "lib/utils/PBUtils.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,
|
|
W_ATTACH,
|
|
W_MD,
|
|
WORKING,
|
|
INERT,
|
|
COMPLETED
|
|
};
|
|
|
|
struct BookOrderBase::PImpl {
|
|
InternalStatus internal_status = InternalStatus::INACTIVE;
|
|
ChildOrder child;
|
|
OrderServices *services = nullptr;
|
|
OrderDefinition definition;
|
|
const api::Security *security;
|
|
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); }
|
|
};
|
|
|
|
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 &serv) {
|
|
bts_assert(def.has_custom_def());
|
|
|
|
// Save our order definition
|
|
me->definition = def;
|
|
me->security = serv.sec_db.resolve(SecurityKey(def.security_key()));
|
|
if (me->security == nullptr) {
|
|
terminate(ORDER_STATUS_REJECTED_BY_SPARK, "Unknown security");
|
|
transition(InternalStatus::COMPLETED);
|
|
}
|
|
|
|
auto result = do_on_init_new(serv);
|
|
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_:
|
|
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);
|
|
else
|
|
transition(InternalStatus::W_MD);
|
|
return report_and_return();
|
|
}
|
|
case InternalStatus::COMPLETED:
|
|
return report_and_return();
|
|
|
|
case InternalStatus::W_ATTACH:
|
|
|
|
case InternalStatus::WORKING:
|
|
case InternalStatus::W_MD:
|
|
case InternalStatus::INERT:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
|
|
bts_assert(false);
|
|
return report_and_return();
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::on_update(Update const &update) {
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::W_MD:
|
|
case InternalStatus::W_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::INERT:
|
|
case InternalStatus::WORKING: {
|
|
auto r = do_on_update(update);
|
|
if (r.ok()) {
|
|
// Copy the new custom parameters if any
|
|
if (update.has_custom()) {
|
|
*me->definition.mutable_custom_def() = update.custom();
|
|
me->services->report_definition(me->definition);
|
|
}
|
|
}
|
|
return report_and_return(r);
|
|
}
|
|
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::W_MD:
|
|
case InternalStatus::W_ATTACH:
|
|
case InternalStatus::WORKING:
|
|
case InternalStatus::INERT:
|
|
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::W_MD:
|
|
case InternalStatus::W_ATTACH:
|
|
case InternalStatus::WORKING:
|
|
case InternalStatus::INERT:
|
|
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{};
|
|
}
|
|
|
|
bool BookOrderBase::is_attaching() const {
|
|
auto ak = OrderKey(me->definition.attach_target_order_key());
|
|
return (
|
|
ak != null_order_key() &&
|
|
!should_forward_attachment(me->definition.custom_def()));
|
|
}
|
|
|
|
void BookOrderBase::on_book_update(const BookUpdateEvent &ev) {
|
|
|
|
switch (me->internal_status) {
|
|
|
|
case InternalStatus::W_MD: {
|
|
if (ev.book.side(api::SIDE_SELL)[0].size() &&
|
|
ev.book.side(api::SIDE_BUY)[0].size()) {
|
|
|
|
me->book_at_inception = ev.book;
|
|
me->last_book = ev.book;
|
|
|
|
if (is_attaching())
|
|
transition(InternalStatus::W_ATTACH);
|
|
else
|
|
transition(InternalStatus::WORKING);
|
|
report_and_return();
|
|
}
|
|
return;
|
|
}
|
|
|
|
case InternalStatus::WORKING:
|
|
me->last_book = ev.book;
|
|
do_on_book(ev.book);
|
|
return;
|
|
|
|
case InternalStatus::W_ATTACH:
|
|
case InternalStatus::COMPLETED:
|
|
case InternalStatus::INERT:
|
|
return;
|
|
|
|
case InternalStatus::INACTIVE:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
bts_assert(false);
|
|
}
|
|
|
|
void BookOrderBase::unsubscribe_from_book() {
|
|
if (!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::W_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_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::W_MD:
|
|
case InternalStatus::WORKING:
|
|
case InternalStatus::INERT:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
report_and_return();
|
|
}
|
|
|
|
void BookOrderBase::on_child_order_update(OrderStateUpdateEvent const &ev) {
|
|
on_child_order_update(ev.key, ev.rebuild_state());
|
|
}
|
|
|
|
void BookOrderBase::on_child_order_update(
|
|
OrderKey child_key, api::orderman::OrderState const &child_state) {
|
|
switch (me->internal_status) {
|
|
case InternalStatus::INERT:
|
|
case InternalStatus::WORKING:
|
|
if (child_key == me->child.key()) {
|
|
switch (child_state.status()) {
|
|
|
|
case ORDER_STATUS_WORKING:
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
me->child_state = child_state;
|
|
do_on_child_order_update(child_key, child_state);
|
|
report_and_return();
|
|
return;
|
|
|
|
case ORDER_STATUS_COMPLETED:
|
|
me->child_state = child_state;
|
|
do_on_child_order_update(child_key, child_state);
|
|
if (me->internal_status != InternalStatus::COMPLETED)
|
|
terminate(ORDER_STATUS_COMPLETED);
|
|
report_and_return();
|
|
return;
|
|
|
|
case ORDER_STATUS_CANCELED:
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_ERROR:
|
|
terminate_error("Unexpected child order state");
|
|
return;
|
|
|
|
case ORDER_STATUS_INACTIVE:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
return;
|
|
}
|
|
} else {
|
|
// This is not the child we manage. All we care about is
|
|
// catching errors
|
|
switch (child_state.status()) {
|
|
|
|
case ORDER_STATUS_DISCONNECTED:
|
|
case ORDER_STATUS_CANCELED:
|
|
case ORDER_STATUS_COMPLETED:
|
|
case ORDER_STATUS_WORKING:
|
|
case ORDER_STATUS_PENDING_WORKING:
|
|
return;
|
|
|
|
case ORDER_STATUS_REJECTED_BY_SPARK:
|
|
case ORDER_STATUS_ERROR:
|
|
terminate_error("Unexpected child order state");
|
|
return;
|
|
|
|
case ORDER_STATUS_INACTIVE:
|
|
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
case InternalStatus::COMPLETED:
|
|
break;
|
|
case InternalStatus::INACTIVE:
|
|
case InternalStatus::W_ATTACH:
|
|
case InternalStatus::W_MD:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void BookOrderBase::update_child(api::orderman::Update &u) {
|
|
bts_assert(me->internal_status == InternalStatus::WORKING);
|
|
bts_assert(!me->child.empty());
|
|
me->child.update(u, [this](auto const &r) { on_child_update_completed(r); });
|
|
}
|
|
|
|
void BookOrderBase::update_child(Price p, Quantity q) {
|
|
Update up;
|
|
up.set_price(p.value());
|
|
up.set_quantity(q.value());
|
|
update_child(up);
|
|
}
|
|
|
|
void BookOrderBase::on_child_update_completed(OrderOpResult r) {
|
|
|
|
if (r.error == ORDER_ERROR_OK)
|
|
return;
|
|
|
|
switch (me->internal_status) {
|
|
case InternalStatus::WORKING:
|
|
on_detach();
|
|
return;
|
|
|
|
case InternalStatus::INERT:
|
|
case InternalStatus::COMPLETED:
|
|
case InternalStatus::W_MD:
|
|
case InternalStatus::W_ATTACH:
|
|
case InternalStatus::INACTIVE:
|
|
break;
|
|
}
|
|
bts_assert(false);
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::report_and_return(
|
|
api::orderman::OrderError e, std::string const &msg) {
|
|
return report_and_return({e, msg});
|
|
}
|
|
|
|
OrderOpResult BookOrderBase::report_and_return(OrderOpResult r) {
|
|
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 r;
|
|
}
|
|
|
|
void BookOrderBase::terminate_error(std::string const &error) {
|
|
me->set_external_status(ORDER_STATUS_ERROR);
|
|
if (error.size())
|
|
me->state.set_last_msg(error);
|
|
|
|
// If we attached we do not want to cancel our child
|
|
if (is_attaching())
|
|
me->child = ChildOrder{};
|
|
|
|
transition(InternalStatus::COMPLETED);
|
|
}
|
|
|
|
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::W_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::W_MD) {
|
|
|
|
// To PENDING_ATTACH
|
|
if (new_state == InternalStatus::W_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(*me->services, false);
|
|
return;
|
|
}
|
|
|
|
// To COMPLETED
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
unsubscribe_from_book();
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// From PENDING_ATTACH
|
|
if (me->internal_status == InternalStatus::W_ATTACH) {
|
|
|
|
// To WORKING
|
|
if (new_state == InternalStatus::WORKING) {
|
|
_transition(new_state);
|
|
do_on_ready(*me->services, 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);
|
|
|
|
if (r.ok()) {
|
|
// Copy the new custom parameters if any
|
|
if (me->pending_update->has_custom()) {
|
|
*me->definition.mutable_custom_def() =
|
|
me->pending_update->custom();
|
|
me->services->report_definition(me->definition);
|
|
}
|
|
} else if (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{};
|
|
me->pending_update.reset();
|
|
terminate_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 we received it
|
|
// as a regular child state update. This makes the code
|
|
// easier since we do not have to distinguish the attach case
|
|
// from the regular one.
|
|
if (me->internal_status == InternalStatus::WORKING) {
|
|
on_child_order_update(me->child.key(), me->child_state);
|
|
}
|
|
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;
|
|
}
|
|
|
|
// To INERT
|
|
if (new_state == InternalStatus::INERT) {
|
|
me->pending_update.reset();
|
|
unsubscribe_from_book();
|
|
_transition(new_state);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// From INERT
|
|
if (me->internal_status == InternalStatus::INERT) {
|
|
if (new_state == InternalStatus::COMPLETED) {
|
|
if (!me->child.empty()) {
|
|
me->child.cancel();
|
|
me->child = ChildOrder{};
|
|
}
|
|
_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::W_MD:
|
|
bts_assert(me->security);
|
|
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::W_ATTACH:
|
|
bts_assert(me->security);
|
|
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->security);
|
|
bts_assert(me->external_status() == ORDER_STATUS_WORKING);
|
|
bts_assert(!never_saw_md());
|
|
break;
|
|
|
|
case InternalStatus::INERT:
|
|
bts_assert(!me->child.empty());
|
|
bts_assert(me->external_status() == ORDER_STATUS_WORKING);
|
|
bts_assert(!never_saw_md());
|
|
bts_assert(me->book_sid.empty());
|
|
bts_assert(!me->pending_update);
|
|
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->pending_update);
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool BookOrderBase::never_saw_md() {
|
|
return me->book_at_inception.ask()[0].size() == 0 &&
|
|
me->book_at_inception.bid()[0].size() == 0 &&
|
|
me->last_book.ask()[0].size() == 0 &&
|
|
me->last_book.bid()[0].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();
|
|
}
|
|
|
|
api::orderman::OrderDefinition const &BookOrderBase::definition() const {
|
|
return me->definition;
|
|
}
|
|
|
|
api::orderman::OrderState &BookOrderBase::state() { return me->state; }
|
|
api::orderman::OrderState const &BookOrderBase::state() const {
|
|
return me->state;
|
|
}
|
|
|
|
void BookOrderBase::cancel(std::string const &msg) {
|
|
bts_assert(me->internal_status == InternalStatus::WORKING);
|
|
terminate(ORDER_STATUS_CANCELED, msg);
|
|
report_and_return();
|
|
}
|
|
|
|
void BookOrderBase::detach(std::string const &msg) {
|
|
bts_assert(me->internal_status == InternalStatus::WORKING);
|
|
bts_assert(!me->child.empty());
|
|
|
|
if (parent_key() == null_order_key()) {
|
|
if (msg.size())
|
|
me->state.set_last_msg(msg);
|
|
me->child = ChildOrder{};
|
|
terminate(ORDER_STATUS_CANCELED);
|
|
} else
|
|
transition(InternalStatus::INERT);
|
|
|
|
report_and_return();
|
|
}
|
|
|
|
OrderKey BookOrderBase::child_key() const { return me->child.key(); }
|
|
|
|
Book const &BookOrderBase::last_book() const { return me->last_book; }
|
|
|
|
Book const &BookOrderBase::book_at_inception() const {
|
|
return me->book_at_inception;
|
|
}
|
|
|
|
Price BookOrderBase::tick_size() const {
|
|
bts_assert(me->internal_status == InternalStatus::WORKING);
|
|
return Price(me->security->contract_spec().low_tick());
|
|
}
|
|
|
|
md::PriceSize BookOrderBase::other_side_top_of_book() const {
|
|
return last_book().side(other_side())[0];
|
|
}
|
|
|
|
md::PriceSize BookOrderBase::my_side_top_of_book() const {
|
|
return last_book().side(my_side())[0];
|
|
}
|
|
|
|
api::Side BookOrderBase::my_side() const { return definition().side(); }
|
|
|
|
api::Side BookOrderBase::other_side() const {
|
|
return securities::other_side(my_side());
|
|
}
|
|
|
|
void BookOrderBase::launch_child(
|
|
api::orderman::OrderDefinition const &child_def) {
|
|
bts_assert(me->internal_status == InternalStatus::WORKING);
|
|
bts_assert(me->child.empty());
|
|
me->child = ChildOrder{child_def, *me->services};
|
|
me->child.activate(Price(me->state.price()), Quantity(me->state.quantity()));
|
|
}
|
|
|
|
log::CharPtrWrapper BookOrderBase::state_name(InternalStatus st) {
|
|
switch (st) {
|
|
case InternalStatus::W_MD:
|
|
return {"W_MD"};
|
|
case InternalStatus::COMPLETED:
|
|
return {"COMPLETED"};
|
|
case InternalStatus::W_ATTACH:
|
|
return {"W_ATTACH"};
|
|
case InternalStatus::INACTIVE:
|
|
return {"INACTIVE"};
|
|
case InternalStatus::WORKING:
|
|
return {"WORKING"};
|
|
case InternalStatus::INERT:
|
|
return {"INERT"};
|
|
}
|
|
bts_assert(false);
|
|
return {""};
|
|
}
|
|
|
|
} // namespace orderman
|
|
} // namespace bts
|