Also, I wanted to use my programmed indicators into my strategy. For my strategy this I must also use an indicator with two outputs. How can I install the two outputs of SMAoverCCI-indicator in my strategy:
/**
*SMAoverCCI
*/
package jforex.indicators;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle;
public class SMAOverCCI implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][][] inputs = new double[1][][];
private double[][] outputs = new double[2][];
private IIndicatorContext context;
private IIndicator cciIndicator;
private IIndicator smaIndicator;
public void onStart(IIndicatorContext context) {
//getting interfaces of CCI and SMA indicators
IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
cciIndicator = indicatorsProvider.getIndicator("CCI");
smaIndicator = indicatorsProvider.getIndicator("SMA");
//inicator with one input, two optional params and two outputs
indicatorInfo = new IndicatorInfo("SMA_CCI", "SMA over CCI", "My indicators", false, false, true, 1, 2, 2);
//one input array of doubles
inputParameterInfos = new InputParameterInfo[] {
new InputParameterInfo("Input CCI", InputParameterInfo.Type.PRICE)};
//two optional params, one for every indicator
optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("CCI Time Period", OptInputParameterInfo.Type.OTHER,
new IntegerRangeDescription(38, 0, 300, 100)), new OptInputParameterInfo("SMA Time Period", OptInputParameterInfo.Type.OTHER,
new IntegerRangeDescription(15, 0, 300, 100))};
//two output arrays, one for RSI and one for SMA over CCI
outputParameterInfos = new OutputParameterInfo[] {
new OutputParameterInfo("CCI line", OutputParameterInfo.Type.DOUBLE, DrawingStyle.LINE),
new OutputParameterInfo("SMA line", OutputParameterInfo.Type.DOUBLE, DrawingStyle.LINE)};
}
public IndicatorResult calculate(int startIndex, int endIndex) {
//calculating cci
int cciLookback = cciIndicator.getLookback();
//first alocate buffer for cci results
double[] cciOutput;
if (startIndex > endIndex || cciLookback > endIndex) {
return new IndicatorResult(0, 0);
} else {
//take the greater value (startindex/ lookback)
cciOutput = new double[endIndex - (cciLookback > startIndex ? cciLookback : startIndex) + 1];
}
//init cci indicator with input data and array for output
cciIndicator.setInputParameter(0, inputs[0]);
cciIndicator.setOutputParameter(0, cciOutput);
IndicatorResult cciResult = cciIndicator.calculate(startIndex, endIndex);
if (cciResult.getNumberOfElements() < smaIndicator.getLookback()) {
//not enough data to calculate sma
return new IndicatorResult(0, 0);
}
//calculating sma
smaIndicator.setInputParameter(0, cciOutput);
smaIndicator.setOutputParameter(0, outputs[1]);
IndicatorResult smaResult = smaIndicator.calculate(0, cciResult.getNumberOfElements() - 1);
if (smaResult.getNumberOfElements() == 0) {
//sma returned 0 values
return new IndicatorResult(0, 0);
}
//copy cci values to output excluding first values used for sma lookback
System.arraycopy(cciOutput, smaResult.getFirstValueIndex(), outputs[0], 0, smaResult.getNumberOfElements());
//creating result, first value index for our input is FVI for cci + FVI for sma, because we calculated sma starting from 0 element
IndicatorResult result = new IndicatorResult(cciResult.getFirstValueIndex() + smaResult.getFirstValueIndex(), smaResult.getNumberOfElements());
return result;
}
public IndicatorInfo getIndicatorInfo() {
return indicatorInfo;
}
public InputParameterInfo getInputParameterInfo(int index) {
if (index <= inputParameterInfos.length) {
return inputParameterInfos[index];
}
return null;
}
public int getLookback() {
return cciIndicator.getLookback() + smaIndicator.getLookback();
}
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) {
//set optional params in indicators
switch (index) {
case 0:
cciIndicator.setOptInputParameter(0, value);
break;
case 1:
smaIndicator.setOptInputParameter(0, value);
break;
}
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
}
Thank you for your help in advance.
regards