Friday, August 31, 2018

Using 2 Moving Averages of SPY Closing #s To Build A WEKA ARFF File


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;

/**
 * @author joe mcverry
 *
 * video discussion: https://www.youtube.com/watch?v=_9sWbCEq-u
 */
public class BuildSPYSARFFFIle {

 public static void main(String[] args) throws IOException {
 
 
  int daysAhead = 5;
 
  PrintWriter pwTrain = new PrintWriter("spy.arff");
  pwTrain.println("% 1. Title: spy");
  pwTrain.println("% first one");
  pwTrain.println("% 2. Sources:");
  pwTrain.println("%      (a) Creator: Joe McVerry");
  pwTrain.println("%      (c) Date: " + new Date());
  pwTrain.println("%");
  pwTrain.println("@RELATION spyWithAverages");
  pwTrain.println("@ATTRIBUTE currentPrice numeric");
  pwTrain.println("@ATTRIBUTE maFast numeric");
  pwTrain.println("@ATTRIBUTE maSlow numeric");
  pwTrain.println("@ATTRIBUTE result numeric");
  pwTrain.println("@DATA");
  pwTrain.flush();

  BufferedReader br = new BufferedReader(new FileReader("spy.csv"));
  br.readLine(); // skip header
  /* Date,Open,High,Low,Close,Adj Close,Volume */
 
  ArrayList closes = new ArrayList<>();
  String in = "";
  while ((in = br.readLine()) != null) {
   String ins[] = in.split(",");
   closes.add(Double.parseDouble(ins[5]));
  }
  br.close();
 
  double[] dclose = new double[closes.size()];
  for (int ix = 0; ix < closes.size(); ix++)
   dclose[ix] = closes.get(ix);

  Core core = new Core();
  MInteger outBegIdx = new MInteger();
  MInteger outNBElement = new MInteger();
  double[] smaFast = new double[closes.size()];
  core.sma(0, dclose.length - 1, dclose, 5, outBegIdx, outNBElement, smaFast);
 
  for (int ix = smaFast.length - 1; ix > 5; ix--)
   smaFast[ix] = smaFast[ix - 5];
 
  outBegIdx = new MInteger();
  outNBElement = new MInteger();
  double[] smaSlow = new double[closes.size()];
  core.sma(0, dclose.length - 1, dclose, 20, outBegIdx, outNBElement, smaSlow);
  for (int ix = smaSlow.length - 1; ix > 20; ix--)
   smaSlow[ix] = smaSlow[ix - 20];
 
  for (int ix = 25; ix < dclose.length - daysAhead; ix++) {
   pwTrain.println(dclose[ix] + "," + smaFast[ix] + "," + smaSlow[ix] + "," + dclose[ix + daysAhead]);
  }

  pwTrain.flush();
  pwTrain.close();

 }

}

No comments:

Post a Comment