#include "lib/orderman/orders/book_algos/JoinOrder.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" #include "lib/utils/PBUtils.h" namespace bts { namespace orderman { using namespace api::orderman; enum class JoinOrder::Status { WAITING_FOR_MD, WAITING_TO_JOIN, JOINED }; struct JoinOrder::PImpl { Quantity min_quantity_to_join; Status status = Status::WAITING_FOR_MD; OrderKey child_key = null_order_key(); ChildTemplate *child_template = nullptr; Book book_at_inception; Book last_book; }; JoinOrder::JoinOrder() : me(new PImpl()) {} JoinOrder::~JoinOrder() {} OrderOpResult JoinOrder::do_before_launch() { auto &def = definition(); bts_assert(def.has_custom_def()); bts_assert(def.custom_def().has_join()); // Validate the join part of the definition auto &join = def.custom_def().join(); if (!join.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, "Join order must have either a child order definition or an " "order to attach to"}; } 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().join().child(); me->child_template = &(make_child_template(child_def)); } return {}; } void JoinOrder::do_launch() { update_join_threshold(); bts_assert(me->child_template); me->child_key = launch_child(*me->child_template, price(), quantity()); subscribe_book(SecurityKey(definition().security_key())); } void JoinOrder::do_launch_attached(OrderKey k, ChildState st) { update_join_threshold(); me->child_key = k; subscribe_book(SecurityKey(definition().security_key())); on_child_change(k, st); } void JoinOrder::do_update(api::orderman::Update const &update) { LOG(OrderMan, Diag, "JoinOrder order {} receiving update: {}", order_key().value(), utils::pb::to_string(update)); if (update.has_custom() && !update.custom().has_join()) { terminate_error("Invalid update"); return; } Update child_update; child_update.set_price(update.price()); child_update.set_quantity(update.quantity()); if (update.has_custom()) { update_join_threshold(); auto &join_update = update.custom().join(); *child_update.mutable_custom() = join_update.child(); } update_child(me->child_key, child_update); // If we were not yet joined it is possible we now can, since we // could have changed the min_quantity_to_join if (me->status == Status::WAITING_TO_JOIN) try_to_join(); } void JoinOrder::on_book(SecurityKey, Book const &b) { me->last_book = b; LOG(OrderMan, Diag, "JoinOrder order {} on book. BID: {}@{} ASK: {}@{}", order_key().value(), b.side(api::SIDE_BUY)[0].size(), b.side(api::SIDE_BUY)[0].price(), b.side(api::SIDE_SELL)[0].size(), b.side(api::SIDE_SELL)[0].price()); switch (me->status) { case Status::WAITING_FOR_MD: { auto other_side = securities::other_side(definition().side()); auto &other_side_top = me->last_book.side(other_side)[0]; if (other_side_top.size() > 0) { me->status = Status::WAITING_TO_JOIN; me->book_at_inception = b; LOG(OrderMan, Diag, "JoinOrder order {} receiving first book: {} Target: {}", order_key().value(), me->min_quantity_to_join.value(), other_side_top.price()); try_to_join(); } } break; case Status::JOINED: break; case Status::WAITING_TO_JOIN: try_to_join(); break; } } void JoinOrder::try_to_join() { bts_assert(me->status == Status::WAITING_TO_JOIN); auto my_side = definition().side(); auto other_side = securities::other_side(my_side); auto &my_side_psz = me->last_book.side(my_side)[0]; auto &opposite_at_inception_psz = me->book_at_inception.side(other_side)[0]; if (my_side_psz.size() && my_side_psz.size() >= me->min_quantity_to_join.value() && securities::is_equal_or_better( my_side, Price(my_side_psz.price()), Price(opposite_at_inception_psz.price()))) { LOG(OrderMan, Diag, "JoinOrder order {} joining: {}@{}", order_key().value(), quantity().value(), my_side_psz.price()); // Time to join Update child_update; child_update.set_price(my_side_psz.price()); child_update.set_quantity(quantity().value()); update_child(me->child_key, child_update); me->status = Status::JOINED; } else { LOG(OrderMan, Diag, "JoinOrder order {} still waiting to join. Order: {}@{} Top of the " "book: {}@{}" " Target: {}@{}", order_key().value(), quantity().value(), price().value(), my_side_psz.size(), my_side_psz.price(), me->min_quantity_to_join.value(), opposite_at_inception_psz.price()); } } void JoinOrder::update_join_threshold() { me->min_quantity_to_join = SpreaderUtils::_quantity_threshold( definition().custom_def().join().join_threshold(), quantity()); } void JoinOrder::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: complete(); return; case ChildStatus::CANCELED: cancel("Unexpected child cancel"); return; } } } // namespace orderman } // namespace bts