diff --git a/src/server/sparkd/position/PositionServicePNLHelper.cpp b/src/server/sparkd/position/PositionServicePNLHelper.cpp index 8c87075fb..df9396c4d 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; + } + + 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 = 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; @@ -110,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:{}", @@ -160,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 1000; + 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) {