diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4d59332ac..6ca205dc5 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -23,10 +23,21 @@ "taskName": "server_build", "type": "shell", "command": ".\\tools\\buildsparkd.cmd", - - "problemMatcher": { - "base": "$gcc", - "fileLocation": "relative" + "problemMatcher": { + "base": "$gcc", + "owner": "cpp", + "fileLocation": [ + "relative", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "(.?)(home)(.?)(bts)(.?)(spark)(.?)(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)", + "file": 8, + "line": 9, + "column": 10, + "severity": 11, + "message": 12 + } }, "presentation": { "echo": true, diff --git a/docker/linux b/docker/linux index d4a32d325..41523ccab 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit d4a32d32592c688c3a1f042750fd3323bcdfd38a +Subproject commit 41523ccab74b95bb007d9b9330d52c0f9e12dd1e diff --git a/src/lib/api/services/AccountService.proto b/src/lib/api/services/AccountService.proto index 217473e6c..3c76a4cd7 100644 --- a/src/lib/api/services/AccountService.proto +++ b/src/lib/api/services/AccountService.proto @@ -21,6 +21,10 @@ message Account { bool can_trade = 20; AccountBalance balance = 30; + bool enforce_margin = 40; + bool enforce_loss_limit = 50; + double loss_limit = 60; + repeated bts.api.user.OperatorID allowed_operators = 100; map future_limits = 110; map spread_limits = 120; diff --git a/src/server/sparkd/position/IMarginObserver.cpp b/src/server/sparkd/position/IMarginObserver.cpp new file mode 100644 index 000000000..6536da462 --- /dev/null +++ b/src/server/sparkd/position/IMarginObserver.cpp @@ -0,0 +1,9 @@ +#include "IMarginObserver.h" + +namespace bts +{ +namespace position +{ + IMarginObserver::~IMarginObserver() {} +} +} diff --git a/src/server/sparkd/position/IMarginObserver.h b/src/server/sparkd/position/IMarginObserver.h new file mode 100644 index 000000000..887a02269 --- /dev/null +++ b/src/server/sparkd/position/IMarginObserver.h @@ -0,0 +1,17 @@ +#pragma once + +#include "server/sparkd/orderman/utils/orderman_utils.h" + +namespace bts +{ +namespace position +{ +class MarginResult; +class IMarginObserver +{ +public: + virtual ~IMarginObserver(); + virtual void on_margin_change(const orderman::AccountName&, const MarginResult&) = 0; +}; +} +} diff --git a/src/server/sparkd/position/IPositionObserver.h b/src/server/sparkd/position/IPositionObserver.h index a25a3f133..0ef31666f 100644 --- a/src/server/sparkd/position/IPositionObserver.h +++ b/src/server/sparkd/position/IPositionObserver.h @@ -6,11 +6,12 @@ namespace bts { namespace position { +struct Position; class IPositionObserver { public: virtual ~IPositionObserver(); - virtual void on_position_change(orderman::AccountName, SecurityKey, double quantity, double avg_price) = 0; + virtual void on_position_change(const orderman::AccountName&, SecurityKey, double quantity, double avg_price) = 0; }; } } diff --git a/src/server/sparkd/position/MarginResult.h b/src/server/sparkd/position/MarginResult.h new file mode 100644 index 000000000..7f5708949 --- /dev/null +++ b/src/server/sparkd/position/MarginResult.h @@ -0,0 +1,25 @@ +#pragma once + +#include "server/sparkd/orderman/utils/orderman_utils.h" +#include "lib/domain_model/Security.pb.h" + +namespace bts +{ +namespace position +{ +struct MarginResult +{ + bool success; + std::string settle_indicator; + std::string settle_qualifier; + std::string portfolio_id; + std::string as_of_time; + std::string calculation_id; + + double opt_value; + double nonopt_value; + double maint_margin; + double initial_margin; +}; +} +} diff --git a/src/server/sparkd/position/MarginServiceCore.cpp b/src/server/sparkd/position/MarginServiceCore.cpp index 0d2b89e9c..ce19f35ae 100644 --- a/src/server/sparkd/position/MarginServiceCore.cpp +++ b/src/server/sparkd/position/MarginServiceCore.cpp @@ -1,15 +1,23 @@ #include "MarginServiceCore.h" +#include "PositionServiceCore.h" +#include "MarginResult.h" +#include "IMarginObserver.h" +#include "server/sparkd/securities/SecurityDB.h" + #include "lib/config/config.h" #include "lib/api/services/AccountService.pb.h" #include "lib/api/services/UserService.pb.h" - #include "lib/utils/bts_assert.h" #include "lib/logging/logging.h" +#include "lib/utils/helper_macros.h" + +#include "curl/curl.h" +#include "pugixml/pugixml.hpp" #include - -using namespace bts::orderman; +#include +#include namespace bts { @@ -18,17 +26,194 @@ namespace position struct MarginServiceCore::PImpl { - PImpl() + securities::SecurityDB& db; + IMarginObserver* observer; + boost::container::flat_map> results; + + std::string url_base, core_user, core_password, post_margin_path, get_margin_path; + PImpl(securities::SecurityDB& d, IMarginObserver* obs) : + db(d), observer(obs), results(), + url_base("https://cmecorenr.cmegroup.com/MarginServiceApi/1.4/"), core_user("api_bts1"), core_password("BT5u53r!"), + post_margin_path("margins/?complete=true"), + get_margin_path("margins/") + { + auto res = curl_global_init(CURL_GLOBAL_DEFAULT); + if(res != CURLE_OK) + LOG(Gen,Error,"curl_global_init() failed: {}\n", std::string(curl_easy_strerror(res))); + } + ~PImpl() + { + curl_global_cleanup(); + } + + struct CurlMsg + { + std::vector data; + }; + + static size_t create_reply_callback(void* contents, size_t size, size_t nmemb, void* userp) + { + size_t realsize = size * nmemb; + CurlMsg& mem = *(CurlMsg*)userp; + mem.data.reserve(mem.data.size() + realsize + 1); + for(size_t i = 0; i < realsize; i++) + mem.data.push_back(static_cast(contents)[i]); + mem.data.push_back(0); + return realsize; + } + + struct curl_slist* setup_url_and_headers(CURL* curl, const std::string& xpath) + { + //URL and HTTPS + curl_easy_setopt(curl, CURLOPT_URL, url_base + xpath); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + + //HEADERS + struct curl_slist *chunk = nullptr; + chunk = curl_slist_append(chunk, std::string("username:" + core_user).c_str()); + chunk = curl_slist_append(chunk, std::string("password:" + core_password).c_str()); + chunk = curl_slist_append(chunk, "Content-Type:application/xml"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + return chunk; + } + + bool post(const std::string& xpath, const std::string& payload, CurlMsg& reply) + { + CURL* curl = curl_easy_init(); + if (curl) + { + struct curl_slist* header = setup_url_and_headers(curl, xpath); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, create_reply_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload); + + auto res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if(res != CURLE_OK) + LOG(Gen,Error,"POST curl_easy_perform() failed: {}\n", std::string(curl_easy_strerror(res))); + curl_easy_cleanup(curl); + if(header) + curl_slist_free_all(header); + return true; + } + else + LOG(Gen,Error,"curl_easy_init() failed\n"); + return false; + } + + bool get(const std::string& xpath, CurlMsg& reply) + { + CURL* curl = curl_easy_init(); + if (curl) + { + struct curl_slist* header = setup_url_and_headers(curl, xpath); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, create_reply_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&reply); + + auto res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if(res != CURLE_OK) + LOG(Gen,Error,"GET curl_easy_perform() failed: {}\n", std::string(curl_easy_strerror(res))); + curl_easy_cleanup(curl); + if(header) + curl_slist_free_all(header); + return true; + } + else + LOG(Gen,Error,"curl_easy_init() failed\n"); + return false; + } + + //For futures- it is only necessary to populate one of the following: Ticker, or Maturity Code and Clearing Code, or Maturity Code and Product Name. + //so Ticker, Net Positions + void create_margin_request(const std::string& account, pugi::xml_document& doc, pugi::xml_node& trades_node) + { + auto att = doc.append_attribute("standalone"); + att.set_value("yes"); + auto ns = doc.append_child("ns2:marginReq"); + auto ns2 = ns.append_attribute("xmlns:ns2"); + ns2.set_value("http://cmegroup.com/schema/core/1.4"); + auto user = ns.append_attribute("reqUserId"); + user.set_value(core_user.c_str()); + + auto child = ns.append_child("margin"); + child = child.append_child("transactions"); + child = child.append_child("transaction"); + child.append_attribute("type").set_value("TRADE"); + child.append_attribute("portfolioId").set_value(account.c_str()); + child = child.append_child("payload"); + child.append_attribute("format").set_value("CSV"); + child.append_attribute("encoding").set_value("STRING"); + trades_node = child.append_child("string"); + trades_node.set_value("Ticker, Net Positions\n"); + } + + bool parse_margin_response(const CurlMsg& msg, std::string& account, std::string& calculation_id) + { + std::string str(msg.data.begin(), msg.data.end()); + pugi::xml_document doc; + doc.load_string(str.c_str()); + + auto child = doc.child("ns2:marginRpt"); + child = child.child("margin"); + if(child) + { + account = child.attribute("portfolioId").as_string(); + calculation_id = child.attribute("id").as_string(); + return true; + } + else + return false; + } + + void parse_margin_calculation(const CurlMsg& msg, MarginResult& result) + { + result.success = false; + std::string str(msg.data.begin(), msg.data.end()); + pugi::xml_document doc; + doc.load_string(str.c_str()); + + auto child = doc.child("ns2:marginRpt"); + child = child.child("margin"); + if(child) + { + result.settle_indicator = child.attribute("settleInd").as_string(); + result.settle_qualifier = child.attribute("settleQual").as_string(); + result.portfolio_id = child.attribute("portfolioId").as_string(); + result.as_of_time = child.attribute("asOfTime").as_string(); + result.calculation_id = child.attribute("id").as_string(); + auto val = child.child("amounts"); + if(val && result.portfolio_id != "") + { + result.opt_value = child.attribute("optVal").as_double(); + result.nonopt_value = child.attribute("nonOptVal").as_double(); + result.maint_margin = child.attribute("maint").as_double(); + result.initial_margin = child.attribute("init").as_double(); + result.success = true; + } + } } }; -MarginServiceCore::MarginServiceCore() - : me(new PImpl()) +MarginServiceCore::MarginServiceCore(securities::SecurityDB& db, IMarginObserver* obs) + : me(new PImpl(db, obs)) { } MarginServiceCore::~MarginServiceCore() {} +void MarginServiceCore::query_margin(const orderman::AccountName&, MarginResult&) +{ +} + +void MarginServiceCore::test_margin() +{ +} + + } } diff --git a/src/server/sparkd/position/MarginServiceCore.h b/src/server/sparkd/position/MarginServiceCore.h index dcfe6246a..cac65fc2f 100644 --- a/src/server/sparkd/position/MarginServiceCore.h +++ b/src/server/sparkd/position/MarginServiceCore.h @@ -2,26 +2,27 @@ #include "server/sparkd/orderman/utils/orderman_utils.h" #include "lib/domain_model/Security.pb.h" -#include "lib/utils/bts_no_copy.h" #include -#include -#include namespace bts { +namespace securities { class SecurityDB; } namespace position { +struct MarginResult; +class IMarginObserver; class MarginServiceCore { BTS_NO_COPY(MarginServiceCore); public: - MarginServiceCore(); + MarginServiceCore(securities::SecurityDB&, IMarginObserver* obs = nullptr); ~MarginServiceCore(); - // void query_margin(const std::string& account, std::string& as_of_time, - // double& opt_value, double& nonopt_value, double& maint_margin, double& initial_margin) - + //This is a blocking call to a WebAPI. Very Slow. + void query_margin(const orderman::AccountName& account, MarginResult& result); +private: + void test_margin(); private: struct PImpl; diff --git a/src/server/sparkd/position/Position.h b/src/server/sparkd/position/Position.h new file mode 100644 index 000000000..865bafb6c --- /dev/null +++ b/src/server/sparkd/position/Position.h @@ -0,0 +1,19 @@ +#pragma once + +#include "server/sparkd/orderman/utils/orderman_utils.h" +#include "lib/domain_model/Security.pb.h" + +namespace bts +{ +namespace position +{ +struct Position +{ + orderman::AccountName account; + const api::Security* security; + Quantity position; + api::Side side; + Price avg_price; +}; +} +} diff --git a/src/server/sparkd/position/PositionServiceCore.cpp b/src/server/sparkd/position/PositionServiceCore.cpp index bd6ed6eb4..e2fb36f10 100644 --- a/src/server/sparkd/position/PositionServiceCore.cpp +++ b/src/server/sparkd/position/PositionServiceCore.cpp @@ -1,5 +1,6 @@ #include "PositionServiceCore.h" +#include "Position.h" #include "IPositionObserver.h" #include "server/sparkd/orderman/utils/orderman_utils.h" #include "server/sparkd/securities/SecurityDB.h" @@ -21,33 +22,29 @@ template int sgn(T val) { class BookEntry { - orderman::AccountName _account; - SecurityKey _key; - double _position; - double _avg_price; +public: + orderman::AccountName account; + SecurityKey security_key; + double position; + double avg_price; public: BookEntry(const orderman::AccountName& acc, SecurityKey k) : - _account(acc), _key(k), _position(0.0), _avg_price(0.0) + account(acc), security_key(k), position(0.0), avg_price(0.0) { } void add_fill(double last_size, double last_price, api::Side side) { if(side == api::SIDE_SELL) last_size = -last_size; - if(_position == 0.0 || sgn(_position) == sgn(last_size)) + if(position == 0.0 || sgn(position) == sgn(last_size)) { - double prev = _position * _avg_price; - _position += last_size; - _avg_price = prev + (last_size*last_price) / _position; + double prev = position * avg_price; + position += last_size; + avg_price = prev + (last_size*last_price) / position; } else - _position += last_size; + position += last_size; } - void position_and_price(double& position, double& avg_price) - { - position = _position; - avg_price = _avg_price; - } }; struct PositionServiceCore::PImpl @@ -74,11 +71,7 @@ struct PositionServiceCore::PImpl auto& entry = find_entry(account, sec); entry.add_fill(last_size, last_price, side); if(observer) - { - double position, avg_price; - entry.position_and_price(position, avg_price); - observer->on_position_change(account, sec, position, avg_price); - } + observer->on_position_change(account, sec, entry.position, entry.avg_price); } }; @@ -89,13 +82,43 @@ PositionServiceCore::PositionServiceCore(securities::SecurityDB& db, IPositionOb PositionServiceCore::~PositionServiceCore() {} -void PositionServiceCore::query_position(const orderman::AccountName& account, SecurityKey sec, - double& position, double& avg_price) -{ - auto& entry = me->find_entry(account, sec); - entry.position_and_price(position, avg_price); +bool PositionServiceCore::query_position(const orderman::AccountName& account, SecurityKey seckey, Position& result) +{ + auto& entry = me->find_entry(account, seckey); + auto sec = _db.resolve(entry.security_key); + if(sec) + { + result.account = entry.account; + result.avg_price = Price(entry.avg_price); + result.position = Quantity(abs(entry.position)); + result.side = (entry.position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY); + result.security = sec; + return true; + } + else + return false; } +void PositionServiceCore::query_all_positions(const orderman::AccountName& account, std::vector& result) +{ + result.reserve(me->book.size()); + for(auto& entry : me->book) + { + if(entry.first.first != account) + continue; + auto sec = _db.resolve(entry.second->security_key); + if(sec) + { + Position pos; + pos.account = entry.second->account; + pos.avg_price = Price(entry.second->avg_price); + pos.position = Quantity(abs(entry.second->position)); + pos.side = (entry.second->position < 0.0 ? api::SIDE_SELL : api::SIDE_BUY); + pos.security = sec; + result.push_back(pos); + } + } +} void PositionServiceCore::consume_raw_fill(const std::string& account, uint32_t security_key, double last_size, double last_price, api::Side side) diff --git a/src/server/sparkd/position/PositionServiceCore.h b/src/server/sparkd/position/PositionServiceCore.h index 361f4600d..d382035ee 100644 --- a/src/server/sparkd/position/PositionServiceCore.h +++ b/src/server/sparkd/position/PositionServiceCore.h @@ -11,6 +11,7 @@ namespace bts namespace securities { class SecurityDB; } namespace position { +struct Position; class IPositionObserver; class PositionServiceCore { @@ -19,7 +20,8 @@ public: PositionServiceCore(securities::SecurityDB&, IPositionObserver* obs = nullptr); ~PositionServiceCore(); - void query_position(const orderman::AccountName&, SecurityKey, double& position, double& avg_price); + bool query_position(const orderman::AccountName&, SecurityKey, Position&); + void query_all_positions(const orderman::AccountName&, std::vector&); private: void consume_raw_fill(const std::string& account, uint32_t security_key, diff --git a/src/server/sparkd/risk_limits/AccountRiskLimiter.cpp b/src/server/sparkd/risk_limits/AccountRiskLimiter.cpp index 8a14e65e0..b61388c38 100644 --- a/src/server/sparkd/risk_limits/AccountRiskLimiter.cpp +++ b/src/server/sparkd/risk_limits/AccountRiskLimiter.cpp @@ -23,15 +23,14 @@ namespace risklimits { SharedLimiterData::SharedLimiterData(const AccountName& acct, const SymbolKey& sym) : - account_info({ acct, 0, 1.0, false }), + account_info(api::account::Account::default_instance()), position_info({ sym, 0u, 0u, 0u, 0u }), risk_limit(), info_string() { - std::string account, opid; - to_str(account_info.account.value(), &account); - info_string = " Additional Information: AccountName: " + account - + " Symbol: " + std::get<0>(position_info.symbol); + std::string account; + to_str(acct.value(), &account); + info_string = " Additional Information: AccountName: " + account + " Symbol: " + std::get<0>(position_info.symbol); } @@ -87,17 +86,17 @@ void AccountRiskLimiter::allowed_operators(std::vector& op void AccountRiskLimiter::on_account_update(const api::account::Account& acct) { //TODO This is a little strange-- we update the account, but not the limits, - //we break those down above and call down below. Might want to rework this - bts_assert(to(acct.tag1()) == me->shared_data.account_info.account); - bool prev = me->shared_data.account_info.can_trade; - me->shared_data.account_info.can_trade = acct.can_trade(); - me->shared_data.account_info.cash_position = acct.balance().cash_position(); - me->shared_data.account_info.leverage_factor = acct.balance().leverage_factor(); - if(prev != me->shared_data.account_info.can_trade) - { - for(const auto& i : me->operator_map) - i.second->on_account_update(); - } + //we break those down above and call down below. Might want to rework this, but the reason is because the scope of this object + //is Account,Symbolname + bts_assert(acct.tag1() == me->shared_data.account_info.tag1()); + me->shared_data.account_info = acct; + for(const auto& i : me->operator_map) + i.second->on_account_update(); +} + +void AccountRiskLimiter::on_risk_limit_update(const api::account::RiskLimit& risk) +{ + me->shared_data.risk_limit = risk; } void AccountRiskLimiter::on_operator_update(const api::user::Operator& oper) @@ -106,9 +105,9 @@ void AccountRiskLimiter::on_operator_update(const api::user::Operator& oper) lim.on_operator_update(oper); } -void AccountRiskLimiter::on_risk_limit_update(const api::account::RiskLimit& risk) +void AccountRiskLimiter::on_margin_update(const position::MarginResult& margin) { - me->shared_data.risk_limit = risk; + me->shared_data.margin = margin; } const api::account::RiskLimit& AccountRiskLimiter::risk_limit() diff --git a/src/server/sparkd/risk_limits/AccountRiskLimiter.h b/src/server/sparkd/risk_limits/AccountRiskLimiter.h index c2db50dc7..5a2eed135 100644 --- a/src/server/sparkd/risk_limits/AccountRiskLimiter.h +++ b/src/server/sparkd/risk_limits/AccountRiskLimiter.h @@ -1,7 +1,8 @@ #pragma once #include "server/sparkd/orderman/utils/orderman_utils.h" -#include "SymbolKey.h" +#include "server/sparkd/risk_limits/SymbolKey.h" +#include "server/sparkd/position/MarginResult.h" #include "lib/api/services/AccountService.pb.h" #include "lib/api/services/UserService.pb.h" #include "lib/utils/bts_no_copy.h" @@ -11,20 +12,6 @@ namespace bts { -namespace api -{ - namespace user - { - class User; - class Operator; - } - namespace account - { - class Account; - class AccountBalance; - class RiskLimit; - } -} namespace risklimits { class RiskLimitManager; @@ -34,12 +21,12 @@ class SharedLimiterData BTS_NO_COPY(SharedLimiterData); public: SharedLimiterData(const orderman::AccountName&, const SymbolKey&); - struct AccountInfo { orderman::AccountName account; - int cash_position; double leverage_factor; bool can_trade; } account_info; + api::account::Account account_info; struct PositionInfo { SymbolKey symbol; uint32_t long_position; uint32_t short_position; uint32_t long_working; uint32_t short_working; } position_info; api::account::RiskLimit risk_limit; + position::MarginResult margin; std::string info_string; }; @@ -59,7 +46,8 @@ protected: friend class RiskLimitManager; void on_account_update(const api::account::Account&); void on_risk_limit_update(const api::account::RiskLimit&); - void on_operator_update(const api::user::Operator&); + void on_operator_update(const api::user::Operator&); + void on_margin_update(const position::MarginResult&); protected: friend class RiskLimitsTests; diff --git a/src/server/sparkd/risk_limits/OperatorRiskLimiter.cpp b/src/server/sparkd/risk_limits/OperatorRiskLimiter.cpp index 7d6f730e9..4bf0719aa 100644 --- a/src/server/sparkd/risk_limits/OperatorRiskLimiter.cpp +++ b/src/server/sparkd/risk_limits/OperatorRiskLimiter.cpp @@ -42,8 +42,8 @@ struct OperatorRiskLimiter::PImpl void notify() { - bool status = operator_can_trade && account.shared_data().account_info.can_trade; - RiskLimitState state = { account.shared_data().account_info.account, + bool status = operator_can_trade && account.shared_data().account_info.can_trade(); + RiskLimitState state = { to(account.shared_data().account_info.tag1()), operator_id, status }; auto obs = account.manager().observer(); if(obs) diff --git a/src/server/sparkd/risk_limits/RiskLimitManager.cpp b/src/server/sparkd/risk_limits/RiskLimitManager.cpp index ad24caaa2..0e6c78e6a 100644 --- a/src/server/sparkd/risk_limits/RiskLimitManager.cpp +++ b/src/server/sparkd/risk_limits/RiskLimitManager.cpp @@ -13,7 +13,7 @@ #include "lib/utils/helper_macros.h" #include -#include +#include using namespace bts::orderman; @@ -21,7 +21,7 @@ namespace bts { namespace risklimits { -using KeyType = std::tuple; +using KeyType = std::pair; using SharedLimiterMap = boost::container::flat_map>; using OperatorMap = boost::container::flat_map; @@ -110,6 +110,13 @@ RiskLimiter& RiskLimitManager::find_limiter(const AccountName& acctname, const O return sym.find_limiter(opid, key); } +void RiskLimitManager::on_margin_update(const orderman::AccountName& account, const position::MarginResult& margin) +{ + for(const auto& i : me->limiter_map) + if(i.first.first == account) + i.second->on_margin_update(margin); +} + AccountRiskLimiter& RiskLimitManager::find_shared_limiter(const AccountName& acctname, const api::Security& security) { AccountRiskLimiter& shared = me->find_limiter(acctname, me->to_symbolkey(security)); diff --git a/src/server/sparkd/risk_limits/RiskLimitManager.h b/src/server/sparkd/risk_limits/RiskLimitManager.h index 4f97962e6..3ef68bf5d 100644 --- a/src/server/sparkd/risk_limits/RiskLimitManager.h +++ b/src/server/sparkd/risk_limits/RiskLimitManager.h @@ -23,6 +23,7 @@ namespace api class AccountBalance; } } +namespace position { struct MarginResult; } namespace risklimits { class IRiskLimitObserver; @@ -34,12 +35,12 @@ class RiskLimitManager public: RiskLimitManager(IRiskLimitObserver* observer = nullptr); ~RiskLimitManager(); - - RiskLimiter& find_limiter(const orderman::AccountName&, const orderman::OperatorID&, - const api::Security&, orderman::OrderKey); + void initialize(const std::vector&, const std::vector&); + RiskLimiter& find_limiter(const orderman::AccountName&, const orderman::OperatorID&, + const api::Security&, orderman::OrderKey); + void on_margin_update(const orderman::AccountName&, const position::MarginResult&); IRiskLimitObserver* observer(); - protected: friend class RiskLimitsTests; AccountRiskLimiter& find_shared_limiter(const orderman::AccountName&, const api::Security&); diff --git a/src/server/sparkd/risk_limits/RiskLimiter.cpp b/src/server/sparkd/risk_limits/RiskLimiter.cpp index b377223c6..0bdfeb259 100644 --- a/src/server/sparkd/risk_limits/RiskLimiter.cpp +++ b/src/server/sparkd/risk_limits/RiskLimiter.cpp @@ -38,9 +38,9 @@ struct RiskLimiter::PImpl } bool check_can_trade(std::string& message) const { - if(!shared.account_info.can_trade) + if(!shared.account_info.can_trade()) { - std::string acct; to_str(shared.account_info.account.value(), &acct); + const std::string& acct = shared.account_info.tag1(); message = fmt::format("Account {} does not have permission to trade. {}", acct, shared.info_string); return false; } diff --git a/src/server/sparkd/risk_limits/tests/RiskLimitsTests.cpp b/src/server/sparkd/risk_limits/tests/RiskLimitsTests.cpp index 481d6993a..ad838f5fa 100644 --- a/src/server/sparkd/risk_limits/tests/RiskLimitsTests.cpp +++ b/src/server/sparkd/risk_limits/tests/RiskLimitsTests.cpp @@ -232,7 +232,7 @@ TEST_CASE ("RiskLimits Limits", "[risklimits]") api::Security sec = api::Security::default_instance(); setup_security(sec, data.position_info.symbol); OrderKey key(0); - RiskLimiter& lim = _manager.find_limiter(data.account_info.account, operators[0], sec, key); + RiskLimiter& lim = _manager.find_limiter(to(data.account_info.tag1()), operators[0], sec, key); auto res = lim.check(Quantity(rand() % limits.max_order_size()), api::SIDE_BUY); REQUIRE(res.success); res = lim.check(Quantity(rand() % limits.max_order_size()), api::SIDE_SELL); @@ -244,7 +244,7 @@ TEST_CASE ("RiskLimits Limits", "[risklimits]") { api::account::Account new_account = api::account::Account::default_instance(); - new_account.set_tag1(to_str(data.account_info.account)); + new_account.set_tag1(data.account_info.tag1()); new_account.set_can_trade(false); RiskLimitsTests::account_update(*i.get(), new_account); bool ok = limits_ok(state); @@ -312,9 +312,9 @@ TEST_CASE ("RiskLimits Limits", "[risklimits]") auto maxorder = limits.max_order_size(); OrderKey key1(0); - RiskLimiter& lim1 = manager.find_limiter(data->account_info.account, operators[0], sec, key1); + RiskLimiter& lim1 = manager.find_limiter(to(data->account_info.tag1()), operators[0], sec, key1); OrderKey key2(1); - RiskLimiter& lim2 = manager.find_limiter(data->account_info.account, operators[1], sec, key2); + RiskLimiter& lim2 = manager.find_limiter(to(data->account_info.tag1()), operators[1], sec, key2); lim1.on_order_state(side, Quantity(maxpos), Quantity(maxpos)); auto r1 = lim1.check(Quantity(maxorder), side); REQUIRE(!r1.success); @@ -366,13 +366,13 @@ TEST_CASE ("RiskLimits Limits", "[risklimits]") int iters = 2; for(int i = 0; i < iters; i++) { - RiskLimiter& lim = observed_manager.find_limiter(data.account_info.account, operators[0], sec, key); + RiskLimiter& lim = observed_manager.find_limiter(to(data.account_info.tag1()), operators[0], sec, key); SharedLimiterData& shared2 = RiskLimitsTests::shared_data(lim); auto limsym = shared2.position_info.symbol.first; auto limtype = shared2.position_info.symbol.second; REQUIRE(limsym == secsym); REQUIRE(type == sectype); - REQUIRE(shared2.account_info.account == data.account_info.account); + REQUIRE(shared2.account_info.tag1() == data.account_info.tag1()); REQUIRE(&shared2 == &data); RiskLimiter::RiskLimitResult res;