STOCK MARKET PREDICTION USING NEURAL NETWORKS

by Dr. Valentin Steinhauer

Disclaimer: No liability assumed for accuracy , completeness of the methods and other information in this tutorial.

Short description

 

   Here are considered two complementary directions: one - time series prediction, the second - Bollinger Bands method. Why are they selected? In the quiet time the harmonicas laws prevail and at this point are good a time series prediction. This method (from different sources) allows a prediction up and down in the stock market values with about  52%-53% probability, 50% -50% is, don’t forget, random. Second direction (Bollinger Bands) is from completely different area: the stock market nervousness. When prices fall or grow too much compared to earlier times, then there is often a turning point. Statistics of positive predictions in this case are better. But the cases do not occur as often, and it will be required waiting.

Time series prediction in stock market

   Time series prediction plays a big role in economics. The stock market courses, as well as the consumption of energy can be predicted to be able to make decisions. This tutorial shows one possible approach how neural networks can be used for this kind of prediction. It extends the Neuroph tutorial called "Time Series Prediction", that gives a good theoretical base for prediction.

   Used for our purposes, the time sequence is observing variable. Variable observed at discrete time intervals. The analysis of time series includes a description of the process or phenomenon, which generates a sequence. To predict the time series, it is necessary to present the behavior of the process in the form of a mathematical model that can be extended in the future. To do this, the model is a good representation of observations in any local segment of time close to the present. Usually there is no need to have a model that would represent a very old observations, as they probably will not characterize the moment. Also there is no need to submit observations in the distant future, je after a time interval that is greater than the horizon. Once the correct model will be created to handle the temporal sequence, we can develop appropriate means of prediction.

   To show how it works, we trained the network with the DAX (German stock index) data – for a month (03.2009: from 02th to 30) - to predict the value at 31.03.2009. As a strategy we take the sequences from 4 days to predict each 5th day. In the training set 5th day is the supervised value. The data DAX can be downloaded from the following URL (one of the possibilities):  http://download.finance.yahoo.com/d/quotes.csv?s=^GDAXI&f=sl1d1t1c1ohgv&e=.cs

   TrainingSetGetter is available for download as a part of NetBeans project, however it is only the hard coded example data with normalization . Also the first step is the normalization of the training data in area (0-1). The following formula offers it as follows:

      Norm.value = 0.8 (value – v1) / (v2 – v1) + 0.1,

Here 0.8 and 0.1 are the values to correct data set from limits 0 and 1, v2 is the maximum value * expander and v1 – minimum value/expander. The normalization plays important role in preparation of data for network training. This “expander” coefficient is entered for compression or stretching at a normalization. In each case of a prediction it is desirable to select this coefficient from control points.

   Next, the network topology is defined: what type of network, how many layers and how many neurons per layer are used. Actually, there is no rule for this, and usually it is determined experimentaly. However the common type of network used for prediction is a multi layer perceptron. A recommendation is to have 2n+1 nodes for hidden-layer, where n is the number of the input nodes. The output layer has only one node in this case. The good results were obtained with the following topology and parameter set: maxIteration=10000, maxerror=0.0001 and the training set is organized  as follows:

