diff --git a/src/lib/orderman/orders/algo/AlgoOrder.h b/src/lib/orderman/orders/algo/AlgoOrder.h index 67839eee3..a09137e23 100644 --- a/src/lib/orderman/orders/algo/AlgoOrder.h +++ b/src/lib/orderman/orders/algo/AlgoOrder.h @@ -39,9 +39,11 @@ class AlgoOrder : private Order { // Resolves the security identified by k, returning nullptr if no // such security exists api::Security const *lookup_security(SecurityKey k) const; - // Returns the price for this order + // Returns the price for this order. This value is not available + // from do_before_launch() Price price() const; - // Returns the quantity for this order + // Returns the quantity for this order. This value is not available + // from do_before_launch(). Quantity quantity() const; // Returns wheter this order is attaching to an existing order bool is_attaching() const; diff --git a/src/lib/orderman/orders/book_algos/TopOrder.cpp b/src/lib/orderman/orders/book_algos/TopOrder.cpp index edbd86ff9..322a6fc52 100644 --- a/src/lib/orderman/orders/book_algos/TopOrder.cpp +++ b/src/lib/orderman/orders/book_algos/TopOrder.cpp @@ -42,7 +42,7 @@ OrderOpResult TopOrder::do_before_launch() { if (!is_attaching()) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. - api::orderman::OrderDefinition child_def = definition(); + auto child_def = definition(); *child_def.mutable_custom_def() = definition().custom_def().top().child(); me->child_template = &(make_child_template(child_def)); } @@ -52,6 +52,7 @@ OrderOpResult TopOrder::do_before_launch() { void TopOrder::do_launch() { subscribe_book(SecurityKey(definition().security_key())); + me->child_key = launch_child(*me->child_template, price(), quantity()); } void TopOrder::do_launch_attached(OrderKey k, ChildState st) { @@ -80,41 +81,13 @@ void TopOrder::do_update(api::orderman::Update const &update) { auto &top_update = update.custom().top(); *child_update.mutable_custom() = top_update.child(); me->top_threshold_measure = top_update.top_threshold(); - - // if we have not yet launched our child we need to update - // our child template - if (!has_running_child()) { - bts_assert(me->child_template != nullptr); - api::orderman::OrderDefinition child_def = definition(); - *child_def.mutable_custom_def() = - definition().custom_def().top().child(); - me->child_template = &(make_child_template(child_def)); - } } update_child(me->child_key, child_update); } void TopOrder::on_book(SecurityKey, Book const &b) { me->last_book = b; - if (has_running_child()) - ensure_min_quantity(); - else { - // If we do not have a running child, because we had not yet - // received a book, we have to make sure its OK to launch it. - if (better_than_top()) { - terminate_cancel("Order would become BBO on inception"); - return; - } - - if (at_top_of_book() && top_of_book_quantity() < min_quantity_at_top()) { - terminate_cancel("Top of book is below threshold"); - return; - } - - // If we reached this point we are clear to launch our child - bts_assert(me->child_template); - me->child_key = launch_child(*me->child_template, price(), quantity()); - } + ensure_min_quantity(); } void TopOrder::on_child_change(OrderKey, ChildState const &st) { @@ -184,10 +157,6 @@ Quantity TopOrder::min_quantity_at_top() const { quantity()); } -bool TopOrder::has_running_child() const { - return me->child_key != null_order_key(); -} - Price TopOrder::top_of_book_price() const { auto my_side = definition().side(); auto &my_side_psz = me->last_book.side(my_side)[0]; diff --git a/src/lib/orderman/orders/book_algos/TopOrder.h b/src/lib/orderman/orders/book_algos/TopOrder.h index 42b5b884b..0ab80a1bf 100644 --- a/src/lib/orderman/orders/book_algos/TopOrder.h +++ b/src/lib/orderman/orders/book_algos/TopOrder.h @@ -28,7 +28,6 @@ class TopOrder : public AlgoOrder { bool better_than_top() const; Quantity min_quantity_at_top() const; bool ensure_min_quantity(); - bool has_running_child() const; Price top_of_book_price() const; Quantity top_of_book_quantity() const; }; diff --git a/src/lib/orderman/orders/book_algos/TopOrder2.cpp b/src/lib/orderman/orders/book_algos/TopOrder2.cpp deleted file mode 100644 index 800575450..000000000 --- a/src/lib/orderman/orders/book_algos/TopOrder2.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// READY TO REVIEW - -#include "TopOrder2.h" -#include "lib/domain_model/Security.h" -#include "lib/orderman/order_factory/IOrderFactory.h" -#include "lib/orderman/orderman_utils/Book.h" -#include "lib/orderman/orderman_utils/orderman_utils.h" -#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" -#include "lib/utils/PBUtils.h" - -namespace bts { -namespace orderman { - -using namespace api::orderman; - -struct TopOrder2::PImpl { - spark::QuantityMeasure top_threshold_measure; -}; - -TopOrder2::TopOrder2() : me(new PImpl()) {} -TopOrder2::~TopOrder2() {} - -OrderOpResult TopOrder2::do_on_init_new(const PreActivationOrderServices&) { - auto &def = definition(); - bts_assert(def.has_custom_def()); - bts_assert(def.custom_def().has_top()); - - // validate the top part of the definition - auto &top = def.custom_def().top(); - if (!top.has_child() && - OrderKey(def.attach_target_order_key()) == null_order_key()) { - return OrderOpResult{ - ORDER_ERROR_INVALID_ORDER_DEFINITON, - "Top order must have either a child order definition or an " - "order to attach to"}; - } - - me->top_threshold_measure = top.top_threshold(); - - // set the custom component of our state - state().mutable_top(); - return OrderOpResult{}; -} - -void TopOrder2::do_on_ready(OrderServices&,bool is_attached) { - - if (!is_attached) { - - if (better_than_top()) { - cancel("Order would become BBO on inception"); - return; - } - - if (at_top_of_book() && top_of_book_quantity() < min_quantity_at_top()) { - cancel("Top of book is below threshold"); - return; - } - - api::orderman::OrderDefinition child_def = definition(); - *child_def.mutable_custom_def() = definition().custom_def().top().child(); - launch_child(child_def); - return; - } - check_min_quantity(); -} - -void TopOrder2::do_on_book(Book const &) { check_min_quantity(); } - -void TopOrder2::check_min_quantity() { - LOG(OrderMan, Diag, "Checking min quantity in top order {}", key().value()); - - if (better_than_top()) { - LOG(OrderMan, Diag, - "Top order {} became better than the top of the book, canceling", - key().value()); - cancel("Order would become BBO"); - return; - } - - if (at_top_of_book()) { - auto total_min_quantity = - state().quantity() + min_quantity_at_top().value(); - LOG(OrderMan, Diag, - "Top order {} is now at the top of the book, min quantity for book " - "is: {}" - " book quantity is: {}", - key().value(), total_min_quantity, top_of_book_quantity().value()); - if (top_of_book_quantity() < Quantity(total_min_quantity)) { - LOG(OrderMan, Diag, - "Top order {} is canceling because min quantiy is not met", - key().value()); - cancel("Top of book is below threshold"); - } - } -} - -OrderOpResult TopOrder2::do_on_update(api::orderman::Update const &update) { - // validate update - if (update.has_custom() && !update.custom().has_top()) - return {ORDER_ERROR_INVALID_OP, - "Update is not valid for this order type"}; - - LOG(OrderMan, Diag, "Top order {} receiving update: {}", key().value(), - utils::pb::to_string(update)); - - // Copy new price, size to our state - state().set_price(update.price()); - state().set_quantity(update.quantity()); - - // Build child update - Update child_update; - child_update.set_price(update.price()); - child_update.set_quantity(update.quantity()); - if (update.has_custom()) { - // process custom part of the udpate if any - auto &top_update = update.custom().top(); - *child_update.mutable_custom() = top_update.child(); - me->top_threshold_measure = top_update.top_threshold(); - } - update_child(child_update); - check_min_quantity(); - return OrderOpResult(); -} - -bool TopOrder2::at_top_of_book() const { - auto my_side = definition().side(); - auto &my_side_psz = last_book().side(my_side)[0]; - - return securities::is_same(Price(state().price()), - Price(my_side_psz.price())); -} - -bool TopOrder2::better_than_top() const { - auto my_side = definition().side(); - auto &my_side_psz = last_book().side(my_side)[0]; - - return securities::is_better(my_side, Price(state().price()), - Price(my_side_psz.price())); -} - -Quantity TopOrder2::top_of_book_quantity() const { - auto my_side = definition().side(); - auto &my_side_psz = last_book().side(my_side)[0]; - return Quantity(my_side_psz.size()); -} - -Quantity TopOrder2::min_quantity_at_top() const { - return SpreaderUtils::_quantity_threshold(me->top_threshold_measure, - Quantity(state().quantity())); -} - -} // namespace orderman -} // namespace bts diff --git a/src/lib/orderman/orders/book_algos/TopOrder2.h b/src/lib/orderman/orders/book_algos/TopOrder2.h deleted file mode 100644 index 62f2cb0f4..000000000 --- a/src/lib/orderman/orders/book_algos/TopOrder2.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "lib/orderman/orders/book_algos/BookOrderBase.h" - -namespace bts { -namespace orderman { - -class TopOrder2 : public BookOrderBase { - public: - TopOrder2(); - ~TopOrder2(); - -private: - OrderOpResult do_on_init_new(const PreActivationOrderServices&) override; - void do_on_ready(OrderServices&,bool is_attached) override; - void do_on_book(Book const &) override; - OrderOpResult do_on_update(api::orderman::Update const &) override; - struct PImpl; - std::unique_ptr me; - - bool at_top_of_book() const; - bool better_than_top() const; - Quantity top_of_book_quantity() const; - Quantity min_quantity_at_top() const ; - void check_min_quantity() ; -}; - -} // namespace orderman -} // namespace bts