From eb053ee99dc13cc59ba0f2e9c6ca2ceccac2d4f8 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 2 Apr 2019 16:36:24 -0400 Subject: [PATCH] Cleaned up algos. --- .../orders/book_algos/BookOrderBase.cpp | 71 ++++++++++++++-- .../orders/book_algos/BookOrderBase.h | 5 ++ .../orderman/orders/book_algos/JoinOrder2.cpp | 85 +++++++++---------- .../orderman/orders/book_algos/JoinOrder2.h | 1 + .../orderman/orders/book_algos/LeanOrder2.cpp | 24 ++---- .../orderman/orders/book_algos/LeanOrder2.h | 11 +-- 6 files changed, 120 insertions(+), 77 deletions(-) diff --git a/src/lib/orderman/orders/book_algos/BookOrderBase.cpp b/src/lib/orderman/orders/book_algos/BookOrderBase.cpp index 052daf16f..58c4259ac 100644 --- a/src/lib/orderman/orders/book_algos/BookOrderBase.cpp +++ b/src/lib/orderman/orders/book_algos/BookOrderBase.cpp @@ -1,4 +1,5 @@ #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" @@ -16,6 +17,7 @@ enum class BookOrderBase::InternalStatus { PENDING_ATTACH, WAITING_FOR_MD, WORKING, + INERT, COMPLETED }; @@ -117,6 +119,7 @@ OrderOpResult BookOrderBase::on_activate(Price p, Quantity q, case InternalStatus::WORKING: case InternalStatus::WAITING_FOR_MD: + case InternalStatus::INERT: bts_assert(false); break; } @@ -137,6 +140,7 @@ OrderOpResult BookOrderBase::on_update(Update const &update) { 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()) { @@ -172,6 +176,7 @@ orderman::OrderOpResult BookOrderBase::on_detach() { case InternalStatus::WAITING_FOR_MD: case InternalStatus::PENDING_ATTACH: case InternalStatus::WORKING: + case InternalStatus::INERT: me->child = ChildOrder{}; terminate(ORDER_STATUS_CANCELED); break; @@ -191,6 +196,7 @@ orderman::OrderOpResult BookOrderBase::on_cancel() { case InternalStatus::WAITING_FOR_MD: case InternalStatus::PENDING_ATTACH: case InternalStatus::WORKING: + case InternalStatus::INERT: terminate(ORDER_STATUS_CANCELED); return report_and_return(); @@ -235,6 +241,7 @@ void BookOrderBase::on_book_update(const BookUpdateEvent &ev) { case InternalStatus::PENDING_ATTACH: case InternalStatus::COMPLETED: + case InternalStatus::INERT: return; case InternalStatus::INACTIVE: @@ -284,6 +291,7 @@ void BookOrderBase::on_attach_complete(OrderOpResult r, ChildOrder const &child, case InternalStatus::INACTIVE: case InternalStatus::WAITING_FOR_MD: case InternalStatus::WORKING: + case InternalStatus::INERT: bts_assert(false); break; } @@ -297,6 +305,7 @@ void BookOrderBase::on_child_order_update(OrderStateUpdateEvent const &ev) { void BookOrderBase::on_child_order_update( api::orderman::OrderState const &child_state) { switch (me->internal_status) { + case InternalStatus::INERT: case InternalStatus::WORKING: switch (child_state.status()) { @@ -330,7 +339,7 @@ void BookOrderBase::on_child_order_update( case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_: bts_assert(false); return; - } // End JOINED internal state + } return; case InternalStatus::COMPLETED: break; @@ -365,9 +374,8 @@ void BookOrderBase::on_child_update_completed(OrderOpResult r) { on_detach(); return; + case InternalStatus::INERT: case InternalStatus::COMPLETED: - return; - case InternalStatus::WAITING_FOR_MD: case InternalStatus::PENDING_ATTACH: case InternalStatus::INACTIVE: @@ -517,6 +525,26 @@ void BookOrderBase::transition(InternalStatus new_state) { _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 @@ -556,6 +584,14 @@ void BookOrderBase::check_invariants() { 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 || @@ -563,7 +599,6 @@ void BookOrderBase::check_invariants() { 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(!me->pending_update); break; } @@ -601,8 +636,16 @@ void BookOrderBase::cancel(std::string const &msg) { void BookOrderBase::detach(std::string const &msg) { bts_assert(me->internal_status == InternalStatus::WORKING); - me->child = {}; - terminate(ORDER_STATUS_CANCELED, msg); + 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(); } @@ -617,6 +660,20 @@ Price BookOrderBase::tick_size() const { 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); @@ -637,6 +694,8 @@ log::CharPtrWrapper BookOrderBase::state_name(InternalStatus st) { return {"INACTIVE"}; case InternalStatus::WORKING: return {"WORKING"}; + case InternalStatus::INERT: + return {"INERT"}; } bts_assert(false); return {""}; diff --git a/src/lib/orderman/orders/book_algos/BookOrderBase.h b/src/lib/orderman/orders/book_algos/BookOrderBase.h index 3f73d35e0..92275ca6a 100644 --- a/src/lib/orderman/orders/book_algos/BookOrderBase.h +++ b/src/lib/orderman/orders/book_algos/BookOrderBase.h @@ -3,6 +3,7 @@ #include "lib/api/services/OrderService.server.pb.h" #include "lib/logging/logging.h" #include "lib/orderman/orders/Order.h" +#include "lib/md/PriceSize.h" #include @@ -47,6 +48,10 @@ class BookOrderBase : public Order { Book const&last_book() const; Book const&book_at_inception() const; Price tick_size() const; + api::Side my_side() const; + api::Side other_side() const; + md::PriceSize other_side_top_of_book() const; + md::PriceSize my_side_top_of_book() const; private: enum class InternalStatus; diff --git a/src/lib/orderman/orders/book_algos/JoinOrder2.cpp b/src/lib/orderman/orders/book_algos/JoinOrder2.cpp index 3bec00d16..ae97343f2 100644 --- a/src/lib/orderman/orders/book_algos/JoinOrder2.cpp +++ b/src/lib/orderman/orders/book_algos/JoinOrder2.cpp @@ -8,7 +8,7 @@ namespace orderman { using namespace api::orderman; -enum class JoinOrder2::Status { WAITING_TO_JOIN, JOINED, DONE }; +enum class JoinOrder2::Status { WAITING_TO_JOIN, JOINED }; struct JoinOrder2::PImpl { Quantity min_quantity_to_join; @@ -46,7 +46,7 @@ void JoinOrder2::do_on_ready(bool is_attached) { Quantity(state().quantity())); if (!is_attached) { - auto const&def = definition(); + auto const &def = definition(); api::orderman::OrderDefinition child_def = def; *child_def.mutable_custom_def() = definition().custom_def().join().child(); @@ -81,59 +81,56 @@ void JoinOrder2::try_to_join() { child_update.set_quantity(state().quantity()); update_child(child_update); - // We no longer need market data updates - unsubscribe_from_book(); - - me->status = Status::JOINED; - LOG(OrderMan, Info, "JoinOrder2 order {} joined the market {}@{}", - key().value(), state().quantity(), my_side_psz.price()); + done(); + } } OrderOpResult JoinOrder2::do_on_update(api::orderman::Update const &update) { - switch (me->status) { - case Status::WAITING_TO_JOIN: - case Status::JOINED: { - if (update.has_custom() && !update.custom().has_join()) - return {ORDER_ERROR_INVALID_OP, - "Update is not valid for this order type"}; + if (update.has_custom() && !update.custom().has_join()) + return {ORDER_ERROR_INVALID_OP, + "Update is not valid for this order type"}; - // copy new price, size to our state - state().set_price(update.price()); - state().set_quantity(update.quantity()); + // 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()); + // 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 &join_update = update.custom().join(); - *child_update.mutable_custom() = join_update.child(); + if (update.has_custom()) { + // process custom part of the udpate if any + auto &join_update = update.custom().join(); + *child_update.mutable_custom() = join_update.child(); - // Update quantity threshold - me->min_quantity_to_join = SpreaderUtils::_quantity_threshold( - definition().custom_def().join().join_threshold(), - Quantity(update.quantity())); - } - - update_child(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(); - return OrderOpResult(); - } - case Status::DONE: - bts_assert(false); - break; + // Update quantity threshold + me->min_quantity_to_join = SpreaderUtils::_quantity_threshold( + definition().custom_def().join().join_threshold(), + Quantity(update.quantity())); } - bts_assert(false); - return OrderOpResult{}; + update_child(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(); + return OrderOpResult(); } +void JoinOrder2::done() +{ + bts_assert(me->status == Status::WAITING_TO_JOIN); + auto my_side = definition().side(); + auto &my_side_psz = last_book().side(my_side)[0]; + + me->status = Status::JOINED; + LOG(OrderMan, Diag, "JoinOrder2 order {} joined the market {}@{}", + key().value(), state().quantity(), my_side_psz.price()); + detach("Joined successfully"); +} + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/book_algos/JoinOrder2.h b/src/lib/orderman/orders/book_algos/JoinOrder2.h index 89089e107..717fa6afc 100644 --- a/src/lib/orderman/orders/book_algos/JoinOrder2.h +++ b/src/lib/orderman/orders/book_algos/JoinOrder2.h @@ -11,6 +11,7 @@ class JoinOrder2 : public BookOrderBase { ~JoinOrder2(); private: + void done(); OrderOpResult do_on_init_new() override; void do_on_book(Book const &) override; void do_on_ready(bool is_attached) override; diff --git a/src/lib/orderman/orders/book_algos/LeanOrder2.cpp b/src/lib/orderman/orders/book_algos/LeanOrder2.cpp index 8e169f3e5..bdaa6cb4d 100644 --- a/src/lib/orderman/orders/book_algos/LeanOrder2.cpp +++ b/src/lib/orderman/orders/book_algos/LeanOrder2.cpp @@ -117,7 +117,7 @@ void LeanOrder2::on_toward_me_move() { LOG(OrderMan, Diag, "LeanOrder2 {} is done because opposite moved towards order", key().value()); - done(); + done("Opposite market moved towards"); break; case LeanOrderDefinition_OnToward_REPRICE: me->target_price = Price(other_side_top_of_book().price()); @@ -160,7 +160,7 @@ void LeanOrder2::payup() { "Could not find opposite market to set new target price in " "LeanOrder2, order: {} is done.", key().value()); - done(); + done("Could not find new opposite market"); return; } @@ -175,7 +175,7 @@ void LeanOrder2::payup() { LOG(OrderMan, Diag, "Reached limit of jumps in LeanOrder2 {}. Order is done.", key().value()); - done(); + done("Max number of jumps reached"); return; } } @@ -206,23 +206,17 @@ OrderOpResult LeanOrder2::do_on_update(api::orderman::Update const &update) { update_child(child_update); if (me->status == Status::WORKING) { - // re-eval given that our parameters might hace changed + // re-eval given that our parameters might have changed try_jump(); } return OrderOpResult(); } -void LeanOrder2::done() +void LeanOrder2::done(std::string const&msg) { bts_assert(me->status == Status::WORKING); me->status = Status::DONE; - - if (parent_key() == null_order_key()) - detach(); -} - -md::PriceSize LeanOrder2::other_side_top_of_book() const { - return last_book().side(other_side())[0]; + detach(msg); } bool LeanOrder2::book_moved_toward_me() const { @@ -237,12 +231,6 @@ bool LeanOrder2::book_moved_away_from_me() const { other_side_price); } -api::Side LeanOrder2::my_side() const { return definition().side(); } - -api::Side LeanOrder2::other_side() const { - return securities::other_side(my_side()); -} - uint32_t LeanOrder2::max_jumps() const { return definition().custom_def().lean().max_chase_ticks(); } diff --git a/src/lib/orderman/orders/book_algos/LeanOrder2.h b/src/lib/orderman/orders/book_algos/LeanOrder2.h index 103ac7b5e..6a445dcd6 100644 --- a/src/lib/orderman/orders/book_algos/LeanOrder2.h +++ b/src/lib/orderman/orders/book_algos/LeanOrder2.h @@ -1,7 +1,6 @@ #pragma once #include "lib/orderman/orders/book_algos/BookOrderBase.h" -#include "lib/md/PriceSize.h" namespace bts { namespace orderman { @@ -19,21 +18,15 @@ private: struct PImpl; enum class Status; - api::Side my_side() const; - api::Side other_side() const; - md::PriceSize other_side_top_of_book() const; spark::QuantityMeasure jump_threshold() const; uint32_t max_jumps() const ; api::orderman::LeanOrderDefinition::OnToward on_toward_behaviour() const; - bool book_moved_toward_me() const; bool book_moved_away_from_me() const; - - void try_jump(); void on_toward_me_move(); + void try_jump(); void payup(); - - void done(); + void done(std::string const&msg); std::unique_ptr me; };