A very common activity in financial programming is the generation of price-related data that needs to be visualized by traders or other business stakeholders. Most of the time, the data is expected to be plotted in the form of a chart for easy visualization. Visualization strategies for financial data range from simple line charts for daily prices to complex graphical output using candles, superposed studies, and other less conventional notation.
In this chapter, you will see a number of coding examples for creating and displaying charts based on prices and related quantitative data analysis. You will learn how to perform such tasks using a few different techniques, including external software such as Gnuplot as well as graphical C++ libraries such as Qt. Both techniques may be useful in different situations, as they have their own advantages and disadvantages.
How to create a class that provides a plotting interface
How to use external plotting applications such as Gnuplot
How to convert your data to a format that can be understood by external programs
How to plot csv (comma-separated values) files on UNIX and Windows
How to generate commands to control the open source Gnuplot application
How to create a plot using an open source and multiplatform graphical user interface (GUI) library
How to use Qt to generate a basic plotting window
Plotting with Gnuplot
Create a price chart using Gnuplot.
Solution
Gnuplot is a very popular software package used to create charts based on mathematical functions and data points. You can use Gnuplot in a stand-alone fashion or as an embedded viewer for graphs created by other applications. In this section, you will learn how to generate files that can be easily visualized using Gnuplot.
In this plot, you supplied the mathematical function defined by sin(x) , and Gnuplot is responsible for creating a plot of the values, where the default range is from -10 to 10. You can easily tweak the parameters used to determine the range, as well as other attributes of the plot such as the title, the legend, and the units used in both axes.
Another way to use Gnuplot is to directly plot numeric data, instead of a mathematical function. This is possible by referencing the name of the files that should be imported by the plot command. Most data imported in this way is in the csv (comma-separated values) format, although Gnuplot doesn’t mandate that the number be in csv—any file with numeric data organized as columns will do.
When running the previous commands, make sure that you’re in the same directory in which you have saved the data file (IBM.csv). Another way to do this is to use the full path for the file, for example, "c:\testdata\IBM.csv" (escaped path separators are needed in the Windows platform).
The first command is used to tell Gnuplot that the data in the x axis is time-oriented. The second command defines the separator used in the file. The third command describes the date format stored in the csv file. Finally, the last line tells Gnuplot to plot the contents of file IBM.csv , using columns 1 and 7 (column 1 contains dates, while column 7 has adjusted closing prices), and with the title of the time series defined by the headers for each column of the csv file.
The plot in Figure 6-3 is just a sample of what Gnuplot can do. There are literally hundreds of parameters that can be tweaked using the set command . Among these options, you can find three-dimensional plots, different colors, and line styles, among others.
To solve the problem presented in this section, you need to create a class that receives some data in the form of vectors of doubles or strings and produces output data suitable for consumption by Gnuplot. I have created such a class, which is called GnuplotPlotter and is responsible for the generation of the files needed by Gnuplot.
To access the results of the class, two member functions are available. The csvWrite member function will write the data stored in GnuplotPlotter to the file specified in the constructor, using the csv format. The second member function is generateCmds , which allows one to create a command file with the necessary instructions to Gnuplot. This way, you don’t need to worry about the exact syntax for plotting the file. The commands are stored in a filename specified by the parameter cmdFileName .
Complete Code
GnuplotPlotter.h and GnuplotPlotter.cpp
Running the Code
The plot will be displayed in a separate window, as shown in Figure 6-4.
Plotting Data from a GUI
Create an application that can plot data using the GUI.
Solution
Although it is great to have the ability to create charts with external packages such as Gnuplot, sometimes it is necessary to have a larger degree of control over the output generated by plots. If you cannot find a way to use one of the parameters in Gnuplot to get the desired results, it becomes necessary to implement a plotting solution that runs in C++. This section shows how to achieve this.
There are many graphical libraries available for C++ developers, and the final decision depends mostly on your target environment. However, in this section, I use the Qt library to implement the desired solution.
Qt is probably the easiest to use graphical programming package around. You will see that with just a dozen lines, we are able to create a complete application. Moreover, Qt is available for all major operating systems, so that your application can be easily ported to other targets as necessary.
The class used is called QtPlotter , and it receives data using the setData member function, just as we did with the GnuplotPlotter. The main part of the implementation, however, is performed in the PlotWindow class , which is derived from QMainWindow , one of the key classes in the Qt framework. The PlotWindow class is responsible for managing the window and, most important, painting the plot when necessary.
In the next step, the function paintEvent draws the tick markers along the axis. Finally, the code paints lines between the points given as input to the plot.
This code does most of what is necessary to create a Qt application and display a window on the screen. The window created is the PlotWindow class that we discussed previously, so that the plot is displayed as desired. The QApplication object is part of the Qt framework. It manages the workflow of a graphical application, including menus and windows. When creating a QApplication, we are able to determine the application name with the setApplicationName member function. Finally, we resize and show the plot window and call the exec member function to start the window display loop.
Complete Code
QtPlotter.h and QtPlotter.cpp
Running the Code
To use the QtPlotter class, you need to have the Qt4 library installed in your system. The installation process requires you to visit the developer website (www.qt.io/developers/), download, and run the installer application. After the installation is complete, the libraries will be copied to a user-defined folder.
Conclusion
In this chapter, you learned a few techniques to plot financial data using C++. Visualization is one of the factors that shouldn’t be overlooked in the creation of efficient investment strategies. The better your visualization facilities, the easier it is to spot trends and opportunities in the markets. While there are many free and commercial alternatives to display stock charts, we frequently need to present data in a more flexible way.
I started the chapter with a recipe for creating numerical plots using Gnuplot. Gnuplot is a free, widely available package for data visualization, which runs in most operating systems, including Windows, Mac OS X, and UNIX. You have seen how to create a class that encapsulates the information necessary to create graphs in Gnuplot.
The next section gave you another approach to create your own financial plots, using a C++ graphical library called Qt. You can employ this type of code in multiple platforms, taking advantage of the high portability of the underlying framework. The QtPlotter class presented here exposes an interface that your program can use to display a single plot based on values for the x and y axis.
Many of the algorithms in finance depend on the solution of systems of equations, which are based on linear algebra concepts. For the developer on the financial industry, it is very useful to have a basic knowledge of linear algebra and its software implementations. These concepts can be viewed as the building blocks used by financial engineers and can be easily accessed in C++. In the next chapter, you will see a few programming examples that make use of linear algebra concepts as part of financial applications.