package jforex.indicators;

import com.dukascopy.api.*;

import com.dukascopy.api.indicators.*;

public class CurrencyRSIIndicator implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputBars = new IBar[3][];
    private double[][] outputs = new double[3][];
   
    public void onStart(IIndicatorContext context) {
        
        //indicator info
        indicatorInfo = new IndicatorInfo("RSI_CURR", "RSI for various instruments", "", false, false, true, 3, 0, 3);

        //indicator inputs
        InputParameterInfo gbpUsdInput = new InputParameterInfo("Input data", InputParameterInfo.Type.BAR);
        gbpUsdInput.setInstrument(Instrument.GBPUSD);
        InputParameterInfo audCadInput = new InputParameterInfo("Input data", InputParameterInfo.Type.BAR);
        audCadInput.setInstrument(Instrument.AUDCAD);
        InputParameterInfo eurJpyInput = new InputParameterInfo("Input data", InputParameterInfo.Type.BAR);
        eurJpyInput.setInstrument(Instrument.EURJPY);        
        inputParameterInfos = new InputParameterInfo[] {  gbpUsdInput, audCadInput, eurJpyInput};
        
        OutputParameterInfo gbpUsdOutput = new OutputParameterInfo("gbpUsd_MA", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
        OutputParameterInfo audCadOutput = new OutputParameterInfo("audCad_MA", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
        OutputParameterInfo eurJpyOutput = new OutputParameterInfo("eurJpy_MA", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
        
        outputParameterInfos = new OutputParameterInfo[] {  gbpUsdOutput, audCadOutput, eurJpyOutput};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into an account the lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        
        //i - input array iterator, j - output array iterator
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            //k: 0 = GBPUSD; 1 = AUDCAD; 2 = EURJPY
            for (int k = 0; k < outputs.length; k++){
                //TODO: change this to real RSI algorithm
                if( i < inputBars[k].length){
                    outputs[k][j] = inputBars[k][i].getClose() / inputParameterInfos[k].getInstrument().getPipValue() % 10;
                }
            }
        }
        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 0;
    }

    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) {
        inputBars[index] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {}

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