cppadvanced120 minutes

Advanced Portfolio Management System in C++

Create a mini-project to design and implement a portfolio management system that tracks multiple investment assets, calculates real-time portfolio value, and supports transaction history and performance analysis.

Challenge prompt

Build a C++ portfolio management system that allows users to add multiple assets (stocks, bonds, ETFs), record buy/sell transactions, and calculate real-time portfolio metrics such as total value, return on investment (ROI), and asset allocation percentages. Your system should also handle transaction history with timestamps and provide functions to generate portfolio performance reports over a given period.

Guidance

  • Design appropriate classes to represent Assets, Transactions, and the Portfolio itself.
  • Implement methods for adding/removing assets and recording buy/sell transactions with timestamps.
  • Calculate portfolio metrics dynamically considering current prices and historical transactions.
  • Ensure your system can generate reports summarizing portfolio performance over specified time ranges.

Hints

  • Use a map or unordered_map keyed by asset symbol for efficient lookups.
  • Store transactions in a vector or list and consider sorting/filtering by date when generating reports.
  • Use appropriate data structures and member functions to maintain data encapsulation and modularity.

Starter code

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <ctime>

struct Transaction {
    enum Type { BUY, SELL } type;
    std::string assetSymbol;
    int quantity;
    double pricePerUnit;
    std::time_t timestamp;
};

class Asset {
public:
    Asset(const std::string& symbol, double currentPrice);
    std::string getSymbol() const;
    double getCurrentPrice() const;
    void setCurrentPrice(double price);
private:
    std::string symbol;
    double currentPrice;
};

class Portfolio {
public:
    void addAsset(const Asset& asset);
    void recordTransaction(const Transaction& transaction);
    double getTotalValue() const;
    double getROI() const;
    void printPerformanceReport(std::time_t startTime, std::time_t endTime) const;

private:
    std::unordered_map<std::string, Asset> assets;
    std::vector<Transaction> transactions;
};

Expected output

Expected outputs vary depending on implementation and usage but include correctly computed portfolio total value, ROI percentages, and formatted performance reports with asset allocations and summaries.

Core concepts

classes and objectsdata structures (maps, vectors)time handlingfinancial calculations

Challenge a Friend

Send this duel to someone else and see if they can solve it.