#include "Limiter.h" #include "LimitStore.h" #include "lib/api/services/AdminService.server.pb.h" #include "lib/api/services/PositionService.server.pb.h" #include "lib/domain_model/Security.pb.h" namespace bts { namespace orderman { struct Limiter::PImpl { std::map sec_map; std::unique_ptr limit_store; PImpl() : sec_map(), limit_store(std::make_unique( bts::api::admin::AdminData(), sec_map)) {} }; Limiter::Limiter() : me(new PImpl()) {} Limiter::~Limiter() {} void Limiter::update(api::admin::AdminData const &admin_data) { LOG(OrderMan, Info, "Limiter::update"); _reset(admin_data); } void Limiter::add_securities( std::vector const &new_securities) { LOG(OrderMan, Info, "Limiter::add_securities {} securities", me->sec_map.size()); for (const api::Security *s : new_securities) _add_security(s); _reset(me->limit_store->admin_data()); } void Limiter::add_security(api::Security const &security) { _add_security(&security); _reset(me->limit_store->admin_data()); } void Limiter::set_filled_position(api::Security const &security, AccountID account_id, Quantity long_pos, Quantity short_pos){ // this means a security was added and filled before // add_securities was called or before add was called // (we can put it in a queue and examine it on the // add_securities call) if (!_contains_security(&security)) LOG(OrderMan, Error, "Limiter::set_filled_position could not find" "sec_key:{} this should be investigated", security.id()); else{ if (!me->limit_store->has_account(account_id)) // add a temporary account, so trades can go in there _add_account_and_reset(account_id); me->limit_store->set_filled_position(security, account_id, long_pos, short_pos); } } void Limiter::add_to_working_position(api::Security const &security, AccountID account_id, api::Side side, Quantity quantity){ if (!_contains_security(&security)) add_security(security); if (!me->limit_store->has_account(account_id)) { // add a temporary account, so trades can go in there _add_account_and_reset(account_id); } me->limit_store->add_working_order(security, account_id, side, quantity, true); } void Limiter::remove_from_working_position(api::Security const &security, AccountID account_id, api::Side side, Quantity quantity) { if (!_contains_security(&security)) add_security(security); if (!me->limit_store->has_account(account_id)) { _add_account_and_reset(account_id); } me->limit_store->remove_working_order(security, account_id, side, quantity, true); } void Limiter::set_day_pnl (AccountID account_id, double pnl){ if (!me->limit_store->has_account(account_id)) _add_account_and_reset(account_id); me->limit_store->set_pnl(account_id, pnl); } void Limiter::set_excess_liquidity (AccountID account_id, double excess_liquidity) { LOG(OrderMan, Info, "Limiter::set_excess_liquidity account:{} excess_liquidity: {}", account_id, excess_liquidity); if (!me->limit_store->has_account(account_id)) _add_account_and_reset(account_id); me->limit_store->set_excess_liquidity(account_id, excess_liquidity); } Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id, AccountID account_id) const { LimiterQueryResult rv = me->limit_store->can_trade(user_id, account_id); return rv; } Limiter::LimiterQueryResult Limiter::can_send_new_order(AccountID account_id, api::Security const &security, api::Side side, Quantity quantity) { LimiterQueryResult rv = me->limit_store->can_send_new_order( account_id, security, side, quantity); if (rv.result == Limiter::NO_SUCH_SECURITY) { add_security(security); rv = me->limit_store->can_send_new_order( account_id, security, side, quantity); } return rv; } Limiter::LimiterQueryResult Limiter::can_update_order(AccountID account_id, api::Security const &security, api::Side side, Quantity from, Quantity to) { LimiterQueryResult rv; if (from == to) rv.result = Limiter::OK; else { rv = me->limit_store->can_update_order(account_id, security, side, from, to); if (rv.result == Limiter::NO_SUCH_SECURITY) { add_security(security); rv = me->limit_store->can_update_order(account_id, security, side, from, to); } } return rv; } void Limiter::_add_account_and_reset(AccountID account_id) { bts::api::admin::AdminData admin_data_catchup = me->limit_store->admin_data(); bts::api::admin::BTSAccount *new_account = admin_data_catchup.mutable_accounts()->Add(); new_account->set_id(account_id.value()); new_account->set_is_active(true); update(admin_data_catchup); } void Limiter::_reset(const bts::api::admin::AdminData &admin_data) { LimitStore *new_store = new LimitStore(admin_data, me->sec_map); new_store->copy_data_from(*me->limit_store); me->limit_store.reset(new_store); } void Limiter::_add_security(const api::Security *sec) { if (!_contains_security(sec)) { me->sec_map.insert( std::pair(sec->id(), sec)); if (sec->has_spread()) for (const auto &leg : sec->spread().legs()) _add_security(&leg.security()); } } bool Limiter::_contains_security(uint32_t security_key) { return me->sec_map.find(security_key) != me->sec_map.end(); } bool Limiter::_contains_security(const api::Security *security) { return me->sec_map.find(security->id()) != me->sec_map.end(); } LimitStore &Limiter::limit_store() { return *me->limit_store; } } // namespace orderman } // namespace bts