public DataSet getTrainingSet(int n, int m) {

        trainingSet = new DataSet(n, m);

……………………………………….

       trainingSet.addRow(in, out);

……………………………………….

 

and corresponding network is:

 

        int maxIterations = 10000;

        NeuralNetwork neuralNet = new MultiLayerPerceptron(4, 9, 1);

        ((LMS) neuralNet.getLearningRule()).setMaxError(0.0001);        

        ((LMS) neuralNet.getLearningRule()).setMaxIterations(maxIterations);       

        neuralNet.learn(trainingSet); 

 

   At this point, we are ready to train and test the network. For testing we'll use prepared data set in which the DAX data are given from the 27,28,29 and 30.03.09 to predict the value at 31.03.09

      neuralNet.setInput(test);

      neuralNet.calculate();

      double[] networkOutput = neuralNet.getOutput();

      double predictedNormalized = networkOutput[0] ;

 

   Since the network is initialised with random weight values, the test results will differ from a calculation to calculation. After five tests it came out with the following prediction - results for 03.31.2009: 4084.61; 4081.28; 4073.08; 4075.22; 4087.42.  The duration time was 3 sec ( 2 CPU, 3.3Ghz, 2GB Ram, WinXP).
   That is so called a committee - a collection of different neural networks, that together present the example, with “expander” 1.30D.  It gives a much better result compared to other neural networks procedures. The value which was official announced on that day is 4084.76.

  Therefore  in direction of obtaining better quantitative results is changing the sequence of calculations, which we carried out in previous example. We can use concurrent calculations to create the committee. The committee tends not only to a stability but it also allows an effective relative control of training conditions. Relative scattering of the results from committee is the figure of merit in this case. To create the concurrency we used the jetlang package.  The committee includes the following modules in the example: Actor, Channels, Message and it will be controlled from the Main module.

The periodic signals prevail in this model ( postulate ). Which periodic signal is major? Where is the under fitting or over fitting of perceptron? Is it possible to automatically predict in this model? We'll show an algorithm with auto-correction which demonstrates some basic development ideas ("a biggest harmonicas wave"):

1.      We have more than one sequence of calculations ( see "committee")

2.      The main circle gives the variation of number of points (N) in the window of time prediction. A head period is determined by N in every variation from "a biggest harmonicas wave". The number of hidden layers is 2*N+1 automatic. As figure of merit will be used the R = ∑ | |Fobs| - |Fcalc| | / ∑ |Fobs| , so called R-factor through full time series data.

3.      For each N will be variety the training sets: the elements  will be removed consecutively to achieve the minimum of R-factor and to achieve the optimal relationship between the under fitting and over fitting.

4.      The middle value through committee is the result of this simple automatic flow.

How good is this model "a biggest harmonicas wave" for your tasks, you should decide yourself. The simple DAX tests showed good mathematical results. This filtering of the model are similar to the human sleep: trainings models are maximally reduced, but when the recognition is improved.

By time series prediction the best results  were obtained when analyzing the grow and fall of stock values​​, not for a prognosis of values yourself.

Bollinger Bands in stock market with Neuroph

Bollinger Bands is invented by John Bolliger. Bollinger Bands indicate a variation of price of a financial instrument over time.  The exit for borders of bands conducts with a high probability to return to bands. And the main difficulty is to find: when does occur the return? To predict this return we use Neuroph. Bollinger Bands reflects degree of nervousness and are well suitable for not stabile stock market. You can read more about the Bollinger Bands in other documents [ Wiki, for example] . We will us concentrate on the using technique with Neuroph.

The main parameters for Bollinger Bands are

As the test data will be used DAX.

Consider the training work of this method as an example. In a prepared file, which contains the values ​​of Dax for one day with a 5 minute intervals will be search for situation going beyond the value of the average. To average it is detected number of 21  last values experimentally. The output is 1,  if the current value of  Dax is upper 2 sigma average or below  2 sigma from average and in the 15 min is the behavior correct, in other cases it is 0. “Correct” means the value returns to the band. The sigma is the standard deviation and it will be calculated in the method “standardAbweichung” dynamically. The algorithm is implemented in the program BollingerAnalyze see the appendix (project package). The normalization is similar to that shown above, see the “normalize” method in the BollingerAnalyze.java.

In the training program will be estimated (in order to control) the frequency of correct operation of the method: how many cases are right from all cases. You can try other parameters for your coals. Using the trained network is shown in the method getRecommendation. The method gives 1 or -1 if you have Bollinger Bands situation (up or down) and in 15 minutes will  return DAX in the band. In a bad case it is 0 or the value can not be predicted.  Of course a big problem  is to obtain the values ​​in real time. However, this is an organizational problem and it may be made with the dynamic Neuroph value recognition from images in real time probably.

The message after the training and test is as the follow:

extremes number = 4707  bollingers extremes number. = 1006 full number of points = 21136 defects = 1. In the case “defects” means how many bollinger situation were not correct predicted.

Conclusion

 

Finally, two important factors in predicting problems - possibilities and interests of people who make and use prediction. Ideally, the historical information is analyzed automatically, and the forecast is a manager for a possible modification. The introduction of an expert in the forecasting process is important, but it requires the cooperation of experienced managers. Next forecast sent to managers, who use it to make decisions. And even if they say the weather is just talk, they can get real benefits from its use.

Download the NetBeans projects with source

If You have special orders or You want to support me simply (PayPal)