Hi all,
could anyone give me an example of indicator with multiply optional input parameters?
I'm trying to make an indicator that draws MA with 2 parallel lines and i need to add 1 more (besides time period) input that would contain a line shift value.
Line 1 = MA + value;
Line 2 = MA - value.
Here's the code:
package jforex;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
public class ChannelMA implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][] inputs = new double[2][];
private int timePeriod = 5;
private double channelValue = 0.001;
private double[][] outputs = new double[3][];
public void onStart(IIndicatorContext context) {
indicatorInfo = new IndicatorInfo("Channel MA", "Calculates MA with channel", "My indicators",
true, false, false, 2, 2, 3);
inputParameterInfos = new InputParameterInfo[] {
new InputParameterInfo("Price 1:", InputParameterInfo.Type.DOUBLE){
{
setAppliedPrice(AppliedPrice.HIGH);
}
},
new InputParameterInfo("Price 2:", InputParameterInfo.Type.DOUBLE){
{
setAppliedPrice(AppliedPrice.LOW);
}
}
};
optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
new IntegerRangeDescription(5, 2, 100, 1)),
new OptInputParameterInfo("Channel value", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.001, 0, 1.0, 0.1, 5))
};
outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("MA", OutputParameterInfo.Type.DOUBLE,
OutputParameterInfo.DrawingStyle.LINE),
new OutputParameterInfo("Channel1", OutputParameterInfo.Type.DOUBLE,OutputParameterInfo.DrawingStyle.LINE),
new OutputParameterInfo("Channel2", 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 value = 0;
for (int k = timePeriod; k > 0; k--) {
value += (inputs[0][i - k] - inputs[1][i - k]) / 2 + inputs[1][i - k];
}
outputs[0][j] = value / timePeriod;
outputs[1][j] = value / timePeriod + channelValue;
outputs[2][j] = value / timePeriod - channelValue;
}
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;
channelValue = (Double) value;
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
}
I've added these lines for additional opt input:
private double channelValue = 0.001;
new OptInputParameterInfo("Channel value", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.001, 0, 1.0, 0.1, 5))
and for setOptInputParameter method (I'm not sure if this is correct):
public void setOptInputParameter(int index, Object value) {
timePeriod = (Integer) value;
channelValue = (Double) value;
}
It compiles well but it doesn't work, the error appears:
18:03:46 java.lang.Integer cannot be cast to java.lang.Double
How to fix it?
Thank you in advance!