Sensitivity of Bond Prices

Summary

This demo illustrates how the JMSL Library can be used to easily create a Java™ application that allows the user to specify certain inputs and immediately view an output chart displaying the relationship of factors in bond values. In this case, changes in interest rates affect the price of existing bonds. The window at the left is a list of US Treasury bonds, with different maturity dates and different nominal interest rates.

Usage

Select one or more bonds in the "Select Bonds" dialog to chart its prices versus possible current interest rates. Note that the maximum value of the y axis increases as required.

JMSL Library Math/Stat Classes

com.imsl.finance.Bond - The price() method of the Bond class is used in ChartPanel.java to evaluate the values for charting. The API for this method that returns a double is price(GregorianCalendar settlement, GregorianCalendar maturity, double rate, double yield, double redemption, int frequency, DayCountBasis basis). The settlement data is hardcoded as one week from the current date. The maturity data and the rate are read in from a data file, and the yield is just the x axis values. The other values are hardcoded as redemption=100.0d, frequency=Bond.SEMIANNUAL, and basis=DayCountBasis.BasisActualActual.

JMSL Library Charting Classes

The chart is defined in ChartPanel.java and is a standard line graph. The Data objects are created in the createLine() method. Of interest is how each curve, once computed, is stored in a Hashtable. Curves are added to the table as bonds are selected in the dialog box. However, they are not removed from the table when deselected; instead the paint attribute is set to false. When re-selected, the curve is not recomputed, just the paint attribute is set back to true.

Java Code

There is code in ChartPanel.java that reads the Comma Separated Value (csv) data file used in this example. The method readData() uses a java.io.LineNumberReader to read lines from the file. The lines are then parsed with a java.util.StringTokenizer.

The ChartPanel and the DialogSelect need to share information. This is accomplished by passing the DialogSelect object into the constructor of the ChartPanel object, as seen in MainFrame.java:

DialogSelect ds = new DialogSelect(this);
chartPanel = new ChartPanel(Ds);

And then in ChartPanel.java we see references to the passed DialogSelect:

public ChartPanel(DialogSelect Ds) {
    ...

    ds.setChartPanel(this);
    for (int iBond = 0; iBond < coupon.length; iBond++) {
        ds.addBond(iBond, maturity[iBond], coupon[iBond]);
    }
    ds.show();
}

Links to Source Code

MainFrame.java This is the main class for this demo and extends JFrame. It creates the DialogSelect and ChartPanel objects and positions their windows.
ChartPanel.java This class extends JPanelChart and does all of the calculations and charting.
DialogSelect.java This JDialog contains the list of Bonds to select..
TBondData.csv The data file is a csv file with two columns: the coupon value and the maturity date.

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.