From ba3439b0494ee7d0857aeaa58b352247d44ddfa4 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 5 Aug 2019 18:21:03 -0400 Subject: [PATCH] twap --- src/lib/api/services/OrderService.proto | 37 ++-- .../algo/algo_orders/bluex/TWAPAlgo.cpp | 173 ++++++++---------- .../orders/algo/algo_orders/bluex/TWAPAlgo.h | 5 +- 3 files changed, 104 insertions(+), 111 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 8f483e961..c93149d9d 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -256,52 +256,67 @@ message OrderAggression { double i_would_price = 20; // default = 0.0, risk neutral, -1.0 to 1.0. nondimensional parameter. -1 is completely risk averse, 1 is risk seeking // this has the net effect of tilting Arrival and VWAP schedules to do more earlier the more negative the parameter - double risk_threshold = 30; //Constant * lambda in Almgren 2000; Konishi 2002 + // Constant * lambda in Almgren 2000; Konishi 2002 + double risk_threshold = 30; // default = 1, time in periods which to let unexecuted size remain // 0 means unexecuted size must be resolved (paid up) by end of initial period-- this means orders can only work within their period // 1 means unexecuted size must be resolved (paid up) by end of next period-- this establishes two tracks: 'tactical' and 'aggressive' // orders start in the tactical track and are moved to the aggressive once they work beyond their slippage thresh + // 2...etc // means unexecuted size can live until end of order - int32 schedule_slippage = 40; // 1 => Markov, Mazur, Saltz 2014 + int32 tactical_schedule_slippage = 50; } message VolumeDistribution { // default = 20, historical volume distribution time horizon uint32 volume_time_horizon_days = 10; - - // default = 0.0, override the computed volume variance to determine optimal - // passive vs aggressive 'buckets', 0.0 means use historical variance + + // default = 0.0 means use historical mean, another value overrides the computed volume mean + double volume_mean_override = 15; + // default = 0.0 means use historical variance, another value overrides the computed volume variance double volume_variance_override = 20; } message PriceDistribution { - // default = 5 biz, 7 cal (weekly), historical price distribution time horizon + // default = 5 biz, 7 cal (weekly), historical price distribution time horizon uint32 price_time_horizon_days = 10; - // default = 0.0, override the computed price variance to determine optimal - // passive vs aggressive 'buckets', 0.0 means use historical variance + + // default = 0.0 means use historical mean, another value overrides the computed price mean + double price_mean_override = 15; + // default = 0.0 means use historical variance, another value overrides the computed price variance double price_variance_override = 20; } message TWAPOrderDefinition { OrderDuration duration = 10; OrderAggression aggression = 20; + + CustomOrderDefinition tactical_child = 200; + CustomOrderDefinition aggressive_child = 210; } message VWAPOrderDefinition { OrderDuration duration = 10; OrderAggression aggression = 20; VolumeDistribution volume_distribution = 30; - - double vol_correlation = 100; - PriceDistribution price_distribution = 110; + PriceDistribution price_distribution = 40; + + CustomOrderDefinition tactical_child = 200; + CustomOrderDefinition aggressive_child = 210; } message POVOrderDefinition { double min_participation_pct = 10; OrderDuration duration = 20; OrderAggression aggression = 30; + + CustomOrderDefinition tactical_child = 200; + CustomOrderDefinition aggressive_child = 210; } message ArrivalPriceOrderDefinition { OrderDuration duration = 10; OrderAggression aggression = 20; VolumeDistribution volume_distribution = 30; PriceDistribution price_distribution = 40; + + CustomOrderDefinition tactical_child = 200; + CustomOrderDefinition aggressive_child = 210; } message DynamicOrderDefinition { 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 0f04c7fb1..ec238f1ee 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.cpp @@ -1,6 +1,7 @@ #include "lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h" #include "lib/orderman/orders/algo/IAlgoServices.h" #include "lib/orderman/orders/algo/DynamicHelpers.h" +#include "lib/api/services/OrderService.pb.h" #include "lib/orderman/order_factory/IOrderFactory.h" #include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h" @@ -12,21 +13,34 @@ namespace orderman { using namespace api::orderman; -enum class TWAPAlgo::Status { WAITING_FOR_MD, WAITING_TO_SCRATCH, SCRATCHED }; +enum class TWAPAlgo::Status { WAITING_FOR_MD, WORKING, DONE }; +enum class TWAPAlgo::Channel { Tactical, Aggressive }; + +struct OrderDef +{ + OrderKey child_key = null_order_key(); + ChildTemplate* child_template = nullptr; + uint32_t generation = 0u; + Channel channel = Channel::Tactical; +}; struct TWAPAlgo::PImpl { IAlgoServices *algo; OrderDefinition definition; - Quantity scratch_threshold; - Price chase_diff; + OrderDuration duration; + OrderAggression aggression; + Status status = Status::WAITING_FOR_MD; - OrderKey child_key = null_order_key(); - ChildTemplate *child_template = nullptr; - Book last_book; - Book book_at_inception; - Price scratch_price=Price(os::nan()); + + ChildTemplate* tactical_template = nullptr; + ChildTemplate* aggressive_template = nullptr; + + std::map child_orders; + std::map child_state; + + Book last_book; }; TWAPAlgo::TWAPAlgo() : me(new PImpl()) {} @@ -37,21 +51,25 @@ OrderOpResult TWAPAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { const auto& def = serv.definition(); bts_assert(def.has_custom_def()); - bts_assert(def.custom_def().has_scratch()); - // Validate the scratch part of the definition - auto &scratch = def.custom_def().scratch(); - if (!scratch.has_child() && OrderKey(def.attach_target_order_key()) == null_order_key()) + bts_assert(def.custom_def().has_twap()); + // Validate the algo part of the definition + auto &algo = def.custom_def().twap(); + if (!algo.has_tactical_child() || algo.has_aggressive_child()) { return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, - "Scratch order must have either a child order definition or an order to attach to"}; + "Algo order must have a tactical and aggressive child order definition"}; } if (!serv.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 = def; - *child_def.mutable_custom_def() = def.custom_def().scratch().child(); - me->child_template = &(serv.make_child_template(child_def)); + 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)); + + 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)); } return {}; } @@ -59,19 +77,29 @@ OrderOpResult TWAPAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) void TWAPAlgo::do_launch(IAlgoServices &algo) { me->algo = &algo; - update_scratch_threshold(); - bts_assert(me->child_template); - me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); + update_params(); + bts_assert(me->tactical_child_template); + bts_assert(me->aggressive_child_template); + + //me->child_key = algo.launch_child(*me->child_template, algo.price(), algo.quantity()); algo.subscribe_book(SecurityKey(algo.security().id())); + algo.subscribe_ticker(SecurityKey(algo.security().id())); + algo.subscribe_timer(); } void TWAPAlgo::do_launch_attached(IAlgoServices &algo, OrderKey k, ChildState st) { me->algo = &algo; - update_scratch_threshold(); - me->child_key = k; - algo.subscribe_book(SecurityKey(algo.security().id())); + update_params(); + bts_assert(me->tactical_child_template); + bts_assert(me->aggressive_child_template); + + me->child_orders[k] = OrderDef{ k, me->tactical_template, me->generation, Channel::Tactical }; on_child_change(k, st); + + algo.subscribe_book(SecurityKey(algo.security().id())); + algo.subscribe_ticker(SecurityKey(algo.security().id())); + algo.subscribe_timer(); } void TWAPAlgo::do_update(api::orderman::Update const &update) @@ -90,7 +118,7 @@ void TWAPAlgo::do_update(api::orderman::Update const &update) child_update.set_quantity(update.quantity()); if (update.has_custom()) { - update_scratch_threshold(); + update_params(); auto &scratch_update = update.custom().scratch(); *child_update.mutable_custom() = scratch_update.child(); } @@ -137,90 +165,41 @@ void TWAPAlgo::on_book(SecurityKey, Book const &b) } } -void TWAPAlgo::try_to_scratch() -{ - bts_assert(me->status == Status::WAITING_TO_SCRATCH); - auto my_side = algo().definition().side(); - auto other_side = securities::other_side(my_side); - - Quantity my_quan; - auto &my_side_psz = me->last_book.side(my_side)[0]; - auto &other_side_psz = me->last_book.side(other_side)[0]; - auto other_side_price = Price(other_side_psz.price()); - auto inception_other_side_price = Price(me->book_at_inception.side(other_side)[0].price()); - - bool market_moved = !securities::is_equal_or_better(other_side, other_side_price, inception_other_side_price); - bool on_market = securities::is_same(Price(my_side_psz.price()), Price(algo().price())); - if (market_moved || (on_market && my_side_psz.size() <= me->scratch_threshold.value()) ) - { - Price scratch_price = securities::worsen_price(other_side, inception_other_side_price, me->chase_diff); - LOG(OrderMan, Diag, "ScratchOrder order {} scratching: {}@{}", - algo().order_key().value(), algo().quantity().value(), scratch_price); - // Time to scratch - Update child_update; - me->scratch_price = scratch_price; - child_update.set_price(scratch_price.value()); - //we still want to try to scratch all of it - child_update.set_quantity(algo().quantity().value()); - algo().update_child(me->child_key, child_update); - me->status = Status::SCRATCHED; - } - else - { - LOG(OrderMan, Diag, - "ScratchOrder order {} still waiting to scratch. Order: {}@{} " - "My side: {}@{}" - "Thresh : {}@{}", - algo().order_key().value(), algo().quantity().value(), algo().price().value(), - my_side_psz.size(), my_side_psz.price(), - me->scratch_threshold.value(), my_quan); - } -} - -Price TWAPAlgo::get_price_to_pass (Price price) const +void TWAPAlgo::update_params() { - Price rv; - switch (me->status) { - case Status::WAITING_FOR_MD: - case Status::WAITING_TO_SCRATCH: - rv = price; - break; - case Status::SCRATCHED: - rv = me->scratch_price; - break; - } - return rv; -} - -void TWAPAlgo::update_scratch_threshold() -{ - if (algo().definition().custom_def().scratch().scratch_threshold().quantity_case() == - bts::spark::QuantityMeasure::QuantityCase::QUANTITY_NOT_SET) + if (!algo().definition().custom_def().twap().has_duration()) { - algo().terminate_error("Invalid quantity, terminating"); + algo().terminate_error("Invalid duration params, terminating"); return; } - me->scratch_threshold = SpreaderUtils::_quantity_threshold( - algo().definition().custom_def().scratch().scratch_threshold(), algo().quantity()); - me->chase_diff = Price(securities::tick_size( - algo().security()).value() * algo().definition().custom_def().scratch().chase_ticks()); + if (!algo().definition().custom_def().twap().has_aggression()) + { + algo().terminate_error("Invalid aggression params, terminating"); + return; + } + me->duration.CopyFrom(algo().definition().custom_def().twap().duration()); + me->aggression.CopyFrom(algo().definition().custom_def().twap().aggression()); } -void TWAPAlgo::on_child_change(OrderKey, ChildState const &st) +void TWAPAlgo::on_child_change(OrderKey key, const ChildState& st) { - algo().update_fill_state(st.total_fills, st.last_price); - switch (st.status) + me->child_state[key] = st; + + uint32_t fills = 0u; bool all_complete = true, all_cancelled = true; + for(const auto& ch : me->child_state) { - case ChildStatus::WORKING: - return; - case ChildStatus::COMPLETED: - algo().complete(); - return; - case ChildStatus::CANCELED: - algo().cancel("Unexpected child cancel"); - return; + fills += ch.second.total_fills.value(); + all_complete = all_complete && (ch.second.status == ChildStatus::COMPLETED || + ch.second.status == ChildStatus::CANCELED); + all_cancelled = all_cancelled && (ch.second.status == ChildStatus::CANCELED); } + algo_services().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"); } } // namespace orderman 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 4b6112fe0..bd5cbb9b1 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/TWAPAlgo.h @@ -20,13 +20,12 @@ public: private: enum class Status; + enum class Channel; struct PImpl; std::unique_ptr me; IAlgoServices& algo() const; - void update_scratch_threshold(); - void try_to_scratch(); - Price get_price_to_pass (Price price) const; + void update_params(); }; }}