112 lines
4.6 KiB
C++
112 lines
4.6 KiB
C++
#include "LimitStore.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
LimitStore::LimitStore(const bts::api::admin::AdminData &admin_data,
|
|
const std::map<uint32_t, api::Security const *> &sec_map)
|
|
: _admin_data(bts::admin::AdminServiceUtils::sort_limits(admin_data)) {
|
|
for (const auto& acct : _admin_data.accounts())
|
|
_account_data_map.insert(std::pair<uint32_t,AccountData>(
|
|
acct.id(),AccountData(acct)));
|
|
for (auto it = sec_map.begin(); it != sec_map.end(); it++) {
|
|
const api::Security &security = *(it->second);
|
|
_add_product_entry(security, _admin_data);
|
|
}
|
|
for (auto it = sec_map.begin(); it != sec_map.end(); it++) {
|
|
// pos limit
|
|
const api::Security &security = *(it->second);
|
|
ProductLimitTree *product_tree_ptr = _find_product_for_security(security);
|
|
bts_assert(product_tree_ptr != nullptr);
|
|
_sec_map.insert(std::pair<uint32_t, SecurityLimitTree>(
|
|
security.id(), SecurityLimitTree(security, *product_tree_ptr,
|
|
_admin_data, _zero_security_limit)));
|
|
}
|
|
_set_user_data();
|
|
}
|
|
void LimitStore::copy_data_from(LimitStore &source) {
|
|
// copy securities
|
|
for (auto it = _sec_map.begin(); it != _sec_map.end(); it++) {
|
|
SecurityLimitTree *source_security_limit_tree =
|
|
source._find_security(it->first);
|
|
if (source_security_limit_tree != nullptr)
|
|
it->second.copy_positions_from(*source_security_limit_tree);
|
|
}
|
|
// copy pnl's and liqs
|
|
for (auto it=_account_data_map.begin();it!=_account_data_map.end();it++){
|
|
auto source_it = source._account_data_map.find(it->first);
|
|
if(source_it != source._account_data_map.end()) {
|
|
it->second.curr_day_pnl = source_it->second.curr_day_pnl;
|
|
it->second.excess_liquidity = source_it->second.excess_liquidity;
|
|
}
|
|
}
|
|
}
|
|
void LimitStore::set_pnl (AccountID account_id, double pnl){
|
|
auto it = _account_data_map.find(account_id.value());
|
|
bts_assert(it != _account_data_map.end());
|
|
it->second.curr_day_pnl = pnl;
|
|
}
|
|
void LimitStore::set_excess_liquidity (AccountID account_id,
|
|
double excess_liquidity){
|
|
auto it = _account_data_map.find(account_id.value());
|
|
bts_assert(it != _account_data_map.end());
|
|
it->second.excess_liquidity = excess_liquidity;
|
|
}
|
|
void LimitStore::_add_product_entry(
|
|
const api::Security &security,
|
|
const bts::api::admin::AdminData &admin_data) {
|
|
ProductLimitTree *product_tree_ptr = _find_product_for_security(security);
|
|
if (product_tree_ptr == nullptr) {
|
|
api::Product product;
|
|
product.set_exchange(security.exchange());
|
|
product.set_type(bts::admin::AdminServiceUtils::security_type(security));
|
|
product.set_symbol(security.symbol());
|
|
_prod_map.emplace_back(product, admin_data, _zero_product_limit);
|
|
}
|
|
}
|
|
void LimitStore::_set_user_data() {
|
|
for (const auto &user : _admin_data.users()) {
|
|
if (user.is_present()) {
|
|
UserData user_data;
|
|
user_data.user = &user;
|
|
bts::admin::AdminServiceUtils::find_user_trading_accounts(
|
|
_admin_data, user.id(), user_data.accounts);
|
|
_user_data_map.insert(
|
|
std::pair<uint32_t, UserData>(user.id(), user_data));
|
|
}
|
|
}
|
|
}
|
|
ProductLimitTree *
|
|
LimitStore::_find_product_for_security(const api::Security &security) {
|
|
api::Product product;
|
|
product.set_exchange(security.exchange());
|
|
product.set_type(bts::admin::AdminServiceUtils::security_type(security));
|
|
product.set_symbol(security.symbol());
|
|
return _find_product(product);
|
|
}
|
|
ProductLimitTree *LimitStore::_find_product(const api::Product &product) {
|
|
for (ProductLimitTree &plt : _prod_map)
|
|
if (product.exchange() == plt.product().exchange() &&
|
|
product.type() == plt.product().type() &&
|
|
product.symbol() == plt.product().symbol())
|
|
return &plt;
|
|
return nullptr;
|
|
}
|
|
const SecurityLimitNode *
|
|
LimitStore::test_only_func_find_security_node(AccountID account_id,
|
|
uint32_t security_id) {
|
|
const SecurityLimitTree *sec_data = _find_security(security_id);
|
|
if (sec_data != nullptr)
|
|
return sec_data->find_account(account_id.value());
|
|
return nullptr;
|
|
}
|
|
const ProductLimitNode *
|
|
LimitStore::test_only_func_find_product_node(AccountID account_id,
|
|
const api::Product &product) {
|
|
ProductLimitTree *prod_data = _find_product(product);
|
|
if (prod_data != nullptr)
|
|
return prod_data->find_account(account_id.value());
|
|
return nullptr;
|
|
}
|
|
} // namespace orderman
|
|
} // namespace bts
|