Consider the following approach:
package jforex.indicators;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.indicators.*;
public class MultiBarsFromParams implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private IBar[][] inputs = new IBar[1][];
private double[][] outputs = new double[1][];
private InputParameterInfo barInput;
private IConsole console;
private Instrument instrument = Instrument.EURUSD;
public void onStart(IIndicatorContext context) {
this.console = context.getConsole();
//load Period values and names
int[] periodValues = new int[Period.values().length];
String[] periodNames = new String[Period.values().length];
for (int i = 0; i < periodValues.length; i++) {
periodValues[i] = i;
periodNames[i] = Period.values()[i].name();
}
//load OfferSide values and names
int[] offerSideValues = new int[OfferSide.values().length];
String[] offerSideNames = new String[OfferSide.values().length];
for (int i = 0; i < offerSideValues.length; i++) {
offerSideValues[i] = i;
offerSideNames[i] = OfferSide.values()[i].name();
}
indicatorInfo = new IndicatorInfo("OPTPARAMS", "Period and Offer side from opt input params", "My indicators", false, false, false, 1, 2, 1);
optInputParameterInfos = new OptInputParameterInfo[] {
//period as optional input
new OptInputParameterInfo("Period", OptInputParameterInfo.Type.OTHER,
new IntegerListDescription(Period.ONE_MIN.ordinal(), periodValues, periodNames)),
//offer side as optional input
new OptInputParameterInfo("Offer Side", OptInputParameterInfo.Type.OTHER,
new IntegerListDescription(OfferSide.BID.ordinal(), offerSideValues, offerSideNames))
};
barInput = new InputParameterInfo("bar from input", InputParameterInfo.Type.BAR){{ setInstrument(instrument); }};
inputParameterInfos = new InputParameterInfo[] { barInput };
outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)};
}
//always returns 1
public IndicatorResult calculate(int startIndex, int endIndex) {
if (startIndex - getLookback() < 0) {
startIndex -= startIndex - getLookback();
}
int i, j;
for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
outputs[0][j] = 1.0;
}
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) {
//log the received data of the currently forming bar
print(index + ". input: " + arrayToString((IBar[]) array));
inputs[index] = (IBar[]) array;
}
//this method reacts on every optional input parameter change
public void setOptInputParameter(int index, Object value) {
int enumIndex = ((Integer) value).intValue();
if (index == 0) {
barInput.setPeriod(Period.values()[enumIndex]);
}
if (index == 1) {
barInput.setOfferSide(OfferSide.values()[enumIndex]);
}
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
public static String arrayToString(Object [] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
str += "[" + r + "] "+ arr[r] + "; ";
}
return str;
}
private void print(Object o){ this.console.getOut().println(o); }
}