Created
May 30, 2013 15:55
-
-
Save powerlim2/5678989 to your computer and use it in GitHub Desktop.
Simple Moving Average for a portfolio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); // First 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.size() != period) 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