Using Weka for stock value forecasting (Advanced)

The following recipe uses Apple stock data (found in the dataset directory) taken from Yahoo! finance. This file contains daily high, low, opening, and closing data for Apple computer stocks from January 3, 2011 to December 31, 2011. Based on the last 12-24 days, we will forecast the next day's closing price.

Getting ready

This recipe requires Weka developer version and forecasting module (introduced in versions greater than Weka 3.7.3) named timeseriesForecasting1.0.10.zip, which can be downloaded from http://sourceforge.net/projects/weka/files/weka-packages/.

Add the following JAR to the class path of your local machine:

-classpath ".;C:Program Files (x86)Weka-3-7weka.jar;C:Weka-3-7 timeseriesForecastingpdm-timeseriesforecasting-ce-TRUNK-SNAPSHOT.jar"

How to do it...

Follow this snippet:

import java.io.*;

import java.util.List;
import weka.core.Instances;
import weka.classifiers.functions.GaussianProcesses;
import weka.classifiers.evaluation.NumericPrediction;
import weka.classifiers.timeseries.WekaForecaster;

public class TimeSeriesExample {

  public static void main(String[] args) {
    try {

      Instances dataset = new Instances(new BufferedReader(new FileReader("dataset/appl.arff")));
      dataset.sort(0);

      WekaForecaster forecaster = new WekaForecaster();
      forecaster.setFieldsToForecast("Close");

      forecaster.setBaseForecaster(new GaussianProcesses());

      forecaster.getTSLagMaker().setTimeStampField("Date"); 
      forecaster.getTSLagMaker().setMinLag(12);
      forecaster.getTSLagMaker().setMaxLag(24);

      forecaster.buildForecaster(dataset);

      forecaster.primeForecaster(dataset);
      
      List<NumericPrediction> predsAtStep = forecast.get(i);
      NumericPrediction predForTarget = predsAtStep.get(0);
      System.out.println("" + predForTarget.predicted() + " ");

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

This code snippet outputs the next stock value.

How it works...

First, make the following imports.

import java.io.*;

import java.util.List;
import weka.core.Instances;
import weka.classifiers.functions.GaussianProcesses;
import weka.classifiers.evaluation.NumericPrediction;
import weka.classifiers.timeseries.WekaForecaster;

public class TimeSeriesExample {

  public static void main(String[] args) {
    try {

Load the stock data:

      Instances dataset = new Instances(new BufferedReader(new FileReader("dataset/appl.arff")));

Sort the data by date (that is, the first attribute):

      dataset.sort(0);

Initialize a new forecaster:

      WekaForecaster forecaster = new WekaForecaster();

Set the target variables we want to forecast:

      forecaster.setFieldsToForecast("Close");

Replace the SMOreg default classifier with Gaussian processes:

      forecaster.setBaseForecaster(new GaussianProcesses());

Specify that the Date attribute contains the timestamp field:

      forecaster.getTSLagMaker().setTimeStampField("Date"); 

The forecaster constructs a window of samples over a time period to allow the forecasting algorithm to capture the relationship between past and current value. The minimum and maximum lags define the number of past steps to be included in the window:

      forecaster.getTSLagMaker().setMinLag(12);
      forecaster.getTSLagMaker().setMaxLag(24); 

Build the forecaster:

     forecaster.buildForecaster(dataset);

Put enough recent historical data to cover the maximum lag. In our case, we just specify the complete dataset:

      forecaster.primeForecaster(dataset);

Forecast the next value and output predictions:

      List<NumericPrediction> predsAtStep = forecast.get(0);
      NumericPrediction predForTarget = predsAtStep.get(0);
      System.out.println("" + predForTarget.predicted() + " ");

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

The outputted number is the next predicted value. You can then use this value in your decision logic, for example, if the predicted value is higher than the current value, then generate buy signal

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85