#include "lib/api/services/OrderService.client.pb.h" #include "lib/logging/logging.h" #include "lib/md/MDClient.h" #include "lib/os/os.h" #include "PositionServicePNLHelper.h" #include "lib/config/protos/cme_reference_data.pb.h" #include "lib/config/config.h" //#include "PositionUtilities.h" #include "lib/utils/LockBox.h" #include #include #include #include #include #include #include namespace bts { namespace position{ using namespace bts::api::position; using namespace bts::api::orderman; using namespace bts::md; class HandleIndex { public: enum PNLType{ UNRESOLVED = 0, SPREAD, OUTRIGHT }; bts::md::MDClient& _client; int _sub; std::optional _consumer; public: utils::LockBox> index; HandleIndex(md::MDClient& client): _client(client), _sub(0), _consumer(std::nullopt), index() { _sub = _client.on_consumer().subscribe([=](const MDConsumer& c) { on_consumer(c); }); on_consumer(_client.current_consumer()); } ~HandleIndex() { _client.on_consumer().unsubscribe(_sub); } MDConsumerHandle _get_handle(SecurityKey key) const{ auto handle = index.lock([id=key](const auto& index) { auto iter = index.find(id); return iter != index.end() ? iter->second : MDConsumerHandle {}; }); return handle; } PNLType get_pnl_type (SecurityKey key) const{ auto h = _get_handle(key); if (h.is_initialized()){ const auto& sec = h.get_normalized_security(); if (sec.has_future() || sec.has_option()) return PNLType::OUTRIGHT; else return PNLType::SPREAD; } else return PNLType::UNRESOLVED; } std::string get_symbol (SecurityKey key) const{ auto handle = _get_handle(key); return handle.get_usersymbol().symbol(); } void get_md(SecurityKey key, double& bid, double& ask, double& last, double& settle){ auto handle = _get_handle(key); bid = bts::os::nan(); ask = bid; last = bid; settle = bid; if(handle.is_initialized()){ TimeStamp time; PriceSize bid_ps,ask_ps; handle.get_direct_book(time, 1, &bid_ps, &ask_ps); bid = bid_ps.price(); ask = ask_ps.price(); PriceSize last_ps; datetime::LLDateTime trade_time; handle.get_last_trade(time, last_ps, trade_time, nullptr); last = last_ps.price(); settle = handle.settlement_price().price(); } } private: void on_consumer(const MDConsumer& c) { MDConsumer temp(c); if (temp.all_heartbeats_ok()) { auto existing = _consumer ? _consumer->id() : 0; LOG(md,Info, "Updating MBOService MDConsumer. Old {} new {}", existing, temp.id()); // clear the index index.lock([](auto& index) { index.clear(); }); // Swap to new consumer if (_consumer) { _consumer->swap(temp); } else { _consumer = temp; } // Subscribe to on_security auto id = _consumer->id(); _consumer->on_security([this,id](const SecurityPtr&, MDConsumerHandle handle) { if (_consumer->id() == id) { index.lock([handle](auto& index) { index[SecurityKey(handle.key())] = handle; }); } }); } } }; struct PositionServicePNLHelper::PImpl { HandleIndex handle_index; std::map symbol_cpp_map; std::map sec_cpp_map; PImpl(bts::md::MDClient& md_client) : handle_index(md_client), symbol_cpp_map(), sec_cpp_map() { const auto& cpp_map = config::get().cpp_entry(); LOG(md,Info,"PositionServicePNLHelper::cons loaded {} cpps",cpp_map.size()); for (const auto& p : cpp_map){ symbol_cpp_map[std::string(p.first)]=p.second; LOG(md,Info,"PositionServicePNLHelper loading cpp's Sym:{} cpp:{}", p.first,p.second); } } }; void PositionServicePNLHelper::compute_pnl( const std::map& in_pos, const std::map& account_map, std::vector& out_pnl, std::vector& out_account_pnl){ out_account_pnl.clear(); for (const auto& p : in_pos) { const PositionUnit& pu = *(p.second); uint32_t sec_key = pu.security_id(); double cpp = _get_cpp(sec_key); double bid, ask, last, settle; me->handle_index.get_md(SecurityKey(sec_key),bid,ask,last,settle); PNLUnit pnlu; pnlu.set_record_id(pu.record_id()); pnlu.mutable_pos()->CopyFrom(pu); pnlu.set_cash_value(cpp); pnlu.set_bid_price(bid); pnlu.set_ask_price(ask); pnlu.set_last_price(last); pnlu.set_settlement_price(settle); out_pnl.push_back(pnlu); } _aggregate_account_pnl(out_pnl,account_map,out_account_pnl); } void PositionServicePNLHelper::_aggregate_account_pnl( const std::vector& in_pnl, const std::map& account_map, std::vector& out_account_pnl){ out_account_pnl.clear(); if (account_map.size() == 0) return; std::map acc_pnl; for (const auto& pnlu : in_pnl){ uint32_t acct_id = pnlu.pos().account_id(); const std::string& acct_name = pnlu.pos().account(); auto it=acc_pnl.find(acct_id); if(it==acc_pnl.end()){ AccountPNL new_acc; new_acc.set_account_id(acct_id); new_acc.set_account_name(acct_name); new_acc.set_day_pnl(0.0); acc_pnl[acct_id]=new_acc; it=acc_pnl.find(acct_id); } AccountPNL& acc_pnl = it->second; double pnl_dollars = _compute_day_pnl(pnlu); acc_pnl.set_day_pnl(acc_pnl.day_pnl()+pnl_dollars); } // for accounts with no positions add 0 for (const auto& p : account_map){ const std::string& acct_name = p.first; uint32_t acct_id = p.second; if(acc_pnl.find(acct_id) == acc_pnl.end()){ AccountPNL new_acc; new_acc.set_account_id(acct_id); new_acc.set_account_name(acct_name); new_acc.set_day_pnl(0.0); acc_pnl[acct_id]=new_acc; } } for(auto& u : acc_pnl) out_account_pnl.push_back(u.second); } double PositionServicePNLHelper::_compute_day_pnl ( const bts::api::position::PNLUnit& u){ // long double pnl; auto pnl_type = me->handle_index.get_pnl_type( SecurityKey(u.pos().security_id())); switch(pnl_type){ case HandleIndex::OUTRIGHT:{ double md = (u.bid_price() + u.ask_price())*0.5; md = bts::os::is_nan(md) ? u.bid_price() : md; md = bts::os::is_nan(md) ? u.ask_price() : md; md = bts::os::is_nan(md) ? u.settlement_price() : md; double long_pnl = std::abs(u.pos().day_long_pos()) * u.cash_value() * (md - u.pos().day_long_avg_price()), short_pnl = std::abs(u.pos().day_short_pos()) * u.cash_value() * (u.pos().day_short_avg_price() - md); pnl = long_pnl + short_pnl; break; } case HandleIndex::SPREAD: pnl = 0.0; break; case HandleIndex::UNRESOLVED: pnl = os::nan(); break; } return pnl; } double PositionServicePNLHelper::_get_cpp(uint32_t sec_key){ auto it = me->sec_cpp_map.find(sec_key); if(it == me->sec_cpp_map.end()){ // try to find it and put the value there std::string symbol = me->handle_index.get_symbol(SecurityKey(sec_key)); auto cppit = me->symbol_cpp_map.find(symbol); if(cppit == me->symbol_cpp_map.end()){ LOG(md,Warn,"Could not find cpp for key:{} symbol:{}", sec_key,symbol); return bts::os::nan(); } else{ double cpp = cppit->second; me->sec_cpp_map[sec_key]=cpp; return cpp; } } else return it->second; } PositionServicePNLHelper::PositionServicePNLHelper(md::MDClient& md_client) : me(new PImpl(md_client)){ } PositionServicePNLHelper::~PositionServicePNLHelper(){ } }}