diff --git a/docker/linux b/docker/linux index 653781226..56c61231f 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit 65378122620c7a26561641cb49b477c822b315f2 +Subproject commit 56c61231f38261f275e0b26baae70622c760dc33 diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index c374026de..6961b7c9d 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -28,6 +28,25 @@ service OrderService { rpc fills (FillsSub) returns (stream Fill); rpc exchange_positions (ExchangePositionSub) returns (stream RetrievedExchangePosition); rpc exchange_open_orders (ExchangePositionSub) returns (stream RetrievedExchangeOpenOrder); + + // temporary + // precomputed theos + rpc set_precomputed_theos(PrecomputedTheos) returns (bts.api.ResultMessage); +} + +message PrecomputedTheoPoint { + uint32 sec_id = 1; + double driver = 10; + double theo = 20; + double delta = 30; + double gamma = 40; + double vega = 50; + double theta = 60; + double rho = 70; +} + +message PrecomputedTheos { + repeated PrecomputedTheoPoint theos = 10; } message ManualBustParameters{ diff --git a/src/lib/orderman/OrderServiceServer.cpp b/src/lib/orderman/OrderServiceServer.cpp index f7b8fef07..dcca7db21 100644 --- a/src/lib/orderman/OrderServiceServer.cpp +++ b/src/lib/orderman/OrderServiceServer.cpp @@ -469,6 +469,16 @@ void OrderServiceServer::send_rfq( r(response); } +void OrderServiceServer::set_precomputed_theos( + const bts::api::orderman::PrecomputedTheos &precomp_theos, + const api::Response<::bts::api::ResultMessage> &reply) { + LOG(Gen, Info, "OrderServiceServer::set_precomputed_theos count: {}", + precomp_theos.theos().size()); + me->service_bridge->set_precomputed_theos(precomp_theos); + bts::api::ResultMessage res = {}; + reply(res); +} + void OrderServiceServer::on_order_op_result( OrderOpResultEvent const &op_result) { auto it = me->in_flight_order_ops.find(op_result.req_id); diff --git a/src/lib/orderman/OrderServiceServer.h b/src/lib/orderman/OrderServiceServer.h index 285151827..f2110fd7b 100644 --- a/src/lib/orderman/OrderServiceServer.h +++ b/src/lib/orderman/OrderServiceServer.h @@ -55,6 +55,10 @@ class OrderServiceServer : public api::orderman::OrderServiceBase { void send_rfq( const api::orderman::RFQRequest &, const api::Response &) override; + // temporary + void set_precomputed_theos( + const bts::api::orderman::PrecomputedTheos &precomp_theos, + const api::Response<::bts::api::ResultMessage> &reply) override; private: struct PImpl; diff --git a/src/lib/orderman/actors/ServiceBridge.cpp b/src/lib/orderman/actors/ServiceBridge.cpp index 1c0b98aa4..3637df54f 100644 --- a/src/lib/orderman/actors/ServiceBridge.cpp +++ b/src/lib/orderman/actors/ServiceBridge.cpp @@ -7,6 +7,8 @@ #include "lib/mercury/broker/BrokerProxy.h" #include "lib/mercury/events/Stream.h" #include "lib/orderman/OrderRouter.h" +#include "lib/orderman/calculations/ITheoCalculator.h" +#include "lib/orderman/calculations/TheoCalculator.h" #include "lib/orderman/events/AccountPnLUpdateEvent.h" #include "lib/orderman/events/ActivateOrderEvent.h" #include "lib/orderman/events/CancelOrderEvent.h" @@ -22,6 +24,7 @@ #include "lib/orderman/events/RFQEvent.h" #include "lib/orderman/events/StoredFillEvent.h" #include "lib/orderman/events/StoredOrderEvent.h" +#include "lib/orderman/events/TheoEvent.h" #include "lib/orderman/events/UpdateOrderEvent.h" #include "lib/orderman/order_factory/OrderFactory.h" #include "lib/orderman/orderman_utils/orderman_common.h" @@ -74,10 +77,12 @@ struct ServiceBridge::PImpl { mercury::Stream account_pnl_pub; mercury::Stream filled_position_pub; mercury::Stream excess_liquidity_pub; + mercury::Stream theo_pub; std::unordered_set known_orders; std::vector> admin_data_updates; mercury::BrokerProxy &service_call_marshaller; std::promise ready; + bts::orderman::analytics::TheoCalculator theo_calculator = {}; }; ServiceBridge::ServiceBridge( @@ -158,6 +163,7 @@ void ServiceBridge::init() { me->detach_order_pub = make_stream(); me->manually_bust_fills_pub = make_stream(); me->rfq_pub = make_stream(); + me->theo_pub = make_stream(); me->new_admin_data_pub = make_stream(); me->account_pnl_pub = make_stream(); me->filled_position_pub = make_stream(); @@ -347,6 +353,19 @@ void ServiceBridge::update_excess_liquidity_report( }); } +// updates the calculator and publishes the event (with a pointer to it) +void ServiceBridge::set_precomputed_theos( + const bts::api::orderman::PrecomputedTheos &precomputed_theos) { + LOG(Gen, Info, "ServiceBridge::update_theos size: {}", + precomputed_theos.theos_size()); + me->theo_calculator.set_data(precomputed_theos); + me->service_call_marshaller.schedule_action([this]() { + bts::orderman::analytics::ITheoCalculator const *theo_calc_ptr = + &(me->theo_calculator); + publish(me->theo_pub, theo_calc_ptr); + }); +} + std::optional make_pos_event( api::position::PositionUnit const &pu, securities::MapSecurityDB const &o) { auto sec = o.resolve(SecurityKey(pu.security_id())); diff --git a/src/lib/orderman/actors/ServiceBridge.h b/src/lib/orderman/actors/ServiceBridge.h index 9b10ab2e6..7546066f8 100644 --- a/src/lib/orderman/actors/ServiceBridge.h +++ b/src/lib/orderman/actors/ServiceBridge.h @@ -101,6 +101,8 @@ class ServiceBridge : public mercury::Actor { void update_position(api::position::Position_dataUpdate const &); void update_excess_liquidity_report( api::position::Excess_liquidity_reportUpdate const &); + void set_precomputed_theos( + const bts::api::orderman::PrecomputedTheos &precomputed_theos); private: struct PImpl; diff --git a/src/lib/orderman/calculations/ITheoCalculator.h b/src/lib/orderman/calculations/ITheoCalculator.h new file mode 100644 index 000000000..114e82c5f --- /dev/null +++ b/src/lib/orderman/calculations/ITheoCalculator.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace bts::api::orderman { +class PrecomputedTheoPoint; +} // namespace bts::api::orderman + +namespace bts::orderman::analytics { +// This is the calculator interface used on the server to get any theo +// (in its first incarnation, it only works for options). A pointer to the +// ITheoCalculator is passed to the algo, which can then used it to compute +// theos. +class ITheoCalculator { + public: + // computes the theo for a security with id sec_id and returns the result in + // the last arg + virtual bool get_theo( + uint32_t sec_id, + double driver, + bts::api::orderman::PrecomputedTheoPoint &result) const = 0; +}; +} // namespace bts::orderman::analytics \ No newline at end of file diff --git a/src/lib/orderman/calculations/TheoCalculator.cpp b/src/lib/orderman/calculations/TheoCalculator.cpp new file mode 100644 index 000000000..495827081 --- /dev/null +++ b/src/lib/orderman/calculations/TheoCalculator.cpp @@ -0,0 +1,196 @@ +#include "TheoCalculator.h" +#include "lib/api/services/OrderService.pb.h" +#include "lib/logging/logging.h" +#include "lib/os/os.h" +#include "lib/utils/bts_assert.h" +#include +#include +#include +#include + +typedef std::shared_mutex Lock; +typedef std::unique_lock WriteLock; +typedef std::shared_lock ReadLock; + +using namespace bts::api::orderman; + +namespace bts::orderman::analytics { + +const double eps = 0.0000001; + +bool equal(double d1, double d2) { return std::abs(d1 - d2) <= eps; } + +// returns true if d2 >= d1 +bool greater_eq(double d1, double d2) { return equal(d1, d2) || d2 > d1; } + +double lin_interp_exterp(double x1, double y1, double x2, double y2, double x) { + if (equal(x1, x2)) + return bts::os::nan(); + double m = (y2 - y1) / (x2 - x1); // slope + double b = y2 - m * x2; // y-intercept + return m * x + b; +} + +void interpolate( + const PrecomputedTheoPoint &lower, + const PrecomputedTheoPoint &higher, + double driver, + PrecomputedTheoPoint &result) { + result.set_driver(driver); + double theo = lin_interp_exterp( + lower.driver(), lower.theo(), higher.driver(), higher.theo(), driver); + double delta = lin_interp_exterp( + lower.driver(), lower.delta(), higher.driver(), higher.delta(), driver); + double gamma = lin_interp_exterp( + lower.driver(), lower.gamma(), higher.driver(), higher.gamma(), driver); + double vega = lin_interp_exterp( + lower.driver(), lower.vega(), higher.driver(), higher.vega(), driver); + double theta = lin_interp_exterp( + lower.driver(), lower.theta(), higher.driver(), higher.theta(), driver); + double rho = lin_interp_exterp( + lower.driver(), lower.rho(), higher.driver(), higher.rho(), driver); + result.set_theo(theo); + result.set_delta(delta); + result.set_gamma(gamma); + result.set_vega(vega); + result.set_theta(theta); + result.set_rho(rho); +} + +// Contains all the theo points for one security in a sorted vector of tps +struct SecTheoData { + private: + std::vector tps = {}; + + public: + void clear() { tps.clear(); } + void sort() { + std::sort(tps.begin(), tps.end(), [](const auto &u1, const auto &u2) { + return greater_eq(u1.driver(), u2.driver()); + }); + } + void add(const PrecomputedTheoPoint &theos_unit) { + tps.push_back(theos_unit); + } + + void find_neighbors( + double driver, + PrecomputedTheoPoint const *lower, + PrecomputedTheoPoint const *higher) const { + lower = higher = nullptr; + if (tps.size() < 2) + return; + for (size_t i = 0; i < tps.size(); i++) { + PrecomputedTheoPoint const *curr = &tps[i]; + PrecomputedTheoPoint const *next = + i + 1 < tps.size() ? &tps[i + 1] : nullptr; + if (curr != nullptr && next != nullptr) { + if (greater_eq(curr->driver(), driver) && + greater_eq(driver, next->driver())) { + lower = curr; + higher = next; + return; + } + } + } + // this is when driver is smaller than the min driver + if (greater_eq(driver, tps[0].driver())) { + lower = &tps[0]; + higher = &tps[1]; + return; + } + // when the driver is greater than the max driver + if (greater_eq(tps[tps.size() - 1].driver(), driver)) { + lower = &tps[tps.size() - 2]; + higher = &tps[tps.size() - 1]; + return; + } + } +}; + +// This is where all the precomputed data resides +struct TotalTheoData { + private: + std::map sec_map = {}; + + private: + void clear_sec(uint32_t sec_id) { + auto it = sec_map.find(sec_id); + if (it != sec_map.end()) { + it->second.clear(); + sec_map.erase(sec_id); + } + } + void add_tp(const PrecomputedTheoPoint &u) { + auto it = sec_map.find(u.sec_id()); + if (it == sec_map.end()) { + sec_map.insert({u.sec_id(), {}}); + it = sec_map.find(u.sec_id()); + bts_assert(it != sec_map.end()); + } + SecTheoData &sec_data = it->second; + sec_data.add(u); + } + + public: + // this assumes that theos contains all the data for a given sec + void set_data(const PrecomputedTheos &theos) { + // clear data for each sec + for (const auto &tp : theos.theos()) { + clear_sec(tp.sec_id()); + } + for (const auto &tp : theos.theos()) { + add_tp(tp); + } + for (auto &kv : sec_map) { + kv.second.sort(); + } + } + bool get_theo( + uint32_t sec_id, double driver, PrecomputedTheoPoint &result) const { + auto it = sec_map.find(sec_id); + if (it == sec_map.end()) { + return false; + } + const SecTheoData &sec_data = it->second; + PrecomputedTheoPoint const *lower = nullptr; + PrecomputedTheoPoint const *higher = nullptr; + sec_data.find_neighbors(driver, lower, higher); + if (lower == nullptr || higher == nullptr) { + return false; + } else { + interpolate(*lower, *higher, driver, result); + return true; + } + return true; + } +}; + +struct TheoCalculator::PImpl { + PImpl() {} + TotalTheoData total_theo_data = {}; + Lock lock; +}; + +TheoCalculator::TheoCalculator() : me(new PImpl()) { + LOG(Gen, Info, "TheoCalculator::TheoCalculator called"); +} + +TheoCalculator::~TheoCalculator() {} + +bool TheoCalculator::get_theo( + uint32_t sec_id, double driver, PrecomputedTheoPoint &result) const { + ReadLock read_lock(me->lock); + LOG(Gen, Diag, "TheoCalculator::get_theo sec_id: {} driver: {}", sec_id, + driver); + result.clear_driver(); + return me->total_theo_data.get_theo(sec_id, driver, result); +} + +void TheoCalculator::set_data(const PrecomputedTheos &theos) { + LOG(Gen, Info, "TheoCalculator::set_data size: {}", theos.theos_size()); + WriteLock write_lock(me->lock); + me->total_theo_data.set_data(theos); +} + +} // namespace bts::orderman::analytics \ No newline at end of file diff --git a/src/lib/orderman/calculations/TheoCalculator.h b/src/lib/orderman/calculations/TheoCalculator.h new file mode 100644 index 000000000..ff40a6c81 --- /dev/null +++ b/src/lib/orderman/calculations/TheoCalculator.h @@ -0,0 +1,30 @@ +#pragma once + +#include "ITheoCalculator.h" +#include +#include + +namespace bts::api::orderman { +class PrecomputedTheoPoint; +class PrecomputedTheos; +} // namespace bts::api::orderman +namespace bts::orderman::analytics { +// TheoCalculator implements the interface ITheoCalculator and provides a thread +// safe way to compute theos. It gets the precomputed theos via the set_data +// function and 'computes' the theos via interpolation +class TheoCalculator : public ITheoCalculator { + public: + TheoCalculator(); + ~TheoCalculator(); + // interface member + bool get_theo( + uint32_t sec_id, + double driver, + bts::api::orderman::PrecomputedTheoPoint &result) const override; + void set_data(const bts::api::orderman::PrecomputedTheos &theos); + + private: + struct PImpl; + std::unique_ptr me; +}; +} // namespace bts::orderman::analytics \ No newline at end of file diff --git a/src/lib/orderman/events/TheoEvent.h b/src/lib/orderman/events/TheoEvent.h new file mode 100644 index 000000000..cb10d35ef --- /dev/null +++ b/src/lib/orderman/events/TheoEvent.h @@ -0,0 +1,19 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/mercury/events/Event.h" +#include "lib/orderman/calculations/ITheoCalculator.h" + +namespace bts::orderman { + +// ITheoCalculator is published via this event. Subscribers can cache the +// pointer safely +class TheoEvent : public mercury::Event { + public: + bts::orderman::analytics::ITheoCalculator const *theo_calc; + using SynopsisPolicy = mercury::CacheAll; + TheoEvent(bts::orderman::analytics::ITheoCalculator const *theo_calc) + : theo_calc(theo_calc) {} +}; + +} // namespace bts::orderman