red wrote:
the element inputs[0][inputs[0].length-1] is usually not considered in the computation
This is because the last input element mostly corresponds to the currently forming bar (inputs might be bars, prices or price arrays, see
InputParameterInfo.Type). Hence, unless the indicator is designed to work with in-progress data, the
inputs[0][inputs[0].length-1] value does not get used.
As an example indicator which considers current bar data, see
com.dukascopy.indicators.VolumeIndicator.class in
JForex Standalone API.
In Simple Indicator the indicator value on current bar is the sum of prices of the previous 2 bars => the current bar's price does not get used => we don't use the
inputs[0][inputs[0].length-1] value. To see what happens please consider adding some logging to the indicator:
package jforex.indicators;
import java.text.DecimalFormat;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.indicators.*;
public class SimpleIndicatorLog implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][] inputs = new double[1][];
private int timePeriod = 2;
private double[][] outputs = new double[1][];
private IConsole console;
public void onStart(IIndicatorContext context) {
indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
false, false, false, 1, 1, 1);
inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
new IntegerRangeDescription(2, 2, 100, 1))};
outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
OutputParameterInfo.DrawingStyle.LINE)};
console = context.getConsole();
}
public IndicatorResult calculate(int startIndex, int endIndex) {
//calculating startIndex taking into account lookback value
if (startIndex - getLookback() < 0) {
startIndex -= startIndex - getLookback();
}
int i, j;
String inputElLog = "input elements: ";
for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
double value = 0;
for (int k = timePeriod; k > 0; k--) {
value += inputs[0][i - k];
//inputElLog+="take input " + (i - k) + " for output " + j + "; ";
inputElLog+="take " + (i - k) + "; ";
}
outputs[0][j] = value;
}
if(endIndex < 100){
print("calculate, startIndex="+startIndex + " endIndex="+endIndex + " lookback=" + getLookback()
+ " inputs: " + arrayToString(inputs[0]) + " outputs: " + arrayToString(outputs[0]));
print(inputElLog);
}
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;
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
private void print(Object o){
this.console.getOut().println(o);
}
private void printErr(Object o){
this.console.getErr().println(o);
}
public static String arrayToString(IBar [] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
str += "[" + r + "] "+ arr[r] + "; ";
}
return str;
}
public static String arrayToString(double [] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
str += "[" + r + "] "+ (new DecimalFormat("0.00000")).format(arr[r]) + "; ";
}
return str;
}
public static String arrayToString(double [][] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
for (int c=0; c<arr[r].length; c++) {
str += " [" + r + "][" + c + "] " + (new DecimalFormat("0.00000")).format(arr[r][c]);
}
str += "; ";
}
return str;
}
}