Skip to content

Instantly share code, notes, and snippets.

@powerlim2
Created May 28, 2013 00:20
Show Gist options
  • Save powerlim2/5659726 to your computer and use it in GitHub Desktop.
Save powerlim2/5659726 to your computer and use it in GitHub Desktop.
Simple Moving Average - version2 : this program is now able to deal with duplicate dates ( it will give you total stock value for each date and calculate Moving Average). Enjoy.
import java.io.*;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: joonhyunglim
* Date: 5/25/13
* Time: 6:34 PM
* To change this template use File | Settings | File Templates.
*/
public class SimpleMovingAverage {
private final Queue<Double> window = new LinkedList<Double>(); // Last in First Out
private final int period;
private double sum;
public SimpleMovingAverage(int period) {
assert period > 0 : "Period must be a positive integer";
this.period = period;
}
public void newNum(double num) {
sum += num;
window.add(num);
if (window.size() > period) {
sum -= window.remove();
}
}
public double getAvg() {
if (window.isEmpty()) return 0; // technically the average is undefined
return sum / window.size();
}
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); // this is for measuring the computation time.
// ArrayList<Double> Data = new ArrayList<Double>();
Hashtable<String,Double> grouped = new Hashtable<String,Double>();
int[] windowSizes = {30,50,100,200};
// Reading file — Assume that there is no column header.
File NASDAQ = new File("/Users/joonhyunglim/Desktop/MSiA/MSiA431/data/NASDAQ_daily_combined.csv");
BufferedReader Reader = new BufferedReader(new FileReader(NASDAQ));
String line;
while ((line = Reader.readLine()) != null) {
String[] tokens = line.split(",");
// Data.add(Double.parseDouble(tokens[6]));
if (grouped.contains(tokens[2])) {
grouped.put(tokens[2],grouped.get(tokens[2]) + Double.parseDouble(tokens[6])); // group sum
} else {
grouped.put(tokens[2], Double.parseDouble(tokens[6]));
}
}
// System.out.println(grouped.size());
Reader.close();
for (int windSize : windowSizes) { // it will create number of files based on your windowsize specification.
// Writing the outcome
File outfile = new File("/Users/joonhyunglim/Desktop/MSiA/MSiA431/data/NASDAQ_Moving_Average_"+windSize+".csv");
// you don't need to create the file previously, it will create the file if it does not exist
if (outfile.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("I found the file. I am writing to the existing file");
}
BufferedWriter result = new BufferedWriter(new FileWriter(outfile));
// Simple Moving Average
SimpleMovingAverage ma = new SimpleMovingAverage(windSize);
// sort the Hashtable by key and iterate them
String[] keys = grouped.keySet().toArray(new String[0]);
Arrays.sort(keys);
for(String key : keys) {
ma.newNum(grouped.get(key));
result.write(key +","+ grouped.get(key)+"," + ma.getAvg()); // for each date, Left is a data point, and the right is a SMA.
result.newLine();
}
// This is for the unsorted version
/*for (Map.Entry<String,Double> x : grouped.entrySet()) {
ma.newNum(x.getValue());
result.write(x.getKey() +","+ x.getValue()+"," + ma.getAvg()); // for each date, Left is a data point, and the right is a SMA.
result.newLine();
} */
result.close();
}
long time = System.currentTimeMillis() - start;
System.out.println("\n**Execution Time is : "+time/1000+"seconds**");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment