Dear Support,
I thank you for your support.
I have additionally programmed another KAMA-indicator for which I can change the 'fastestPeriod' and 'slowestPeriod' (at Dukascopys KAMA I can only modify the 'time period'). Now I wanted to ask if maybe the code can be optimized:
/**
* KAMA - INDICATOR
*/
package jforex.indicators;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IConsole;
public class myKama implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][] inputs = new double[1][];
private int timePeriod = 40;
private int fastestPeriod = 2;
private int slowestPeriod = 4;
private double[][] outputs = new double[1][];
private IIndicatorContext context;
private IConsole console;
public void onStart(IIndicatorContext context) {
this.console = context.getConsole();
indicatorInfo = new IndicatorInfo("myKama", "myKama Indicator", "My indicators", true, false, false, 1, 3, 1);
inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE) };
optInputParameterInfos = new OptInputParameterInfo[] {
new OptInputParameterInfo("Time Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(40, 1, 100, 1)),
new OptInputParameterInfo("fastest Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(2, 1, 20, 1)),
new OptInputParameterInfo("slowest Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(4, 1, 40, 1))
};
outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("myKama line", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) };
}
public IndicatorResult calculate(int startIndex, int endIndex) {
if (startIndex - getLookback() < 0)
startIndex -= startIndex - getLookback();
/* FORMULA:
* er(t) = abs(close(t) - close(t-n)) / sum(n)(abs(close(t - n) - close(t - n - 1)))
* sc(t) = (er(t) * (2 / (fp + 1) - 2 / (sp + 1)) + 2 / (sp + 1))^2
* kama(t) = sc(t) * (close(t) - kama(t-1)) + kama(t-1)
*
* Note: First KAMA values equals a price.
*/
double direction, volatility = 0;
//direction is just present over previous price
direction = Math.abs(inputs[0][endIndex] - inputs[0][endIndex - timePeriod]);
for (int i = 0; i < timePeriod; i++)
//volatility is the sum of all differences in the past period
volatility += Math.abs(inputs[0][endIndex - i] - inputs[0][endIndex - i - 1]);
//elastic ratio
double er = 1;
if (volatility != 0)
er = direction/volatility;
double fastest = 2 / (fastestPeriod + 1.0);
double slowest = 2 / (slowestPeriod + 1.0);
//smoothing constant
double sc = Math.pow(er * (fastest - slowest) + slowest, 2);
//first KAMA values equals the input-price
int i, j;
for (i = startIndex, j = 0; j < timePeriod; i++, j++)
outputs[0][j] = inputs[0][i];
for (; i < endIndex + 1; i++, j++)
outputs[0][j] = sc * (inputs[0][i] - outputs[0][j - 1]) + outputs[0][j - 1];
return new IndicatorResult(startIndex, outputs[0].length);
}
private void print(Object o){ this.console.getOut().println(o); }
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) {
//set Time Periods
switch (index) {
case 0:
timePeriod = (Integer) value;
break;
case 1:
fastestPeriod = (Integer) value;
break;
case 2:
slowestPeriod = (Integer) value;
break;
}
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
}