#include "lib/orderman/orders/book_algos/TopOrder.h" #include "lib/domain_model/Security.h" #include "lib/logging/logging.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orderman_utils/Book.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" #include "lib/utils/PBUtils.h" namespace bts { namespace orderman { using namespace api::orderman; struct TopOrder::PImpl { spark::QuantityMeasure top_threshold_measure; OrderKey child_key = null_order_key(); ChildTemplate *child_template = nullptr; Book last_book; }; TopOrder::TopOrder() : me(new PImpl()) {} TopOrder::~TopOrder() {} OrderOpResult TopOrder::do_before_launch() { 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(); if (!is_attaching()) { // if we are not attaching create a child template so we can // quickly launch our child when the time comes. auto child_def = definition(); *child_def.mutable_custom_def() = definition().custom_def().top().child(); me->child_template = &(make_child_template(child_def)); } return {}; } 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) { me->child_key = k; subscribe_book(SecurityKey(definition().security_key())); subscribe_child_change(k); on_child_change(k, st); } void TopOrder::do_update(api::orderman::Update const &update) { LOG(OrderMan, Diag, "Top order {} receiving update: {}", order_key().value(), utils::pb::to_string(update)); if (update.has_custom() && !update.custom().has_top()) { terminate_error("Invalid update"); return; } 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(me->child_key, child_update); } void TopOrder::on_book(SecurityKey, Book const &b) { me->last_book = b; ensure_min_quantity(); } void TopOrder::on_child_change(OrderKey, ChildState const &st) { update_fill_state(st.total_fills, st.last_price); switch (st.status) { case ChildStatus::WORKING: return; case ChildStatus::COMPLETED: terminate_complete(); return; case ChildStatus::CANCELED: terminate_cancel("Unexpected child cancel"); return; } } bool TopOrder::ensure_min_quantity() { LOG(OrderMan, Diag, "Top order {} received book update. Current top of book: {}@{}", order_key().value(), top_of_book_quantity().value(), top_of_book_price().value()); if (better_than_top()) { LOG(OrderMan, Diag, "Top order {} became better than the top of the book, canceling", order_key().value()); terminate_cancel("Order would become BBO"); return false; } if (at_top_of_book()) { auto total_min_quantity = quantity().value() + 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: {}", order_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", order_key().value()); terminate_cancel("Top of book is below threshold"); return false; } } else LOG(OrderMan, Diag, "Top order {} is not at the top of the book. Order: {} Book: {}", order_key().value(), price().value(), top_of_book_price().value()); return true; } bool TopOrder::at_top_of_book() const { return securities::is_same(price(), top_of_book_price()); } bool TopOrder::better_than_top() const { auto my_side = definition().side(); return securities::is_better(my_side, price(), top_of_book_price()); } Quantity TopOrder::min_quantity_at_top() const { return SpreaderUtils::_quantity_threshold(me->top_threshold_measure, quantity()); } Price TopOrder::top_of_book_price() const { auto my_side = definition().side(); auto &my_side_psz = me->last_book.side(my_side)[0]; return Price(my_side_psz.price()); } Quantity TopOrder::top_of_book_quantity() const { auto my_side = definition().side(); auto &my_side_psz = me->last_book.side(my_side)[0]; return Quantity(my_side_psz.size()); } } // namespace orderman } // namespace bts