Financial Modeling and Bollinger Bands

The image frame below shows the simulation of some arbitrary (random) stock-price moving average and its Bollinger Bands over discrete time periods. The user is encouraged to run this small application and experiment with varying different parameter values, that is "Number of days" and "Type" (Alpha-level), where the change is reflected instantly from the plottings. You can plot random new generated data by clicking on the button named Generate data. The class that does this simulation is called Bollinger.java; it is named after its inventor, John Bollinger. Interested Java developers who are currently involve in projects developing financial application systems should read the references below:

There are two classes to run the Swing applications: Bollinger.java and BollingGraphics.java.The developer/user can modify the code and develop a JavaBean using Bollinger.java, the WebStart application , applet, or servlet . To get data from a stock market repository, read the data from an input stream, convert each one into double (Java data-type) and store this array as a Matrix object. This object is the input parameter to the calculate method of the Bollinger class. Write a thread to update the data from the stock market repository, say, every 2 minutes. The first element of the old data is chopped-off and a new (recent) data is added to the end of the Matrix object, thus averaging is moving along with every new data that comes in. For example:

Asset = [9,7,3,14,6,7,9,5,7,11]
New Data = 10
Lop-off the first element = 9 and add 10 to the end
Asset = [7,3,14,6,7,9,5,7,11,10]

Note that the Matrix object has to be reconstructed every time new data is received, if the data being read involves a large number of elements, then consider using a Link list so that data can be added and removed in a FIFO (First-In , First-Out) manner.

Richard Baldwin has excellent tutorials right here at Gamelan in data structures: Data Structures in Java: Part 1, Getting Started The Bollinger class is written to read only one-dimensional data, the user should modify it to read two-dimensional data, because the Matrix class can handle this. This is useful when tracking stock prices of, say, the top 1,000 companies every 2 minutes, that is 20,160 discrete data points a month. The amount of data to be read is 1,000 x 20,160 = 20,160,000. This is a lot of data to be processed every 2 minutes, that is why it is better to use Link List.

Statistical Formulas Related to Moving Average

Average (Mean) :

Standard Deviation 1 :

Standard Deviation 2 :

Moving Average :

Exponential Smoothing :

There is a difference between Standard Deviation 1 and Standard Deviation 2, as the former is normalized to n, while the later is n-1. From the Bollinger.java class definition, the red highlighted line is a call that calculates Standard Deviation 1,
double standardDeviation = JDatafun.std(asset_submatrix,false,JDatafun.ROW_DIMENSION).get(0,0);
whereas Standard Deviation 2 is computed if a "true" boolean value is passed to the std method:
double standardDeviation = JDatafun.std(asset_submatrix,true,JDatafun.ROW_DIMENSION).get(0,0);
when the number of data-points is small; Standard Deviation 2 is used while Standard Deviation 1 is applicable to large data-point samples. In the case of the Bollinger class, use Standard Deviation 1 for short-term and Standard Deviation 2 for long-term running moving averages. Here is an explanation from Statistics literature:

Suppose we have some draws made at random with replacement from a box whose std (standard deviation) is unknown. If we knew the average of the
box, the r.m.s (root-mean-square) difference between the sample numbers and the average of the box could be used to estimate the std of the box.
However, we usually do not know the average of the box and must estimate that too, using the average of the draws. Now there is a little problem.
The average of the draws follows the draws around: deviations from the average of the draws tend to be smaller than deviations from the
average of the box. Standard Deviation 2 corrects this problem.

A Moving Average estimator combines the advantages of the last value and average estimators in that it uses only recent history and multiple observations. This procedure places much weight on x t-n+1 as on x t. Exponential Smoothing places more weight on the most recent observation than on older observations that may be less representative of current conditions.

Application

There is an excellent free Java graphic toolkit called the Scientific Graphics Toolkit, which is an advanced tool if the user needs better visualization of the data. This toolkit has such functionality as time-axis (x-axis labels with days, weeks, months), which
is applicable to data such as stock prices. There are also a number of commercial Java2D graphics tools for visualization available (just do a search on the Internet).

Advanced Use

Just looking straight into a Bollinger bands graph and moving average does not tell us all about the mechanics of stock price variations, because this process is regarded in statistics as stochastic, a model process that evolves over time in a probabilistic manner. Take the question: "What was the rate of stock price change for Sun Microsystems yesterday at 4:07 PM?" This is where calculus comes to our aid. From elementary calculus, Rate is described as the ratio of "change in y over change in x". Rate is also the first derivative of a function y(x) which is y'(x). How to do this? Gather data, say, between 3:00 PM to 5:00 PM from yesterday and then run a polynomial fitting, which gives the coefficients of a specific order polynomial (Check out Polynomial Interpolation here on this Web site: Data Interpolation and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 1 ). In the JDatafun class from the Jamlab package, you will find a method for first derivatives, sDiff, that can be used to derive the polynomial. Use the Polyval class to evaluate the value of y'(x), which is the Rate of the interpolated moving average exactly at 4:07 PM.

This sample application is one of the techniques used for analysis, but there are more advanced methods that are used by economist for modeling. These techniques involve differential equations, differential calculus, probability, bayes models, and advanced statistics, etc.

About the Author

Sione Palu has been a Java developer for over three years and has developed software in Publishing Systems, Imaging, and Web Applications. Currently developing (in Java Swing) his own software application in Symbolic and Visualization Mathematics for high school students. Palu graduated from the University of Auckland, New Zealand, double-majoring in mathematics and computer science. He has a personal interest in applying Java and mathematics in the fields of mathematical modeling and simulations, expert systems, neural and soft computation, wavelets, digital signal processing, and control systems.

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.