From f3294701653fbf24ca0f1a80efc8ed1daba63063 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 17 Jun 2019 16:49:21 -0400 Subject: [PATCH] added spreadsheet to help test span --- .../spark_excel/margin_data_example.xlsm | Bin 0 -> 16839 bytes .../rtd/formatting/JsonFormatter.cs | 4 +- src/lib/api/apicore.proto | 5 +- src/lib/api/services/MarginService.proto | 29 +++- .../sparkd/position/MarginServiceServer.cpp | 150 +++++++++++++++--- .../sparkd/position/MarginServiceServer.h | 24 ++- .../sparkd/position/PositionAggregator.cpp | 17 +- .../sparkd/position/PositionAggregator.h | 3 +- 8 files changed, 186 insertions(+), 46 deletions(-) create mode 100644 src/client/spark_excel/margin_data_example.xlsm diff --git a/src/client/spark_excel/margin_data_example.xlsm b/src/client/spark_excel/margin_data_example.xlsm new file mode 100644 index 0000000000000000000000000000000000000000..0decb97da7ba9119dc4c2799b50c3a1ac7aca6a9 GIT binary patch literal 16839 zcmeHuW1A)0vUS;Z7rSiRwr$&1muauO4%eHN+i*N0H?z#J%-sir5;NJX@nd@0= zjP>N4BW6U#jF6KA20;M;1AqVk0Kf-;(fP4Z1_%If4h8^#3;+S7DQIisWNhQ4tK@EH z?5IuSW^MH~4+MxT2LR~v_y2eNFCKw0<#Cx^I)qNbYq+>7G8aJeAUQ=iYHaiZV>~?z zSuDk&My?;Loo_NT{s2kA;Sy~ruk5#PW5d}YtYw3!l2S$iRsAj4p+FsQgU{~7-n<)0 zs(Cpu8@|xElr7fczscSh^%h z(MLc-N>FY1?W^Ww^UM*^e5g80j$-pYo%8S;z^N|Eh5iYJxO3Ajt(>>n*pGdZqr+fQ zGIA1-SwCi(Mw!+_U}vpYQVyDrxahG}FlE=bcQKCCEI>IO&H}==b>9d2#n`FWkuczc zBcKg~$D^sOyXFIG$32qvorf!0K`bgb(qVOTHiz@+!JIJ?EE;kEM(k`G7s zQTarCj}L*s{t||((+`J}yqX&q2j)t6*aoyReXV5tHH-!xixzGb*(&fk1_Y+&C_|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stateful_publisher()), _dispatcher(utils::Dispatcher::get_named("MarginServiceServer")), _timer(_dispatcher, 60000, [=]() { publish_all();} ), _position(new PositionAggregator(context.locate(), mdclient)), _margin(new MarginServiceCore(_dispatcher)), + //state below + _lock(), _last_update(), - _lock() + _admin_data() { LOG(Gen, Info, "MarginServiceServer: service started"); + + _admin_data = *_admin_service.get_admin_data(api::VoidMessage()).value(); + _admin_service.subscribe_admin_data() | + rxcpp::operators::subscribe( + [this](auto const &update) + { + std::scoped_lock l(_lock); + bts_assert(update.all().size() == 1); + _admin_data = *update.all()[0]; + }); + publish_all(); _timer.start(); } @@ -42,25 +60,24 @@ void MarginServiceServer::publish_all() { if(config::get().enabled()) { - auto accts = _position->get_accounts(); + std::scoped_lock l(_lock); + auto accts = AdminServiceUtils::accounts(_admin_data, false); for(const auto& i : accts) { datetime::LLDateTime since = 0; - { - std::scoped_lock l(_lock); - auto it = _last_update.find(i); - if(it != _last_update.end()) - since = it->second; - } - std::vector pos; - orderman::AccountName acct = i; + std::string acct = i->name(); + auto it = _last_update.find(acct); + if(it != _last_update.end()) + since = it->second; + std::vector pos; if(_position->query_positions(acct, pos, &since)) { - LOG(Gen, Info, "MarginServiceServer: computing margin for {} ", orderman::to_str(i) ); - _margin->compute_margin(i, pos, [this, since, acct](const MarginResult& m) + LOG(Gen, Info, "MarginServiceServer: computing margin for {} ", acct); + auto aname = orderman::to(acct); + _margin->compute_margin(aname, pos, [this, since, acct](const MarginResult& m) { - publish_margin(m); - std::scoped_lock l(_lock); + std::scoped_lock l(_lock); + publish_margin(m); _last_update[acct] = since; }); } @@ -84,8 +101,7 @@ void MarginServiceServer::get_margin(const api::position::MarginRequest& request orderman::AccountName acct; orderman::str_to(request.account(), &acct); std::vector pos; - _position->query_positions(acct, pos); - + _position->query_positions(request.account(), pos); _margin->compute_margin(acct, pos, [=](const MarginResult& m) { reply(m); @@ -94,12 +110,104 @@ void MarginServiceServer::get_margin(const api::position::MarginRequest& request } -void MarginServiceServer::publish_margin(const api::position::MarginResult& account_result) +void MarginServiceServer::publish_margin(const api::position::MarginResult& result) { - //account_result here is keyed on account_id, which is included for streaming margin computations but not - //on-demand computations - _publisher.add_or_update(account_result); + std::scoped_lock l(_lock); + _publisher.add_or_update(result); _publisher.flush(); + + publish_excess_liquidity_report(result); } + +void MarginServiceServer::publish_excess_liquidity_report(const api::position::MarginResult& result) +{ + std::scoped_lock l(_lock); + ExcessLiquidityReport report; + report.set_account(result.account()); + report.mutable_margin_result()->CopyFrom(result); + auto acct = AdminServiceUtils::find_account(_admin_data, result.account(), false); + double netliq = 0.0; + if(acct && acct->has_margin_limit()) + { + report.set_leverage_factor( acct->margin_limit().leverage_factor_percentage() ); + report.set_cash_balance(acct->margin_limit().cash_balance()); + netliq = report.leverage_factor() * report.cash_balance(); + } + netliq -= result.maint_margin(); + report.set_excess_liquidity(netliq); + _report_publisher.add_or_update(report); + _report_publisher.flush(); +} + +void MarginServiceServer::update_report(const std::string& account) +{ + MarginResult res; + auto acct = orderman::to(account); + if(_margin->get_last_valid_result(acct, res)) + publish_excess_liquidity_report(res); +} + +void MarginServiceServer::set_cash_factor(const ::bts::api::position::CashFactorUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) +{ + std::scoped_lock l(_lock); + auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false); + if(!acct) + { + auto msg = "Account " + request.account() +"not found"; + api::ResultMessage resp; + resp.set_success(false); + resp.set_error_message(msg); + reply(resp); + } + else + { + acct->mutable_margin_limit()->set_leverage_factor_percentage(request.leverage_factor()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, request, reply](const api::Result& admin_result) + { + api::ResultMessage resp; + resp.set_success(admin_result.success()); + resp.set_error_message(admin_result.error_message()); + reply(resp); + update_report(request.account()); + }); + } +} + +void MarginServiceServer::set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) +{ + std::scoped_lock l(_lock); + auto acct = AdminServiceUtils::find_account(_admin_data, request.account(), false); + if(!acct) + { + auto msg = "Account " + request.account() +"not found"; + api::ResultMessage resp; + resp.set_success(false); + resp.set_error_message(msg); + reply(resp); + } + else + { + acct->mutable_margin_limit()->set_cash_balance(request.cash_balance()); + AccountUpdateAPIArgument update; + update.mutable_complete_data()->CopyFrom(*acct); + + _admin_service.update_account(update, + [this, request, reply](const api::Result& admin_result) + { + api::ResultMessage resp; + resp.set_success(admin_result.success()); + resp.set_error_message(admin_result.error_message()); + reply(resp); + update_report(request.account()); + }); + } +} + }} diff --git a/src/server/sparkd/position/MarginServiceServer.h b/src/server/sparkd/position/MarginServiceServer.h index c63a121ec..3fb3e06ad 100644 --- a/src/server/sparkd/position/MarginServiceServer.h +++ b/src/server/sparkd/position/MarginServiceServer.h @@ -1,11 +1,12 @@ #pragma once #include "lib/orderman/orderman_utils/orderman_common.h" -#include "lib/api/services/MarginService.server.pb.h" #include "lib/md/SecurityOracle.h" +#include "lib/persistence/IStore.h" #include "lib/api/public/ServiceContext.h" -#include "lib/api/services/PositionService.pb.h" -#include "lib/api/services/PositionService.client.pb.h" +#include "lib/api/services/MarginService.server.pb.h" +#include "lib/api/services/AdminService.pb.h" +#include "lib/api/services/AdminService.client.pb.h" #include "lib/utils/bts_no_copy.h" #include "lib/utils/datetime.h" #include @@ -28,18 +29,33 @@ public: void get_margin(const api::position::MarginRequest& request, const api::Response& reply) override; + + void set_cash_factor(const ::bts::api::position::CashFactorUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) override; + + void set_cash_balance(const ::bts::api::position::CashBalanceUpdate& request, + const api::Response<::bts::api::ResultMessage>& reply) override; private: void publish_margin(const api::position::MarginResult&); + void publish_excess_liquidity_report(const api::position::MarginResult&); + void update_report(const std::string&); void publish_all(); private: + api::admin::AdminService _admin_service; MarginServiceBase::Margin_dataStatefulPublisher _publisher; + MarginServiceBase::Excess_liquidity_reportStatefulPublisher _report_publisher; utils::Dispatcher& _dispatcher; utils::DispatcherTimer _timer; std::unique_ptr _position; std::unique_ptr _margin; - std::map _last_update; + +private: + //state std::recursive_mutex _lock; + std::map _last_update; + bts::api::admin::AdminData _admin_data; + }; }} diff --git a/src/server/sparkd/position/PositionAggregator.cpp b/src/server/sparkd/position/PositionAggregator.cpp index 066e4e753..74f0131e5 100644 --- a/src/server/sparkd/position/PositionAggregator.cpp +++ b/src/server/sparkd/position/PositionAggregator.cpp @@ -53,13 +53,6 @@ PositionAggregator::PositionAggregator(api::position::PositionService&& pos, md: PositionAggregator::~PositionAggregator() {} -std::vector PositionAggregator::get_accounts() -{ - std::vector vec; - for(const auto& i : _accounts) - vec.push_back(orderman::to(i.first)); - return vec; -} void eraseSubStr(std::string & mainStr, const std::string & toErase) { @@ -68,25 +61,23 @@ void eraseSubStr(std::string & mainStr, const std::string & toErase) mainStr.erase(pos, toErase.length()); } -bool PositionAggregator::query_positions(const orderman::AccountName& account, std::vector& result, +bool PositionAggregator::query_positions(const std::string& account, std::vector& result, datetime::LLDateTime* since) { - std::string acc = orderman::to_trim_str(account); - auto last_update = _accounts[acc]; + auto last_update = _accounts[account]; if(!since || *since == 0 || bts::datetime::ll_diff_in_us(*since, last_update) > 0) { std::scoped_lock l(_lock); - auto the_account = orderman::to_trim_str(account); for(auto& entry : _position_map) { const PositionUnit& unit = entry.second; - if(unit.account() != the_account) + if(unit.account() != account) continue; auto sec = _oracle.get_security( SecurityKey(unit.security_id()) ); if(sec && sec->has_glbx() && sec->has_future()) { Position pos; - pos.account = account; + pos.account = orderman::to(account); pos.security_desc = securities::descriptor(*sec); eraseSubStr(pos.security_desc, "SIM:"); int32_t totalpos = unit.sod_long_pos() + unit.day_long_pos() - unit.sod_short_pos() - unit.day_short_pos(); diff --git a/src/server/sparkd/position/PositionAggregator.h b/src/server/sparkd/position/PositionAggregator.h index 7571d2cc0..db9ec5ad5 100644 --- a/src/server/sparkd/position/PositionAggregator.h +++ b/src/server/sparkd/position/PositionAggregator.h @@ -21,8 +21,7 @@ public: PositionAggregator(api::position::PositionService&&, md::MDClient&); ~PositionAggregator(); - std::vector get_accounts(); - bool query_positions(const orderman::AccountName& account, std::vector& result, + bool query_positions(const std::string& account, std::vector& result, datetime::LLDateTime* since = nullptr); private: