12. Trading Utilities 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.

In this section we will build a simple static class that implements a rounding function to round off doubles to two decimal places. It’s likely you may want to build some other helper functions so we’ll use a pretty generic name for the class.

Add a new tradingUtils class to your project.

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

This is the header file tradingUtils.h.


#ifndef TRADINGUTILS_H
#define TRADINGUTILS_H

// Various trading utilities
class tradingUtils
{
public:
    // Static method to round doubles to 2 decimal places
    static double fix2D(double inVal);
private:
    tradingUtils() {}
};

#endif // TRADINGUTILS_H

This is the source file tradingUtils.cpp.


#include "tradingUtils.h"
#include <math.h>

// Static class method to round doubles to 2 decimal places

double tradingUtils::fix2D(double inVal)
{
    double dVal = inVal * 100.0;
    long iVal = static_cast<long>(rint(dVal));
    double outVal = static_cast<double>(iVal)/100.0;
    return outVal;
}

Add the header tradingUtils.h and modify the run() method in tradingModel.cpp to use the new static method in the data printout as follows.


#include "tradingUtils.h"
...
// Run the model
void tradingModel::run()
{

    // Set up our moving averages
    std::unique_ptr<simpleMovingAverage> mAvg1 =
std::unique_ptr<simpleMovingAverage>(new simpleMovingAverage(movingAvgLen1_));
    std::unique_ptr<simpleMovingAverage> mAvg2 =
std::unique_ptr<simpleMovingAverage>(new simpleMovingAverage(movingAvgLen2_));

    double open,high,low,close;
    unsigned today;

    // MAIN TRADING ITERATOR
    for(unsigned long todayIndex = 0; todayIndex < priceData_->getNumRows(); todayIndex++)
    {
        // Grab price data into local variables
        today = priceData_->getDate(todayIndex);
        open = priceData_->getOpen(todayIndex);
        high = priceData_->getHigh(todayIndex);
        low = priceData_->getLow(todayIndex);
        close = priceData_->getClose(todayIndex);
        mAvg1->addDataPoint(close);
        mAvg2->addDataPoint(close);

        std::cout << today << ","
                  << open << ","
                  << high << ","
                  << low << ","
                  << close << ","
                  << tradingUtils::fix2D(mAvg1->getCurrentValue()) << ","
                  << tradingUtils::fix2D(mAvg2->getCurrentValue()) << std::endl;

    }
}

Build and run the program. You should now see the following output (last 10 lines) with the moving average values rounded to two decimal places.


20181217,119.07,119.78,115.07,116.1,120.66,125.63
20181218,116.9,118.23,116.02,116.65,120.42,124.98
20181219,117.15,120.27,115.97,116.43,120.22,124.34
20181220,115.7,116.45,111.7,113.02,120.01,123.66
20181221,112.5,115.28,110.44,110.94,119.63,123.02
20181224,109.9,111,107.5,107.57,119.15,122.39
20181226,108,111.39,105.94,111.39,118.74,121.8
20181227,109.99,113.78,109.47,113.78,118.43,121.26
20181228,114.22,114.8,112.5,113.03,117.93,120.61
20181231,113.33,114.35,112.42,113.67,117.54,120.21

In the next section we’ll build our portfolio class. Then we can put everything together and start testing our model.

< previous | next >