Skip to content

Commit

Permalink
Print market positions with their events. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
krazkidd authored Jun 19, 2024
1 parent 93a0064 commit 7bf594b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
6 changes: 5 additions & 1 deletion include/api/Api.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef API_HPP
#define API_HPP

#include <map>
#include <string_view>
#include <vector>

Expand All @@ -27,7 +28,7 @@ class Api
static bool IsLoggedIn();

static std::vector<PortfolioPositionsResponse::EventPosition> GetEventPositions();
static std::vector<PortfolioPositionsResponse::MarketPosition> GetMarketPositions();
static std::vector<PortfolioPositionsResponse::MarketPosition> GetMarketPositions(std::string_view eventTicker = "");

private:
// auth
Expand All @@ -37,6 +38,9 @@ class Api
static inline PortfolioBalanceResponse balance;
static inline PortfolioPositionsResponse positions;

static inline std::map<std::string, PortfolioPositionsResponse::EventPosition> eventsMap;
static inline std::map<std::string, std::map<std::string, PortfolioPositionsResponse::MarketPosition>> marketsMap;

// core
template <typename TResponse>
static ApiResult<TResponse> HandleResponse(int responseCode, const std::stringstream &responseBody);
Expand Down
43 changes: 41 additions & 2 deletions src/api/Api.portfolio.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdexcept>
#include <string_view>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -38,6 +39,26 @@ PortfolioPositionsResponse Api::GetPositions()
if (std::holds_alternative<PortfolioPositionsResponse>(res))
{
positions = std::get<PortfolioPositionsResponse>(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
{
Expand All @@ -54,7 +75,25 @@ std::vector<PortfolioPositionsResponse::EventPosition> Api::GetEventPositions()
return positions.event_positions;
}

std::vector<PortfolioPositionsResponse::MarketPosition> Api::GetMarketPositions()
std::vector<PortfolioPositionsResponse::MarketPosition> 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<PortfolioPositionsResponse::MarketPosition> 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;
}
8 changes: 4 additions & 4 deletions src/ui/PortfolioPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7bf594b

Please sign in to comment.