From 9de3d808913384ec7b1d5cc0a3a6fbda3f1befdb Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 13 Feb 2020 18:37:31 -0500 Subject: [PATCH] getting TradingDistribution data into algo --- .../orderman/orders/algo/AlgoWrapperOrder.cpp | 6 +++++ .../orderman/orders/algo/AlgoWrapperOrder.h | 1 + src/lib/orderman/orders/algo/IAlgoServices.h | 2 ++ .../algo_orders/bluex/ArrivalPriceAlgo.cpp | 13 +++-------- .../algo/algo_orders/bluex/PriceEstimator.cpp | 22 ++++++++++++------- .../algo/algo_orders/bluex/PriceEstimator.h | 9 ++++---- .../orders/algo/algo_orders/bluex/Slicer.cpp | 1 + .../orders/algo/algo_orders/bluex/Slicer.h | 1 + .../algo/algo_orders/bluex/VWAPAlgo.cpp | 6 +---- .../algo_orders/bluex/VolumeEstimator.cpp | 22 ++++++++++++------- .../algo/algo_orders/bluex/VolumeEstimator.h | 9 ++++---- .../algo/algo_orders/tests/AlgoTestDriver.cpp | 7 ++++++ .../algo/algo_orders/tests/AlgoTestDriver.h | 1 + 13 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index fe390ff06..e71c74a48 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -4,6 +4,7 @@ #include "lib/orderman/actors/order_manager/ChildOrder.h" #include "lib/orderman/actors/order_manager/OrderBuilder.h" #include "lib/orderman/actors/order_manager/OrderServices.h" +#include "lib/api/public/ServiceContext.h" #include "lib/orderman/orderman_utils/orderman_utils.h" #include "lib/orderman/orders/PreActivationOrderServices.h" #include "lib/orderman/orders/algo/IAlgoOrder.h" @@ -354,6 +355,11 @@ orderman::OrderOpResult AlgoWrapperOrder::on_cancel() { return {}; } +const bts::api::ServiceContext& AlgoWrapperOrder::get_shared_service_context() const { + static bts::api::ServiceContext context; + return context; +} + // -------------------------------------------------------------------------------- // IBeforeLaunchAlgoServices // -------------------------------------------------------------------------------- diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h index 9cdc1d263..e9ff177f3 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h @@ -65,6 +65,7 @@ class AlgoWrapperOrder : public Order, void complete() override; void update_fill_state(Quantity total_fills, Price last_price) override; void set_ladder_visibility (bool is_visibile) override; + const bts::api::ServiceContext& get_shared_service_context() const override; // Private struct ReportStateGuard; diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index 77cf2450a..ab16494cb 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -3,6 +3,7 @@ #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" namespace bts { +namespace api { class ServiceContext; } namespace orderman { class IAlgoServices : public IBeforeLaunchAlgoServices { @@ -104,6 +105,7 @@ class IAlgoServices : public IBeforeLaunchAlgoServices { // Update this algos's filled quantity and last price virtual void update_fill_state(Quantity total_fills, Price last_price) = 0; + virtual const api::ServiceContext& get_shared_service_context() const = 0; }; } // namespace orderman diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp index 927d9c074..2f91846f2 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp @@ -97,21 +97,14 @@ void ArrivalPriceAlgo::update_params() algo().terminate_error("Invalid volume dist params, terminating"); return; } - if(!volume_estimator().update_params(algo().definition().custom_def().arrival().volume_distribution())) - { - algo().terminate_error("Algo needs volume mean and variance overrides temporarily, terminating"); - return; - } + volume_estimator().update_params(algo().definition().custom_def().arrival().volume_distribution()); if (!algo().definition().custom_def().arrival().has_price_distribution()) { algo().terminate_error("Invalid price dist params, terminating"); return; } - if(!price_estimator().update_params(algo().definition().custom_def().arrival().price_distribution())) - { - algo().terminate_error("Algo needs price mean and variance overrides temporarily, terminating"); - return; - } + price_estimator().update_params(algo().definition().custom_def().arrival().price_distribution()); + me->lambda = algo().definition().custom_def().arrival().risk_threshold(); me->lambda = 1e-6 * pow(1.0, me->lambda); me->atmvol = price_estimator().price_vol_estimate(); diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.cpp index aa54df8ef..f63f64885 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.cpp @@ -1,16 +1,19 @@ #include "lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.h" #include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/HistoricalDataService.pb.h" #include namespace bts { namespace orderman { using namespace api::orderman; +using namespace history; struct PriceEstimator::PImpl { Quantity running_volume = Quantity(0); PriceDistribution price_distribution; + TradingDistribution trading_distribution; double realized_mean = 0.0; double realized_vol = 0.0; }; @@ -49,16 +52,19 @@ void PriceEstimator::on_ticker(SecurityKey, const TradeVector& vec) me->realized_mean /= r; } -bool PriceEstimator::update_params(const api::orderman::PriceDistribution& pd) +bool PriceEstimator::ready() const +{ + return me->price_distribution.price_time_horizon_days() > 0 && me->trading_distribution.volume_total() > 0; +} + +void PriceEstimator::update_params(const api::orderman::PriceDistribution& pd) { me->price_distribution = pd; - - if (me->price_distribution.price_mean_override() <= 0.0 || - me->price_distribution.price_vol_override() <= 0.0) - { - return false; - } - return true; +} + +void PriceEstimator::update_distribution(const history::TradingDistribution& td) +{ + me->trading_distribution = td; } diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.h b/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.h index a3f049c53..a9b7427d5 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/PriceEstimator.h @@ -5,10 +5,9 @@ #include namespace bts { +namespace history { class TradingDistribution; } namespace api { -namespace orderman { - class PriceDistribution; -} +namespace orderman { class PriceDistribution; } } namespace orderman { @@ -18,7 +17,9 @@ public: PriceEstimator(); ~PriceEstimator(); - bool update_params(const api::orderman::PriceDistribution&); + bool ready() const; + void update_params(const api::orderman::PriceDistribution&); + void update_distribution(const history::TradingDistribution& td); void on_ticker(SecurityKey, const TradeVector& vec); Price price_mean_estimate() const; diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp index e70494c1a..b93315ead 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp @@ -66,6 +66,7 @@ struct Slicer::PImpl VolumeEstimator volume_estimator; PriceEstimator price_estimator; + std::unique_ptr trading_distribution; }; OrderAggression& Slicer::aggression() { diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h index 08c10a906..e18d5da0e 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h @@ -1,6 +1,7 @@ #pragma once #include "lib/orderman/orders/algo/IAlgoOrder.h" +#include "lib/api/services/HistoricalDataService.pb.h" #include namespace bts { diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/VWAPAlgo.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/VWAPAlgo.cpp index 3e8f9b879..fb302d86f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/VWAPAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/VWAPAlgo.cpp @@ -87,11 +87,7 @@ void VWAPAlgo::update_params() algo().terminate_error("Invalid volume dist params, terminating"); return; } - if(!volume_estimator().update_params(algo().definition().custom_def().vwap().volume_distribution())) - { - algo().terminate_error("Algo needs volume mean and variance overrides temporarily, terminating"); - return; - } + volume_estimator().update_params(algo().definition().custom_def().vwap().volume_distribution()); } void VWAPAlgo::compute_first_generation(Price&, Quantity& size) diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.cpp b/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.cpp index 78e6de58f..deca627a4 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.cpp @@ -1,15 +1,18 @@ #include "lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.h" #include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/HistoricalDataService.pb.h" #include namespace bts { namespace orderman { using namespace api::orderman; +using namespace history; struct VolumeEstimator::PImpl { VolumeDistribution volume_distribution; + TradingDistribution trading_distribution; Quantity running_volume = Quantity(0); }; @@ -28,16 +31,19 @@ void VolumeEstimator::on_ticker(SecurityKey, const TradeVector& vec) me->running_volume = Quantity( std::max(me->running_volume.value(), t.total_volume.value()) ); } -bool VolumeEstimator::update_params(const api::orderman::VolumeDistribution& vd) +bool VolumeEstimator::ready() const +{ + return me->volume_distribution.volume_time_horizon_days() > 0 && me->trading_distribution.volume_total() > 0; +} + +void VolumeEstimator::update_params(const api::orderman::VolumeDistribution& vd) { me->volume_distribution = vd; - - if (me->volume_distribution.volume_mean_override() <= 0.0 || - me->volume_distribution.volume_variance_override() <= 0.0) - { - return false; - } - return true; +} + +void VolumeEstimator::update_distribution(const history::TradingDistribution& td) +{ + me->trading_distribution = td; } Quantity VolumeEstimator::day_volume_estimate() const diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.h b/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.h index 1d3975f57..ca7cdcca5 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/VolumeEstimator.h @@ -5,10 +5,9 @@ #include namespace bts { +namespace history { class TradingDistribution; } namespace api { -namespace orderman { - class VolumeDistribution; -} +namespace orderman { class VolumeDistribution; } } namespace orderman { @@ -18,7 +17,9 @@ public: VolumeEstimator(); ~VolumeEstimator(); - bool update_params(const api::orderman::VolumeDistribution&); + bool ready() const; + void update_params(const api::orderman::VolumeDistribution&); + void update_distribution(const history::TradingDistribution&); void on_ticker(SecurityKey, const TradeVector& vec); Quantity day_volume_estimate() const; diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp index f1645059d..149d32316 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -2,6 +2,7 @@ #include "lib/md/relay/RelayRecorder.h" #include "catch/catch.hpp" #include "lib/api/services/OrderService.pb.h" +#include "lib/api/public/ServiceContext.h" #include "lib/orderman/orderman_utils/Book.h" #include "lib/orderman/orders/algo/ChildTemplate.h" #include "lib/logging/logging.h" @@ -504,6 +505,12 @@ void AlgoTestDriverBase::complete() { me->state = AlgoState::COMPLETED; } void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } +const api::ServiceContext& AlgoTestDriverBase::get_shared_service_context() const +{ + bts_assert(false); + static api::ServiceContext context; + return context; +} void AlgoTestDriverBase::update_fill_state(Quantity q, Price p) { me->fill_state = { q, p }; } }}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h index 0eb490101..1d8c1420f 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -146,6 +146,7 @@ private: void complete() override; void set_ladder_visibility(bool) override; void update_fill_state(Quantity , Price) override; + const api::ServiceContext& get_shared_service_context() const override; };