13. Portfolio Class

Disclaimer

Before reading this tutorial you, the reader, agree to the following. This tutorial and the code it contains are designed to be informational and educational tools only. They do not constitute investment advice. The author, Dave Topper, strongly recommends that you seek the advice of a financial services professional before making any type of investment. This model is provided as a rough approximation of future financial performance. The results presented are hypothetical and will likely not reflect the actual growth of your own investments. The author, Dave Topper, is not responsible for any human or mechanical errors or omissions. The author, Dave Topper, is not responsible for the consequences of any decisions or actions taken in reliance upon or as a direct or indirect result of the information provided by these tools.

Creating a separate portfolio class will help us reduce possible trading errors as well as contain various statistics and metrics related to our model. Choosing metrics is a very important part of model optimization. We’ll just use a few in this class to illustrate the point.

Add a new stockPortfolio class to your project.

Be sure to enforce the camel case convention for the source and header file names.

This is the header file stockPortfolio.h.


#ifndef STOCKPORTFOLIO_H
#define STOCKPORTFOLIO_H

// Portfolio class to help manage a stock portfolio consisting of shares of one stock at a time

class stockPortfolio
{
public:
    stockPortfolio(double);

    // Buy / Sell
    void doBuy(double,unsigned int);
    void doSell(double);

    // Update with most recent underlying price
    void update(double inPrice);

    // Basic Values
    double getCashValue();
    unsigned getNumShares();
    double getInvestedValue();
    double getTotalValue();
    double getStartValue();
    unsigned getNumSellTrades();
    unsigned getNumTradesPos();
    unsigned getNumTradesNeg();
    double getMinValue();
    double getMaxValue();

private:
   // Main elements
    double startAmount_;
    double cashAmount_;
    double investedValue_;
    double totalValue_;
    double minValue_;
    double maxValue_;
    unsigned numShares_;
    unsigned numSells_;
    unsigned numTradesPos_;
    unsigned numTradesNeg_;
    double buyPrice_;
    double sellPrice_;
};

#endif // STOCKPORTFOLIO_H

This is the source file stockPortfolio.cpp.


#include "stockPortfolio.h"

// Portfolio class to help manage a stock portfolio

// Constructor
stockPortfolio::stockPortfolio(double start)
{
    cashAmount_ = start;
    startAmount_ = start;
    numShares_ = 0;
    numSells_ = 0;
    numTradesPos_ = 0;
    numTradesNeg_ = 0;
    minValue_ = start;
    maxValue_ = start;
}

// Accessors
double stockPortfolio::getCashValue()
{
    return cashAmount_;
}

// Get the number of shares held in the portfolio
unsigned stockPortfolio::getNumShares()
{
    return numShares_;
}

// Get the invested value
double stockPortfolio::getInvestedValue()
{
    return investedValue_;
}

// Get the total value
double stockPortfolio::getTotalValue()
{
    return totalValue_;
}

// Get portfolio start value
double stockPortfolio::getStartValue()
{
    return startAmount_;
}

// Get minimum total value
double stockPortfolio::getMinValue()
{
    return minValue_;
}

// Get maximum total value
double stockPortfolio::getMaxValue()
{
    return maxValue_;
}

// Get total number of trades (buys + sells)
unsigned stockPortfolio::getNumSellTrades()
{
    return numSells_;
}

// Get the number of profitable trades
unsigned stockPortfolio::getNumTradesPos()
{
    return numTradesPos_;
}

// Get the number of trades that incurred a loss
unsigned stockPortfolio::getNumTradesNeg()
{
    return numTradesNeg_;
}

// Update portfolio value and some metrics
void stockPortfolio::update(double inPrice)
{
    investedValue_ = numShares_*inPrice;
    totalValue_ = cashAmount_ + investedValue_;
    if (totalValue_ > maxValue_)
    {
        maxValue_ = totalValue_;
    }
    else if (totalValue_ < minValue_)
    {
        minValue_ = totalValue_;
    }

}

// BUY inNumShares shares at inBuyPrice
void stockPortfolio::doBuy(double inBuyPrice, unsigned inNumShares)
{
    double amountToSpend = inBuyPrice*inNumShares;
    double currentCash = cashAmount_;

    // Adjust cash
    currentCash -= amountToSpend;
    cashAmount_ = currentCash;
    // Num shares
    unsigned currentShares = numShares_;
    currentShares += inNumShares;
    numShares_ = currentShares;

    buyPrice_ = inBuyPrice;
}

// Sell all currently owned shares at inSalePrice
void stockPortfolio::doSell(double inSalePrice)
{
    double currentCash = cashAmount_;
    double totalSale = inSalePrice * numShares_;
    double newPortVal = currentCash+totalSale;
    cashAmount_ = newPortVal;
    numShares_ = 0;

    numSells_++;
    sellPrice_ = inSalePrice;
    if (sellPrice_ > buyPrice_)
    {
        numTradesPos_++;
    }
    else if (sellPrice_ < buyPrice_)
    {
        numTradesNeg_++;
    }

}

Build your project and check for any errors. In the next section we will set up some rules for our tradingModel class and use the portfolio class above.

< previous | next >