140 lines
4.7 KiB
C++
140 lines
4.7 KiB
C++
#include "RiskLimitManager.h"
|
|
|
|
#include "SharedRiskLimiter.h"
|
|
#include "RiskLimitState.h"
|
|
#include "IRiskLimitObserver.h"
|
|
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/Account.pb.h"
|
|
#include "lib/config/protos/User.pb.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/logging/logging.h"
|
|
|
|
#include <boost/container/flat_map.hpp>
|
|
#include <tuple>
|
|
|
|
using namespace bts::orderman;
|
|
|
|
namespace bts {
|
|
namespace risklimits {
|
|
|
|
using OperatorMap = boost::container::flat_map<std::string, config::Operator>;
|
|
|
|
using SymbolKey = std::tuple<std::string, api::Security::ContractCase>;
|
|
using KeyType = std::tuple<AccountName, OperatorID, SymbolKey>;
|
|
using SharedLimiterMap = boost::container::flat_map<KeyType, std::shared_ptr<SharedRiskLimiter>>;
|
|
|
|
using StateKeyType = std::tuple<AccountName, OperatorID>;
|
|
using PBLimitsMap = const google::protobuf::Map<std::__cxx11::string, bts::config::RiskLimit>;
|
|
|
|
struct RiskLimitManager::PImpl
|
|
{
|
|
RiskLimitManager& this_ptr;
|
|
OperatorMap operator_map;
|
|
SharedLimiterMap limiter_map;
|
|
std::vector<IRiskLimitObserver*> observers;
|
|
boost::container::flat_map<StateKeyType, RiskLimitState> aggregate_state;
|
|
|
|
PImpl(RiskLimitManager& rm) :
|
|
this_ptr(rm),
|
|
operator_map(), limiter_map(), observers(), aggregate_state()
|
|
{
|
|
auto users = config::get<config::UserCfg>();
|
|
for(int i = 0; i < users.user_size(); i++)
|
|
{
|
|
auto user = users.user(i);
|
|
for(int j = 0; i < user.operators_size(); j++)
|
|
operator_map[user.operators(j).operator_id().id()] = user.operators(j);
|
|
}
|
|
|
|
auto accts = config::get<config::AccountCfg>();
|
|
for(int i = 0; i < accts.accounts_with_limits_size(); i++)
|
|
{
|
|
const auto& acct = accts.accounts_with_limits(i).account();
|
|
const auto& future_limit = accts.accounts_with_limits(i).future_limits();
|
|
update_limits(future_limit, acct, api::Security::kFuture);
|
|
const auto& spread_limit = accts.accounts_with_limits(i).spread_limits();
|
|
update_limits(spread_limit, acct, api::Security::kSpread);
|
|
}
|
|
}
|
|
|
|
void update_limits(PBLimitsMap & map, const bts::config::Account & acct, api::Security::ContractCase contract_type)
|
|
{
|
|
for(auto j = map.begin(); j != map.end(); j++)
|
|
{
|
|
for(int k = 0; k < acct.allowed_operators_size(); k++)
|
|
{
|
|
auto allowed = operator_map.find(acct.allowed_operators(k).id());
|
|
if(allowed != operator_map.end())
|
|
{
|
|
OperatorID opid(allowed->second.operator_id().id());
|
|
SharedRiskLimiter& lim = find_limiter(to_accountname(acct), opid, to_symbolkey(j->first, contract_type));
|
|
lim.on_account_update(acct);
|
|
lim.on_operator_update(allowed->second);
|
|
lim.on_risk_limit_update(j->second);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SymbolKey to_symbolkey(const api::Security& security)
|
|
{
|
|
return std::make_tuple("ES", security.Contract_case());
|
|
}
|
|
SymbolKey to_symbolkey(const std::string& symbol, api::Security::ContractCase type)
|
|
{
|
|
return std::make_tuple(symbol, type);
|
|
}
|
|
AccountName to_accountname(const config::Account& key)
|
|
{
|
|
return AccountName(key.account_id());
|
|
}
|
|
|
|
SharedRiskLimiter& find_limiter(AccountName acctname, OperatorID opid, SymbolKey symkey)
|
|
{
|
|
KeyType key(acctname, opid, symkey);
|
|
auto lim = limiter_map.find(key);
|
|
if(lim != limiter_map.end())
|
|
return *lim->second;
|
|
else
|
|
{
|
|
SharedRiskLimiter* lim = new SharedRiskLimiter(this_ptr, std::get<0>(key), std::get<1>(key), std::get<2>(key));
|
|
auto ptr = std::shared_ptr<SharedRiskLimiter>(lim);
|
|
limiter_map[key] = ptr;
|
|
return *ptr;
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
RiskLimitManager::RiskLimitManager() : me(new PImpl(*this)) {}
|
|
RiskLimitManager::~RiskLimitManager() {}
|
|
|
|
RiskLimiter& RiskLimitManager::find_limiter(AccountName acctname, OperatorID opid,
|
|
const api::Security& security, OrderKey key)
|
|
{
|
|
auto&& sym = me->find_limiter(acctname, opid, me->to_symbolkey(security));
|
|
return sym.find_limiter(key);
|
|
}
|
|
|
|
void RiskLimitManager::subscribe(IRiskLimitObserver& obs) const
|
|
{
|
|
me->observers.push_back(&obs);
|
|
}
|
|
|
|
void RiskLimitManager::on_limit_state_change(RiskLimitState state)
|
|
{
|
|
StateKeyType sk = std::make_tuple(state.account_name, state.operator_id);
|
|
me->aggregate_state[sk] = state;
|
|
|
|
std::vector<RiskLimitState> res;
|
|
for(auto i = me->aggregate_state.begin(); i != me->aggregate_state.end(); i++)
|
|
res.push_back(i->second);
|
|
for(auto& i : me->observers)
|
|
i->on_limit_state(res);
|
|
}
|
|
|
|
}
|
|
} |