Indicator with multiple input types

Overview

The following example demonstrates how multiple inputs of different types may be defined and used in a single indicator. We will create an indicator that calls both ATR and STDDEV indicators.

Setup

public class STDDEVATRIndicator implements IIndicator{
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    //define inputs for both indicators
    private double[][][] atrInputs = new double[1][][];
    private double[][] stddevInputs = new double[1][];    
    private int atrTimePeriod = 2;
    private int stddevTimePeriod = 5;
    private double nbDev = 1;      
    //define two ouputs
    private double[][] outputs = new double[2][];
    private IIndicatorsProvider indicatorsProvider;
    private IIndicator atrIndicator;
    private IIndicator stddevIndicator;
public void onStart(IIndicatorContext context) {
        indicatorsProvider = context.getIndicatorsProvider();
        atrIndicator = indicatorsProvider.getIndicator("ATR");
        stddevIndicator = indicatorsProvider.getIndicator("STDDEV");        
        indicatorInfo = new IndicatorInfo("ATRSTDDEV", "ATRSTDDEV", "My indicators",
              false, false, false,
              2, 3, 2);
        inputParameterInfos = new InputParameterInfo[] {
                new InputParameterInfo("atr Input", InputParameterInfo.Type.PRICE),
                new InputParameterInfo("stddev Input", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {
                new OptInputParameterInfo("ATR Time period",
                    OptInputParameterInfo.Type.OTHER,
                    new IntegerRangeDescription(atrTimePeriod, 2, 100, 1)),
                new OptInputParameterInfo("STDDEV Time period",
                    OptInputParameterInfo.Type.OTHER,
                    new IntegerRangeDescription(stddevTimePeriod, 2, 100, 1)),
                new OptInputParameterInfo("Nb Dev",
                    OptInputParameterInfo.Type.OTHER,
                    new DoubleRangeDescription(nbDev, 1, 100, 0.2, 2))};
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("ATR",
                    OutputParameterInfo.Type.DOUBLE,
                    OutputParameterInfo.DrawingStyle.LINE),
                new OutputParameterInfo("STDDEV",
                    OutputParameterInfo.Type.DOUBLE,
                    OutputParameterInfo.DrawingStyle.LINE)};
}

Method getLookBack

//Fetching the greater of two lookbacks
public int getLookback() {
       return Math.max(atrIndicator.getLookback(), stddevIndicator.getLookback());
}

Method Calculate

public IndicatorResult calculate(int startIndex, int endIndex) {        
       if (startIndex - getLookback() < 0) {
           startIndex -= startIndex - getLookback();
       }
       //Setting up input and output parameters for both indicators
       atrIndicator.setInputParameter(0, atrInputs[0]);
       atrIndicator.setOutputParameter(0, outputs[0]);
       stddevIndicator.setInputParameter(0, stddevInputs[0]);
       stddevIndicator.setOutputParameter(0, outputs[1]);
       //Calculating
       atrIndicator.calculate(startIndex, endIndex);
       return stddevIndicator.calculate(startIndex, endIndex);
}

Download

STDDEVATRIndicator.java

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