package jforex.indicators;

import com.dukascopy.api.IConsole;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorInfo;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.InputParameterInfo;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

//+===================================================================================+

public class BarFour2 implements IIndicator {
	public static final int OPEN = 0;
	public static final int CLOSE = 1;
	public static final int MIN = 2;
	public static final int MAX = 3;
	public static final int AVE = 4;

	private IndicatorInfo indicatorInfo;
	private InputParameterInfo[] inputParameterInfos;
	private OutputParameterInfo[] outputParameterInfos;
	private double[][][] inputs = new double[1][][];
	private double[][] outputs = new double[2][];
	
	private int lookback = 2;

	IConsole console;

	// +---------------------------------------------------------------------------------+
	public void onStart(IIndicatorContext context) {
		this.console = context.getConsole();

		indicatorInfo = new IndicatorInfo("ABsum", "ABsum", "Custom", false,
				true, false, 1, 0, 2);
		inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo(
				"Price", InputParameterInfo.Type.PRICE) };

		outputParameterInfos = new OutputParameterInfo[] {
				new OutputParameterInfo("Line1",
						OutputParameterInfo.Type.DOUBLE,
						OutputParameterInfo.DrawingStyle.LINE),
				new OutputParameterInfo("Line2",
						OutputParameterInfo.Type.DOUBLE,
						OutputParameterInfo.DrawingStyle.LINE) };
	}

	// +--------------------------------------------------------------------------------+
	public IndicatorResult calculate(int startIndex, int endIndex) {
		if (startIndex - getLookback() < 0) {
			startIndex -= startIndex - getLookback();
		}
		if (startIndex > endIndex) {
			return new IndicatorResult(0, 0);
		}

		int resLen = endIndex - startIndex + 1;
		double[] AB = new double[resLen];

		//print("startIndex=" + startIndex + " endIndex=" + endIndex);

		for (int i = 0; i < resLen; i++) {
			int inputIndex = i + getLookback();

			double[] candle = new double[4];

			candle[OPEN] = inputs[0][0][inputIndex];
			candle[CLOSE] = inputs[0][1][inputIndex];
			candle[MAX] = inputs[0][2][inputIndex];
			candle[MIN] = inputs[0][3][inputIndex];

			outputs[0][i] = (inputs[0][2][inputIndex] - inputs[0][3][inputIndex]) / 2;

			double x3 = 0;
			for (int j = i; j <= i + getLookback(); j++){
				x3 += (inputs[0][2][j] - inputs[0][3][j]) / 2;
			}
			
			outputs[1][i] = x3 / (getLookback() + 1); // 2*AB[i];
		}
		return new IndicatorResult(startIndex, resLen);
	}

	// +--------------------------------------------------------------------------------+
	public IndicatorInfo getIndicatorInfo() {
		return indicatorInfo;
	}

	// +--------------------------------------------------------------------------------+
	public InputParameterInfo getInputParameterInfo(int index) {
		if (index <= inputParameterInfos.length) {
			return inputParameterInfos[index];
		}
		return null;
	}

	// +--------------------------------------------------------------------------------+
	public int getLookback() {
		return lookback;
	}

	// +--------------------------------------------------------------------------------+
	public OutputParameterInfo getOutputParameterInfo(int index) {
		if (index <= outputParameterInfos.length) {
			return outputParameterInfos[index];
		}
		return null;
	}

	// +--------------------------------------------------------------------------------+
	public void setInputParameter(int index, Object array) {
		inputs[0] = (double[][]) array;
	}

	// +--------------------------------------------------------------------------------+
	public OptInputParameterInfo getOptInputParameterInfo(int index) {
		return null;
	}

	// +--------------------------------------------------------------------------------+
	public void setOptInputParameter(int index, Object value) {

	}

	// +--------------------------------------------------------------------------------+
	public void setOutputParameter(int index, Object array) {
		outputs[index] = (double[]) array;
	}

	// +--------------------------------------------------------------------------------+
	public int getLookforward() {
		return 0;
	}
	
    private void print(Object o) {
        this.console.getOut().println(o);
    }
    
}
// +////////////////////////////////////////////////////////////////////////////+