package jforex;

import com.dukascopy.api.indicators.*;

public class Beelzebub implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
   
    private double[][] inputs = new double[1][];
    private int timePeriod;
    private double[][] outputs = new double[1][];
    private IIndicator sma;
    //private IIndicator sma;
    
    
    
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("myMACD", "macd", "My indicators",
        		false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        //might need to be edited later
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time periood", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(26, 2, 100, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        
        //
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
        	double value26 = 0;
            double value12 = 0;
            
        	for (int k = 26; k > 0; k--) {
        		value26 += inputs[0][i - k];
        	}
            
            for (int k = 12; k > 0; k--) {
                value12 += inputs[0][i - k];
            }
            
            value26 = value26/ 26;
            
            value12 = value12 / 12;
            
            //outputs macd sma verison
            outputs[0][j] = value12 - value26;
            
        }
        return new IndicatorResult(startIndex, j);
    }
    

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (double[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}