4. Main Command Line 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.

Select File -> New File or Project.

Select C++ from the Files and Classes section.

Select C++ class.

Name your class mainCommandLine.

Make sure the camel case convention is entered for the class name, header file and source file.

There is no need to enter a base class.

Select Next, to add the class to the algoTradingModel.pro project.

Select Finish.

You should now have two new files in your project corresponding to the new class name.

The qtcreator IDE fills out the bare minimum for a new C++ class. So naturally it doesn’t do anything yet. As stated previously, this class will serve as the top-level entry point for executing all the other necessary classes when run in command line mode.

Let’s add a simple printout to illustrate how this will work.

Add the following prototype for a run() method to your mainCommandLine.h header file.


#ifndef MAINCOMMANDLINE_H
#define MAINCOMMANDLINE_H


class mainCommandLine
{
public:
    mainCommandLine();
    int run();
};

#endif // MAINCOMMANDLINE_H


Then create the function definition in mainCommandLine.cpp.


#include <iostream>
#include "mainCommandLine.h"

// Constructor
mainCommandLine::mainCommandLine()
{

}

// Run method
int mainCommandLine::run()
{
    std::cout << "mainCommandLine::run()" << std::endl;
    return 0;
}

Note we also included <iostream> to be able to use std::cout. Qt has oi methods as well but for various reasons I still prefer std::cout and std::cerr.

Go back and modify main.cpp to create an instance of mainCommandLine and execute the run function. You will need to change the command line branch and include mainCommandLine.h.

Your main.cpp file should now look like this.


#include <QApplication>
#include <iostream>

#include "mainWindow.h"
#include "mainCommandLine.h"

// Return gui or command line version of the application
// ======================================================================
QCoreApplication* createApplication(int &argc, char *argv[])
{
    for (int i = 1; i < argc; ++i)
        if (!qstrcmp(argv[i], "-gui"))
        {
            // Return a Qt application which includes the GUI
            return new QApplication(argc, argv);
        }
    // Return a Qt "core" application which does not contain a gui
    return new QCoreApplication(argc, argv);
}

// Main, either runs as command line or launches a gui (from cmd line)
// ===================================================================
int main(int argc, char* argv[])
{
    QScopedPointer<QCoreApplication> app(createApplication(argc, argv));

    // Show main window and launch Qt event scheduler
    if (qobject_cast<QApplication *>(app.data()))
    {
        mainWindow mW;
        mW.show();
        return app->exec();
    }

    // else run in command line mode via mainCommandLine.run()
    else
    {
        mainCommandLine mCL;
        mCL.run();
    }
}

Build an run the application.

You should see the following output.


$ ./algoTradingModel
mainCommandLine::run()
$ 

With the basics out of the way we can now begin designing and coding up our algorithmic trading model. The first thing we need to do is think about what basic classes we’ll need to make all this work.

< previous | next >