From a014dcc98401d93b7e0d92fc142d508744ee8278 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 21 Jun 2019 17:58:08 -0400 Subject: [PATCH 1/2] abstracted _get_handle in PositionServicePNLHelper::HandleIndex --- .../sparkd/position/PositionServicePNLHelper.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp index 8c87075fb..26de78fef 100644 --- a/src/server/sparkd/position/PositionServicePNLHelper.cpp +++ b/src/server/sparkd/position/PositionServicePNLHelper.cpp @@ -42,19 +42,22 @@ public: on_consumer(_client.current_consumer()); } ~HandleIndex() { _client.on_consumer().unsubscribe(_sub); } - const std::string& symbol (SecurityKey key){ + + 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; + } + + const std::string& symbol (SecurityKey key){ + 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 = index.lock([id=key](const auto& index) { - auto iter = index.find(id); - return iter != index.end() ? iter->second : MDConsumerHandle {}; - }); + auto handle = _get_handle(key); bid = bts::os::nan(); ask = bid; last = bid; @@ -184,7 +187,7 @@ double PositionServicePNLHelper::_get_cpp(uint32_t sec_key){ // me->sec_cpp_map[sec_key]=cpp; // return cpp; // } - return 1000; + return 5000; } else return it->second; From a1ead3d1c1711262936a9bf257f096696c8233d4 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Sat, 22 Jun 2019 14:24:15 -0400 Subject: [PATCH 2/2] cpp routed correctly now --- .../position/PositionServicePNLHelper.cpp | 41 ++++++++++--------- .../sparkd/position/PositionServiceServer.cpp | 2 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp index 26de78fef..df9396c4d 100644 --- a/src/server/sparkd/position/PositionServicePNLHelper.cpp +++ b/src/server/sparkd/position/PositionServicePNLHelper.cpp @@ -51,7 +51,7 @@ public: return handle; } - const std::string& symbol (SecurityKey key){ + std::string get_symbol (SecurityKey key) const{ auto handle = _get_handle(key); return handle.get_usersymbol().symbol(); } @@ -113,6 +113,7 @@ struct PositionServicePNLHelper::PImpl { 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:{}", @@ -163,31 +164,33 @@ void PositionServicePNLHelper::compute_pnl( double PositionServicePNLHelper::_compute_day_pnl ( const bts::api::position::PNLUnit& u){ // long - double mid = (u.bid_price() + u.ask_price())*0.5, - long_pnl = std::abs(u.pos().day_long_pos()) * u.cash_value() * - (mid - u.pos().day_long_avg_price()), + 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() - mid); + (u.pos().day_short_avg_price() - md); return long_pnl + short_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()){ - // TODO // try to find it and put the value there - // const auto& symbol = me->handle_index.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; - // } - return 5000; + 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; diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 50025e4c0..fc0ff04bb 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -54,7 +54,7 @@ struct PositionServiceServer::PImpl { state(), id(0), pnl_helper(md_client), - poll_period_ms(1000), + poll_period_ms(300), manual_fills_store(std::make_unique ("manual_fills")), manual_fills_publisher(manual_fills_publisher) {