package com.jasonlam604.stocktechnicals.indicators;

/**
 * Moving Average Convergence/Divergence Oscillator
 */
public class MovingAverageConvergenceDivergence {

	private double[] prices;
	private double[] macd;
	private double[] signal;

	public MovingAverageConvergenceDivergence calculate(double[] prices, int shortEmaPeriod, int longEmaPeriod,
			int signalPeriod) throws Exception {

		this.prices = prices;
		this.macd = new double[prices.length];
		this.signal = new double[prices.length];

		ExponentialMovingAverage emaShort = new ExponentialMovingAverage();
		emaShort.calculate(prices, shortEmaPeriod).getEma();

		ExponentialMovingAverage emaLong = new ExponentialMovingAverage();
		emaLong.calculate(prices, longEmaPeriod).getEma();

		for (int i = longEmaPeriod - 1; i < this.prices.length; i++) {
			this.macd[i] = emaShort.getEma()[i] - emaLong.getEma()[i];
		}

		ExponentialMovingAverage signalEma = new ExponentialMovingAverage();
		this.signal = signalEma.calculate(this.macd, signalPeriod).getEma();

		return this;
	}

	public double[] getMACD() {
		return this.macd;
	}

	public double[] getSignal() {
		return this.signal;
	}

	@Override
	public String toString() {

		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < this.prices.length; i++) {
			sb.append(String.format("%02.2f", this.prices[i]));
			sb.append(" ");
			sb.append(String.format("%02.2f", this.macd[i]));
			sb.append(" ");
			sb.append(String.format("%02.2f", this.signal[i]));
			sb.append(" ");
			sb.append("\n");
		}

		return sb.toString();
	}
}

@@ -308,24 +308,20 @@
 es, int 
-shortEma
+fast
 Period, 
@@ -324,23 +324,20 @@
 od, int 
+s
 lo
-ngEma
+w
 Period,%0A
@@ -589,16 +589,12 @@
 es, 
-shortEma
+fast
 Peri
@@ -705,23 +705,20 @@
 prices, 
+s
 lo
-ngEma
+w
 Period).
@@ -747,15 +747,12 @@
 i = 
+s
 lo
-ngEma
+w
 Peri

rename input parameters