Stock Price Charting

Summary

This demo illustrates selected charts that can be created with the JMSL Library. The history of several stock prices are read in from a database and displayed in the Ticker window. The Chart Options window displays several pull-down menus to affect the appearance of the main chart. In this example, the stock price and stock volume charts share the same x-axis, but have different y-axes, illustrating the flexibility of the classes to control chart layout.

Usage

Select a stock symbol from the Ticker window or scroll through the list using the arrow buttons at the top. Using the Chart Options window, you can change the charted Period (Day, Week, Month), the Style (Close, Candlestick, High-Low-Close), how the Volume chart is plotted (None, Bars, Line, Area), the Y axis scaling (Linear, Log), and the data interval (Week, 1 Month, 3 Months, Year). You may also select a computed time series to overlay on the chart from None, Parabolic SAR, Moving Average, Kalman Filter.

JMSL Library Math/Stat Classes

com.imsl.math.Sfun - the log10(double x) method is used to compute the common (base 10) logarithm in the init() method of ChartStock.java.

com.imsl.stat.KalmanFilter - this class is used to compute the Kalman Filter overlay. Note that the filter object must be constructed twice - once to update it with the data values and once to perform the filter operation.

JMSL Library Charting Classes

A variety of charting classes are used in this application. The main chart uses com.imsl.chart.Candlestick or com.imsl.chart.HighLowClose while the Volume chart uses com.imsl.chart.Bar. When just line charts are requested, a new com.imsl.chart.Data object is created and setDataType(Data.DATA_TYPE_LINE) is used. For the filled area volume chart, setFillColor("blue") is used instead of setDataType().

When an overlay is selected, a Legend is drawn by setting chart.getLegend().setPaint(true). The setViewport() method is called to position the legend in the upper right corner, so as to not impinge on the main chart. When the overlay is set to "None", we call chart.getLegend().setPaint(false).

Java Code

The data for this demo are stored in the database sp500hst.jar. Consult the Database.java code for references on accessing such a database.

The Moving Average and Parabolic SAR overlays are not computed using JMSL classes. Instead they are written in Java. Here is the code for computing a 20 point moving average.

private double[] computeMA20(Database.Series series) {

    final int period = 20;
    int num = 0;
    double sum = 0.0;
    double[] m = new double[series.close.length];
    for (int i=0; i<series.close.length; i++){
        sum += series.close[i];
        if (i < period) {
            num++; m[i] = sum/num;
        }
else {
            sum -= series.close[i-period];
            m
[i] = sum/period;
        }
    }

    return m;
}

Compiling and Executing the Code

To execute this demo, sp500hst.jar must be in your CLASSPATH. See the How To for information on setting the CLASSPATH.

Links to Source Code

WallStreet.java This is the main class for this demo. It creates and positions all of the windows and...
ChartStock.java This class extends JFrameChart and does all of the charting. The overlay time series are also computed here.
Database.java This class reads the ticker symbols and the time series from the database.
Model.java The Model object keeps track of the selections in the Options dialog.
Options.java This JDialog holds all the JComboBox objects used to set the display options.
ReadStockData.java This class extends com.imsl.io.FlatFile and is used by Database.java.
SelectTicker.java This JDialog holds all the stock symbols.

Running This Demo

Two alternatives are available to run this demo:

1) Use the source code in your development environment as any other Java code. More information is available in the How To.

2) An executable jar file containing all of the demos referenced in this guide is included in the jmsl/lib directory. On Windows, you may double-click the file to run it if files with a ".jar" extension are properly registered with javaw.exe. Alternatively, for both Windows and UNIX environments, the jar file may be executed from the command line using java -jar gallery.jar.

As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.