diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp index ec238f1ee..7e96a1aca 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp @@ -7,13 +7,15 @@ #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" #include "lib/os/os.h" #include "lib/utils/PBUtils.h" +#include + namespace bts { namespace orderman { using namespace api::orderman; -enum class TWAPAlgo::Status { WAITING_FOR_MD, WORKING, DONE }; +enum class TWAPAlgo::Status { WAITING_FOR_MD, WAITING_FOR_START, WORKING, DONE }; enum class TWAPAlgo::Channel { Tactical, Aggressive }; struct OrderDef @@ -21,26 +23,31 @@ struct OrderDef OrderKey child_key = null_order_key(); ChildTemplate* child_template = nullptr; uint32_t generation = 0u; - Channel channel = Channel::Tactical; + TWAPAlgo::Channel channel = TWAPAlgo::Channel::Tactical; }; struct TWAPAlgo::PImpl { - IAlgoServices *algo; + IAlgoServices* algo; OrderDefinition definition; OrderDuration duration; OrderAggression aggression; Status status = Status::WAITING_FOR_MD; + google::protobuf::Timestamp start_time; ChildTemplate* tactical_template = nullptr; ChildTemplate* aggressive_template = nullptr; - + std::map child_orders; - std::map child_state; + std::map child_state; Book last_book; + TradeVector last_trades; + + uint32_t generation = 0; + TimeStamp generation_time; }; TWAPAlgo::TWAPAlgo() : me(new PImpl()) {} @@ -54,6 +61,7 @@ OrderOpResult TWAPAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) bts_assert(def.custom_def().has_twap()); // Validate the algo part of the definition auto &algo = def.custom_def().twap(); + me->generation_time = serv.get_time(); if (!algo.has_tactical_child() || algo.has_aggressive_child()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, @@ -65,11 +73,11 @@ OrderOpResult TWAPAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) // quickly launch our child when the time comes. auto tchild_def = def; *tchild_def.mutable_custom_def() = def.custom_def().twap().tactical_child(); - me->tactical_child_template = &(serv.make_child_template(tchild_def)); + me->tactical_template = &(serv.make_child_template(tchild_def)); auto achild_def = def; *achild_def.mutable_custom_def() = def.custom_def().twap().aggressive_child(); - me->aggressive_child_template = &(serv.make_child_template(achild_def)); + me->aggressive_template = &(serv.make_child_template(achild_def)); } return {}; } @@ -78,8 +86,8 @@ void TWAPAlgo::do_launch(IAlgoServices &algo) { me->algo = &algo; update_params(); - bts_assert(me->tactical_child_template); - bts_assert(me->aggressive_child_template); + bts_assert(me->tactical_template); + bts_assert(me->aggressive_template); //me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); algo.subscribe_book(SecurityKey(algo.security().id())); @@ -91,8 +99,8 @@ void TWAPAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st { me->algo = &algo; update_params(); - bts_assert(me->tactical_child_template); - bts_assert(me->aggressive_child_template); + bts_assert(me->tactical_template); + bts_assert(me->aggressive_template); me->child_orders[k] = OrderDef{ k, me->tactical_template, me->generation, Channel::Tactical }; on_child_change(k, st); @@ -102,66 +110,33 @@ void TWAPAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st algo.subscribe_timer(); } -void TWAPAlgo::do_update(api::orderman::Update const &update) +void TWAPAlgo::do_update(const api::orderman::Update& update) { LOG(OrderMan, Diag, "ScratchOrder order {} receiving update: {}", algo().order_key().value(), utils::pb::to_string(update)); - if (update.has_custom() && !update.custom().has_scratch()) + if (update.has_custom() && !update.custom().has_twap()) { algo().terminate_error("Invalid update"); return; } Update child_update; - Price price_to_pass = get_price_to_pass(Price(update.price())); - child_update.set_price(price_to_pass.value()); + child_update.set_price(update.price()); child_update.set_quantity(update.quantity()); - if (update.has_custom()) + for(auto& i : me->child_orders) { - update_params(); - auto &scratch_update = update.custom().scratch(); - *child_update.mutable_custom() = scratch_update.child(); - } - algo().update_child(me->child_key, child_update); - - // If we were not yet scratched it is possible we now can, since we - // could have changed the scratch_threshold - if (me->status == Status::WAITING_TO_SCRATCH) - try_to_scratch(); -} - -void TWAPAlgo::on_book(SecurityKey, Book const &b) -{ - me->last_book = b; - - LOG(OrderMan, Diag, "ScratchOrder order {} on book. BID: {}@{} ASK: {}@{}", - algo().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(algo().definition().side()); - auto &other_side_top = me->last_book.side(other_side)[0]; - - if (other_side_top.size() > 0) + if (update.has_custom()) { - me->status = Status::WAITING_TO_SCRATCH; - me->book_at_inception = b; - LOG(OrderMan, Diag, - "ScratchOrder order {} receiving first book: {} Target: {}", - algo().order_key().value(), me->scratch_threshold.value(), - other_side_top.price()); - try_to_scratch(); - } - } break; - case Status::SCRATCHED: - break; - case Status::WAITING_TO_SCRATCH: - try_to_scratch(); - break; + update_params(); + auto &algo_update = update.custom().twap(); + if(i.second.channel == Channel::Tactical) + *child_update.mutable_custom() = algo_update.tactical_child(); + else if(i.second.channel == Channel::Aggressive) + *child_update.mutable_custom() = algo_update.aggressive_child(); + else + bts_assert_msg(false, "unknown channel type"); + } + algo().update_child(i.first, child_update); } } @@ -186,7 +161,8 @@ void TWAPAlgo::on_child_change(OrderKey key, const ChildState& st) { me->child_state[key] = st; - uint32_t fills = 0u; bool all_complete = true, all_cancelled = true; + uint32_t fills = 0u; + bool all_complete = true, all_cancelled = true; for(const auto& ch : me->child_state) { fills += ch.second.total_fills.value(); @@ -194,13 +170,128 @@ void TWAPAlgo::on_child_change(OrderKey key, const ChildState& st) ch.second.status == ChildStatus::CANCELED); all_cancelled = all_cancelled && (ch.second.status == ChildStatus::CANCELED); } - algo_services().update_fill_state(Quantity(fills), st.last_price); + algo().update_fill_state(Quantity(fills), st.last_price); - if(all_complete) - algo_services().complete(); - else if(all_cancelled) - algo_services().terminate_error("All children cancelled"); + if(me->status == Status::DONE) + { + if(all_complete) + algo().complete(); + else if(all_cancelled) + algo().terminate_error("All children cancelled"); + } } +void TWAPAlgo::on_book(SecurityKey, const Book& b) +{ + me->last_book = b; + + LOG(OrderMan, Diag, "TWAPAlgo order {} on book. BID: {}@{} ASK: {}@{}", + algo().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: + { + me->status = Status::WAITING_FOR_START; + } + break; + case Status::WAITING_FOR_START: + case Status::WORKING: + case Status::DONE: + break; + } +} + +void TWAPAlgo::on_ticker(SecurityKey, const TradeVector& vec) +{ + me->last_trades = vec; + LOG(OrderMan, Diag, "TWAPAlgo order {} on ticker.", algo().order_key().value()); +} + + +void TWAPAlgo::on_timer() +{ + switch(me->status) + { + case Status::WAITING_FOR_MD: + { + return; + } + case Status::WAITING_FOR_START: + { + if(me->duration.has_start() && me->duration.start().seconds() > 0) + { + if (me->duration.start().seconds() < utils::pb::pb_now().seconds()) + return; + } + me->status = Status::WORKING; + me->start_time = utils::pb::pb_now(); + return; + } + case Status::WORKING: + { + if (me->duration.end_case() == OrderDuration::EndCase::kDurationInMinutes) + { + google::protobuf::Timestamp eend; + eend.set_seconds(me->start_time.seconds() + me->duration.duration_in_minutes() * 60); + if (utils::pb::pb_now().seconds() > eend.seconds()) + { + me->status = Status::DONE; + get_done(); + return; + } + } + else if (me->duration.end_case() == OrderDuration::EndCase::kEndTimestamp) + { + if (me->duration.end_timestamp().seconds() > utils::pb::pb_now().seconds()) + { + me->status = Status::DONE; + get_done(); + return; + } + } + else + { + algo().terminate_error("Unknown end case"); + return; + } + if(me->generation++ == 0) + setup(); + else + compute(); + break; + } + case Status::DONE: + break; + } +} + +void TWAPAlgo::setup() +{ + bts_assert_msg(me->generation == 0, "generation 0"); + //setup quantities +} + +void TWAPAlgo::compute() +{ + int64_t elapsed = utils::pb::pb_now().seconds() - me->start_time.seconds(); + double gen = elapsed / me->duration.discretization_interval_in_seconds(); + if(gen < me->generation) + return; + + me->generation = (uint32_t)gen; + //find tactial quantity + //convert some of tactial to aggressive +} + +void TWAPAlgo::get_done() +{ + //foreach -> market +} + + + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h index bd5cbb9b1..6f7b0f71a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h @@ -13,19 +13,28 @@ public: OrderOpResult do_before_launch(IBeforeLaunchAlgoServices&) override; void do_launch(IAlgoServices &) override; - void do_launch_attached(IAlgoServices&,OrderKey, ChildState) override; - void do_update(api::orderman::Update const &) override; - void on_book(SecurityKey, Book const &) override; - void on_child_change(OrderKey, ChildState const &) override; + void do_launch_attached(IAlgoServices&, OrderKey, ChildState) override; + void do_update(const api::orderman::Update&) override; + void on_child_change(OrderKey, const ChildState&) override; + + void on_book(SecurityKey, const Book&) override; + void on_ticker(SecurityKey, const TradeVector&) override; + void on_timer() override; private: enum class Status; enum class Channel; struct PImpl; + friend struct OrderDef; + std::unique_ptr me; IAlgoServices& algo() const; void update_params(); + + void setup(); + void compute(); + void get_done(); }; }}