<commit_before><commit_msg>Initial code complete for MACD<commit_after>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();
	}
}
{'fraction_non_alphanumeric': 0.14241866175567833, 'fraction_numerical': 0.008594229588704727, 'mean_word_length': 4.699300699300699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4640461', 'n_tokens_mistral': 612, 'n_tokens_neox': 534, 'n_words': 111}