/** * * This is a http://en.wikipedia.org/wiki/MACD * http://investexcel.net/how-to-calculate-macd-in-excel/ * * @author Freeman Lo * @version 0.0.1 Build 1 April 1, 2014. * Limitations - * * */ import java.util.ArrayList; public class ProcessEMA12 { ArrayList sArrList = new ArrayList (); ArrayList ema12 = new ArrayList (); public ProcessEMA12 (ArrayList sArrList) { this.sArrList = sArrList; } private ArrayList getCloseValues() { ArrayList closeValues = new ArrayList (); String [] words = new String[6]; int a=0; while ( a < this.sArrList.size() ) { String line = this.sArrList.get(a); words = line.split(","); for (int x = 0; x < words.length; x++){ if ( x == 4 ) { closeValues.add(words[4]); } } a+=1; } return closeValues; } private double getAverage() { ArrayList closeValues = getCloseValues(); double avgValue = 0.00; for ( int item = 0; item < 12; item++ ) { avgValue += Double.parseDouble(closeValues.get(item)); } avgValue = avgValue / 12.00; return avgValue; } public ArrayList computeEMAValues() { ArrayList closeValues = getCloseValues(); double avgvalue = 0.00; double result = 0.00; avgvalue = getAverage(); this.ema12.add(avgvalue); for ( int item = 0; item < closeValues.size()-12; item++ ) { // starts with 0, so 12 is actually 13th place // Formular to compute EMA12 result = (( Double.parseDouble(closeValues.get(item+12))* (2.00 / (12.00+1.00))) + (this.ema12.get(item)) * ((1.00 - (2.00 / (12.00+1.00) )))); this.ema12.add(result); } return ema12; } }