From 7bf594b4c68f6d90e321e6f5b53a22e09dc09b2d Mon Sep 17 00:00:00 2001 From: Mark Ross Date: Wed, 19 Jun 2024 16:12:50 -0700 Subject: [PATCH] Print market positions with their events. (#35) --- include/api/Api.hpp | 6 +++++- src/api/Api.portfolio.cpp | 43 +++++++++++++++++++++++++++++++++++++-- src/ui/PortfolioPanel.cpp | 8 ++++---- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/include/api/Api.hpp b/include/api/Api.hpp index 0a13f1c..2678324 100644 --- a/include/api/Api.hpp +++ b/include/api/Api.hpp @@ -1,6 +1,7 @@ #ifndef API_HPP #define API_HPP +#include #include #include @@ -27,7 +28,7 @@ class Api static bool IsLoggedIn(); static std::vector GetEventPositions(); - static std::vector GetMarketPositions(); + static std::vector GetMarketPositions(std::string_view eventTicker = ""); private: // auth @@ -37,6 +38,9 @@ class Api static inline PortfolioBalanceResponse balance; static inline PortfolioPositionsResponse positions; + static inline std::map eventsMap; + static inline std::map> marketsMap; + // core template static ApiResult HandleResponse(int responseCode, const std::stringstream &responseBody); diff --git a/src/api/Api.portfolio.cpp b/src/api/Api.portfolio.cpp index d37c533..6268b06 100644 --- a/src/api/Api.portfolio.cpp +++ b/src/api/Api.portfolio.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -38,6 +39,26 @@ PortfolioPositionsResponse Api::GetPositions() if (std::holds_alternative(res)) { positions = std::get(res); + + eventsMap.clear(); + marketsMap.clear(); + + for (const auto &event : positions.event_positions) + { + eventsMap[event.event_ticker] = event; + } + + for (const auto &market : positions.market_positions) + { + std::string eventTicker = market.ticker.substr(0, market.ticker.find("-")); + + if (marketsMap.find(eventTicker) == marketsMap.end()) + { + marketsMap[eventTicker] = {}; + } + + marketsMap[eventTicker][market.ticker] = market; + } } else { @@ -54,7 +75,25 @@ std::vector Api::GetEventPositions() return positions.event_positions; } -std::vector Api::GetMarketPositions() +std::vector Api::GetMarketPositions(std::string_view eventTicker) { - return positions.market_positions; + if (eventTicker.empty()) + { + return positions.market_positions; + } + + //TODO we should probably check if the eventTicker is in the map + const auto map = marketsMap[std::string{eventTicker}]; + + //TODO creating a vector is not very efficient--can we just return an iterator (using a lambda)? + std::vector vec; + vec.reserve(map.size()); + + for (const auto &pair : map) + { + //TODO this is dangerous because we are not making a copy + vec.push_back(pair.second); + } + + return vec; } diff --git a/src/ui/PortfolioPanel.cpp b/src/ui/PortfolioPanel.cpp index 840777a..c74a2eb 100644 --- a/src/ui/PortfolioPanel.cpp +++ b/src/ui/PortfolioPanel.cpp @@ -55,11 +55,11 @@ void PortfolioPanel::UpdateStuff() for (PortfolioPositionsResponse::EventPosition event : Api::GetEventPositions()) { boxSizer->Add(new EventPositionPanel(pnlPositions, wxID_ANY, &event), flagsPnl); - } - for (PortfolioPositionsResponse::MarketPosition market : Api::GetMarketPositions()) - { - boxSizer->Add(new MarketPositionPanel(pnlPositions, wxID_ANY, &market), flagsPnl); + for (PortfolioPositionsResponse::MarketPosition market : Api::GetMarketPositions(event.event_ticker)) + { + boxSizer->Add(new MarketPositionPanel(pnlPositions, wxID_ANY, &market), flagsPnl); + } } pnlPositions->SetSizerAndFit(boxSizer);