Simple Indicator

To create a custom indicator in JForex, right click on Strategy menu option in the Workspace section and select New Indicator:

JForex will generate the following default indicator code:

import com.dukascopy.api.indicators.*;

 public class Indicator implements IIndicator {
    private IndicatorInfo indicatorInfo;    
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    //Indicator input used in calculations
    private double[][] inputs = new double[1][];
    //Default value of optional parameter
    private int timePeriod = 4;
    //Array of indicator output values
    private double[][] outputs = new double[1][];

Method onStart is used to initialize:

  • indicatorInfo: provides basic indicator description e.g. name, title, group, position (over chart or in a subwindow) number of optional parameters, number of outputs, etc.
  • inputParameterInfos: describes indicator input.
  • optInputParameterInfos: describes optional input.
  • outputParameterInfos: describes indicator output.

For in depth description of indicator parameters please see Parameter Configuration section

    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)
        };
    }

Method calculate is used for indicator calculations. This simple indicator sums the number of values defined in the timePeriod parameter:

  • First we need to adjust the startIndex of the input array since we need a number of values (defined in the indicator lookBack) to calculate the first output value.
  • Variables ' and j are used to iterate over the input array and the output array respectively.
  • Method calculate has to return the IndicatorResult object which specifies the index of the first element in the input array that has the corresponding calculated value and a number of calculated values.
public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into an account the lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            //sum values
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;
        }
        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() {
        //calculate indicator lookBack
        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;
    }
 }

Indicator.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.