diff --git a/.vscode/launch.json b/.vscode/launch.json index 4e1141172..1c6e2055a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -44,7 +44,8 @@ "debuggerPath": "/usr/bin/gdb" }, "sourceFileMap": { - "/home/bts/spark/": "${workspaceRoot}" + "/home/bts/spark/": "${workspaceRoot}", + "/home/bts/spark_host/": "${workspaceRoot}", } }, { diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index e71c74a48..ca5a7d584 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -1,10 +1,10 @@ #include "lib/orderman/orders/algo/AlgoWrapperOrder.h" +#include "lib/api/public/ServiceContext.h" #include "lib/orderman/orders/algo/ChildTemplate.h" #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" @@ -355,7 +355,7 @@ orderman::OrderOpResult AlgoWrapperOrder::on_cancel() { return {}; } -const bts::api::ServiceContext& AlgoWrapperOrder::get_shared_service_context() const { +bts::api::ServiceContext& AlgoWrapperOrder::get_shared_service_context() { static bts::api::ServiceContext context; return context; } diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h index e9ff177f3..c73a67910 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.h +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.h @@ -65,7 +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; + bts::api::ServiceContext& get_shared_service_context() override; // Private struct ReportStateGuard; diff --git a/src/lib/orderman/orders/algo/IAlgoServices.h b/src/lib/orderman/orders/algo/IAlgoServices.h index ab16494cb..77cf2450a 100644 --- a/src/lib/orderman/orders/algo/IAlgoServices.h +++ b/src/lib/orderman/orders/algo/IAlgoServices.h @@ -3,7 +3,6 @@ #include "lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h" namespace bts { -namespace api { class ServiceContext; } namespace orderman { class IAlgoServices : public IBeforeLaunchAlgoServices { @@ -105,7 +104,6 @@ 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/IBeforeLaunchAlgoServices.h b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h index aec1f4ed2..e6e225aff 100644 --- a/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h +++ b/src/lib/orderman/orders/algo/IBeforeLaunchAlgoServices.h @@ -6,6 +6,7 @@ #include "lib/utils/bts_chrono.h" namespace bts { +namespace api { class ServiceContext; } namespace orderman { class ChildTemplate; @@ -47,6 +48,8 @@ class IBeforeLaunchAlgoServices { // Get the current time virtual TimeStamp get_time() const; + + virtual api::ServiceContext& get_shared_service_context() = 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 2585708e3..b25809732 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.cpp @@ -37,16 +37,6 @@ void ArrivalPriceAlgo::on_book(SecurityKey k, const Book& b) ArrivalPriceAlgo::ArrivalPriceAlgo() : me(new PImpl()) {} ArrivalPriceAlgo::~ArrivalPriceAlgo() {} -void ArrivalPriceAlgo::on_timer() -{ - if(this->trading_distribution) - { - TimeSlicer::on_timer(); - } - else - LOG(OrderMan, Info, "ArrivalPriceAlgo order {} waiting for TradingDistribution: ", algo().order_key().value()); -} - OrderOpResult ArrivalPriceAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv) { const auto& def = serv.definition(); @@ -71,9 +61,11 @@ OrderOpResult ArrivalPriceAlgo::do_before_launch(IBeforeLaunchAlgoServices &serv google::protobuf::Timestamp begin; begin.set_seconds(end.seconds() - 86400*days); - google::protobuf::util::TimeUtil::FromString("11/25/2019", &begin); - google::protobuf::util::TimeUtil::FromString("11/29/2019", &end); - query_trading_distribution(symbol, sectype, desc, begin, end, algo.duration().interval_in_seconds()); + //TODO + bool b = google::protobuf::util::TimeUtil::FromString("2019-11-25T00:00:00.00Z", &begin); + b = google::protobuf::util::TimeUtil::FromString("2019-11-29T00:00:00.00Z", &end); + b = b && b; + query_trading_distribution(serv, symbol, sectype, desc, begin, end, algo.duration().interval_in_seconds()); if (!serv.is_attaching()) { diff --git a/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.h b/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.h index 3b902136a..b28a05251 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/ArrivalPriceAlgo.h @@ -14,7 +14,6 @@ public: OrderOpResult do_before_launch(IBeforeLaunchAlgoServices&) override; void do_update(const api::orderman::Update& update) override; void on_book(SecurityKey, const Book&) override; - void on_timer() override; protected: void update_params() override; 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 f11097a38..b966f1c58 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.cpp @@ -52,13 +52,15 @@ struct Slicer::PImpl VolumeEstimator volume_estimator; PriceEstimator price_estimator; + + TradingDistributionStatus trading_distribution_status = TradingDistributionStatus::NotRequired; }; -void Slicer::query_trading_distribution(const std::string& symbol, api::SecurityType sectype, const std::string& secdesc, +void Slicer::query_trading_distribution(IBeforeLaunchAlgoServices& serv, const std::string& symbol, api::SecurityType sectype, const std::string& secdesc, const google::protobuf::Timestamp& begin, const google::protobuf::Timestamp& end, uint dt_seconds) { - const api::ServiceContext& sc = me->algo->get_shared_service_context(); + api::ServiceContext sc = serv.get_shared_service_context(); static HistoricalDataService hist = sc.locate(); TradingDistributionQuery query; query.set_symbol(symbol); @@ -67,13 +69,35 @@ void Slicer::query_trading_distribution(const std::string& symbol, api::Security query.mutable_begin()->CopyFrom(begin); query.mutable_end()->CopyFrom(end); query.mutable_d_t()->set_seconds(dt_seconds); + me->trading_distribution_status = TradingDistributionStatus::Waiting; hist.get_trading_distribution(query, [this, query] (api::Result res) { - auto copy = new TradingDistribution(*res.value()); - this->trading_distribution.reset(copy); + if(res.success()) + { + auto* copy = new TradingDistribution(*res.value()); + this->trading_distribution.reset(copy); + me->trading_distribution_status = TradingDistributionStatus::Received; + } + else + me->trading_distribution_status = TradingDistributionStatus::Error; }); } +void Slicer::on_timer() +{ + if(me->trading_distribution_status == TradingDistributionStatus::Error) + { + algo().terminate_error("Can't get Trading Distribution"); + } + else if(me->trading_distribution_status == TradingDistributionStatus::Received) + { + volume_estimator().update_distribution(*this->trading_distribution); + price_estimator().update_distribution(*this->trading_distribution); + update_params(); + me->trading_distribution_status = TradingDistributionStatus::Initialized; + } +} + OrderAggression& Slicer::aggression() { return me->aggression; @@ -311,7 +335,7 @@ OrderKey Slicer::send_child(Price price, Quantity size, Channel chan) OrderKey child_key = null_order_key(); if(validate_price_size(price, size)) { - if(aggression().quantity_variance() > 0) + if(aggression().quantity_variance() > 0 && chan != Channel::Market) { //this isn't really variance, but take a look at std::normal_distribution....it's kinda unusable and probably slow uint32_t var = (rand() % 2*aggression().quantity_variance()) - aggression().quantity_variance(); @@ -321,7 +345,7 @@ OrderKey Slicer::send_child(Price price, Quantity size, Channel chan) if(!validate_price_size(price, size)) return child_key; } - if(aggression().max_participation_pct() > 0) + if(aggression().max_participation_pct() > 0 && chan != Channel::Market) { auto side = algo().definition().side(); auto sz = me->last_book.side( side )[0].size(); @@ -417,8 +441,6 @@ void Slicer::on_ticker(SecurityKey k, const TradeVector& vec) LOG(OrderMan, Diag, "Slicer order {} on ticker.", algo().order_key().value()); } -void Slicer::on_timer() {} - void Slicer::first_generation() { bts_assert_msg(me->generation == 0, "generation 0"); 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 78e8e44ef..175a2ec1a 100644 --- a/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h +++ b/src/lib/orderman/orders/algo/algo_orders/bluex/Slicer.h @@ -33,6 +33,7 @@ public: protected: enum class Status { WAITING_FOR_MD, WAITING_FOR_START, WORKING, DONE }; enum class Channel { Tactical, Aggressive, Market }; + enum class TradingDistributionStatus { NotRequired, Waiting, Error, Received, Initialized}; IAlgoServices& algo() const; void report_status(); @@ -61,7 +62,7 @@ protected: struct PImpl; friend struct OrderDef; std::unique_ptr me; - std::unique_ptr trading_distribution; + std::unique_ptr trading_distribution; protected: void check_i_would(const Book&); @@ -82,8 +83,9 @@ protected: TradeVector& last_trades(); VolumeEstimator& volume_estimator(); PriceEstimator& price_estimator(); + bool trading_distribution_error(); - void query_trading_distribution(const std::string& symbol, api::SecurityType sectype, const std::string& secdesc, + void query_trading_distribution(IBeforeLaunchAlgoServices&, const std::string& symbol, api::SecurityType sectype, const std::string& secdesc, const google::protobuf::Timestamp& begin, const google::protobuf::Timestamp& end, uint dt_seconds); 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 149d32316..987078290 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.cpp @@ -505,7 +505,7 @@ void AlgoTestDriverBase::complete() { me->state = AlgoState::COMPLETED; } void AlgoTestDriverBase::set_ladder_visibility(bool) { /* whatever, ok */ } -const api::ServiceContext& AlgoTestDriverBase::get_shared_service_context() const +api::ServiceContext& AlgoTestDriverBase::get_shared_service_context() { bts_assert(false); static api::ServiceContext context; 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 1d8c1420f..7e32774b3 100644 --- a/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h +++ b/src/lib/orderman/orders/algo/algo_orders/tests/AlgoTestDriver.h @@ -146,7 +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; + api::ServiceContext& get_shared_service_context() override; };