10. Load The Price Data

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 up Step 3. from the execution sequence for the trading model class.

“3. Initialize the model with the input params. Note: this will also load data from the specified input file.”

Change the tradingModel.h header file as follows:


#include "priceDataSet.h"
...
private:
    std::unique_ptr<priceDataSet> priceData_;

Change the loadPriceData method in tradingModel.cpp as follows:


// Read the data file and load it into a price data object
bool tradingModel::loadPriceData()
{
    bool fileReadSuccess = false;

    // Get the data filename
    QString priceDataFilename = modelParams_->getDataFilename();

    // Extra check to make sure we've received a file name
    if (priceDataFilename.isEmpty())
        return false;

    // Create new price data object
    priceData_ = std::unique_ptr<priceDataSet>(new priceDataSet());

    // Set up a QFile for reading
    QFile file(priceDataFilename);
    // Check if file exists
    if (file.exists())
    {
        // Check if the file is readable
        if ( !file.open(QFile::ReadOnly | QFile::Text) )
        {
            std::cerr << "Problem opening file " << priceDataFilename.toStdString() << std::endl;
            return false;
        }
        fileReadSuccess = priceData_->Read(&file);
    }
    return fileReadSuccess;
}

Change the run() method in tradingModel.cpp to print out all the OHLC data.


// Run the model
void tradingModel::run()
{
     priceData_->test();
}

Lastly, add an include for the trading model class and change the run() method in mainCommandLine.cpp as follows. Note this also sets up the top-level execution sequence for the tradingModel class.


#include "tradingModel.h"
...
// Run method
void mainCommandLine::run(QStringList & inputArgs)
{
    //1. Create model params from command line args
    std::unique_ptr<inputArgumentParser> inputArgParser = std::unique_ptr<inputArgumentParser>(new inputArgumentParser());

    //2. Create instance of trading model
    std::unique_ptr<tradingModel> crossOverModel = std::unique_ptr<tradingModel>(new tradingModel());

    //3. Initialize the model
    std::shared_ptr<modelParameters> modelParams = inputArgParser->createModelParams(inputArgs);
    crossOverModel->init(modelParams);

    //4. Run the model
    crossOverModel->run();

    //5. Get statistics
    crossOverModel->printStats();
}


Build and run your code. You should see the following output (last 10 lines).


$./algoTradingModel -df IBM.csv -mAvgL1 20 -mAvgL2 50
...
20181217,119.07,119.78,115.07,116.1
20181218,116.9,118.23,116.02,116.65
20181219,117.15,120.27,115.97,116.43
20181220,115.7,116.45,111.7,113.02
20181221,112.5,115.28,110.44,110.94
20181224,109.9,111,107.5,107.57
20181226,108,111.39,105.94,111.39
20181227,109.99,113.78,109.47,113.78
20181228,114.22,114.8,112.5,113.03
20181231,113.33,114.35,112.42,113.67

In the next couple of sections we will start building up the actual trading logic inside the tradingModel class. But there are still a few things we need to implement first: a moving average class and a portfolio class. In the next section we will look at the former.

< previous | next >