Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Error in indicator: java.lang.IllegalArgumentException
 Post subject: Error in indicator: java.lang.IllegalArgumentException Post rating: 0   New post Posted: Sun 30 Jun, 2013, 17:09 

User rating: 1
Joined: Wed 07 Mar, 2012, 05:56
Posts: 101
Location: New CaledoniaNew Caledonia
hello support,

i try to make an indicator like BBbands with as moving average a tenkanSen.

however i make a mistake in my code but i don't known where.

this is the code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jforex.indicators;

import com.dukascopy.api.indicators.DoubleRangeDescription;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorInfo;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.InputParameterInfo;
import com.dukascopy.api.indicators.IntegerRangeDescription;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;
import java.awt.Color;

/**
 *
 * @author eric
 */
public class TenkanindBB implements IIndicator {
   
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[3][];
   
    private IIndicator tenkanMax;
    private IIndicator tenkanMin;
    private IIndicator stdDevUp;
    private IIndicator stdDevDown;
   
    private int tenkanPeriod = 9;
    private double nbDevUp=2;
    private double nbDevDown=2;
   
    public void onStart(IIndicatorContext context) {
        tenkanMax = context.getIndicatorsProvider().getIndicator("MAX");
        tenkanMin = context.getIndicatorsProvider().getIndicator("MIN");
        stdDevUp = context.getIndicatorsProvider().getIndicator("STDDEV");
        stdDevDown = context.getIndicatorsProvider().getIndicator("STDDEV");
       
        indicatorInfo = new IndicatorInfo("TENKANBB", "TenkanSen BB", "My indicators", true, false, false, 1, 3, 3);
       
        inputParameterInfos = new InputParameterInfo[] {
                new InputParameterInfo("Prices", InputParameterInfo.Type.PRICE)
                };
        optInputParameterInfos = new OptInputParameterInfo[] {
                new OptInputParameterInfo("Tenkan period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(tenkanPeriod, 2, 100, 1)),
                new OptInputParameterInfo("Nb Dev Up", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(nbDevUp, -10000, 10000, 0.01, 3)),
                new OptInputParameterInfo("Nb Dev Dn", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(nbDevDown, -10000, 10000, 0.01, 3)),
                };
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("TENKAN", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
                    {
                        this.setColor(Color.BLUE);
                    }
                },
                new OutputParameterInfo("Upper Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE){
                    {
                        this.setColor(Color.GREEN);
                    }
                },
                new OutputParameterInfo("Lower Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE){
                    {
                        this.setColor(Color.RED);
                    }
                },
                };
    }
    public IndicatorResult calculate(int startIndex, int endIndex) {
       
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
       
        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }
       
       
        double[] tenkanMaxD = new double[endIndex - startIndex + 2 + getLookback()];
        tenkanMax.setInputParameter(0, inputs[0][2]);
        tenkanMax.setOptInputParameter(0, tenkanPeriod);
        tenkanMax.setOutputParameter(0, tenkanMaxD);
        IndicatorResult dtenkanMaxResult = tenkanMax.calculate(startIndex - 1, endIndex);
       
        double[] tenkanMinD = new double[endIndex - startIndex + 2 + getLookback()];
        tenkanMin.setInputParameter(0, inputs[0][3]);
        tenkanMin.setOptInputParameter(0, tenkanPeriod);
        tenkanMin.setOutputParameter(0, tenkanMinD);
   IndicatorResult dtenkanMinResult = tenkanMin.calculate(startIndex - 1, endIndex);                                         
        
        double[] stdDevUpOutput = new double[endIndex - startIndex + 2 + getLookback()];                 
        stdDevUp.setInputParameter(0, inputs[0][2]);
   stdDevUp.setOptInputParameter(1, nbDevUp);
        stdDevUp.setOutputParameter(0, stdDevUpOutput);
   IndicatorResult stdDevUpResult = stdDevUp.calculate(startIndex-1, endIndex);
       
        double[] stdDevDownOutput = new double[endIndex - startIndex + 2 + getLookback()];                 
        stdDevDown.setInputParameter(0, inputs[0][3]);
        stdDevDown.setOptInputParameter(1, nbDevDown);
        stdDevDown.setOutputParameter(0, stdDevDownOutput);
        IndicatorResult stdDevDownResult = stdDevDown.calculate(startIndex - 1, endIndex);   
       
        int i, k;
        for (i = 1, k = dtenkanMaxResult.getNumberOfElements(); i < k; i++) {
            outputs[0][i - 1] = (tenkanMaxD[i] + tenkanMinD[i]) / 2;
         
            }   
       for (k=0; k < stdDevUpResult.getNumberOfElements();k++){
            outputs[1][k] = outputs[0][k] + stdDevUpOutput[k];
       outputs[2][k] = outputs[0][k] - stdDevDownOutput[k];
        }
        return new IndicatorResult(startIndex, i - 1);
    }
   
   

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    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) {
        switch (index) {
            case 0:
                tenkanPeriod = (Integer) value;
              //  stdDevUp.setOptInputParameter(0, tenkanPeriod);
         //  stdDevDown.setOptInputParameter(0, tenkanPeriod);
                break;
            case 1:
            nbDevUp = (Double) value;                                            
      //     stdDevUp.setOptInputParameter(1, nbDevUp);                                         
          //        stdDevUp.setOptInputParameter(0, tenkanPeriod);
                break;   
            case 2:
             nbDevDown = (Double) value;                                                                   
        //     stdDevDown.setOptInputParameter(1, nbDevDown);
         // stdDevDown.setOptInputParameter(0, tenkanPeriod);
                break;
           default:
                throw new ArrayIndexOutOfBoundsException(index);
        }
    }

    public void setOutputParameter(int index, Object array) {
       outputs[index] = (double[]) array;
    }

    public int getLookback() {
       return tenkanPeriod;
    }

    public int getLookforward() {
        return 0;
    }
   
}

the compilation is successfull but i have this error message and i don't understand why.(i'm begginner at programming)
Quote:
java.lang.IllegalArgumentException
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.dukascopy.api.impl.ed.callFunc(Unknown Source)
at com.dukascopy.api.impl.ec.calculate(Unknown Source)
at jforex.indicators.TenkanindBB.calculate(TenkanindBB.java:101)
at com.dukascopy.charts.math.a.a.a(Unknown Source)
at com.dukascopy.charts.math.dataprovider.AbstractDataProvider$e.c(Unknown Source)
at com.dukascopy.charts.math.dataprovider.AbstractDataProvider$e.run(Unknown Source)


can you please help me?


 
 Post subject: Re: Error in indicator: java.lang.IllegalArgumentException Post rating: 0   New post Posted: Mon 01 Jul, 2013, 09:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See our comments in the source code:

package jforex.indicators;

import java.awt.Color;

import com.dukascopy.api.IConsole;
import com.dukascopy.api.indicators.DoubleRangeDescription;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorInfo;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.InputParameterInfo;
import com.dukascopy.api.indicators.IntegerRangeDescription;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

/**
 *
 * @author eric
 */
public class TenkanindBB2 implements IIndicator {

    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[3][];

    private IIndicator tenkanMax;
    private IIndicator tenkanMin;
    private IIndicator stdDevUp;
    private IIndicator stdDevDown;

    private int tenkanPeriod = 9;
    private int stdDevPeriod = 9;
    private double nbDevUp = 2;
    private double nbDevDown = 2;
   
    //InputParameterInfo.Type.PRICE - open, close, high, low, volume
    private static final int HIGH = 2;
    private static final int LOW = 3;

    public void onStart(IIndicatorContext context) {
        tenkanMax = context.getIndicatorsProvider().getIndicator("MAX");
        tenkanMin = context.getIndicatorsProvider().getIndicator("MIN");
        stdDevUp = context.getIndicatorsProvider().getIndicator("STDDEV");
        stdDevDown = context.getIndicatorsProvider().getIndicator("STDDEV");


        inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Prices", InputParameterInfo.Type.PRICE) };
        optInputParameterInfos = new OptInputParameterInfo[] {
                new OptInputParameterInfo("Tenkan period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(tenkanPeriod, 2, 100, 1)),
                new OptInputParameterInfo("StdDev period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(stdDevPeriod, 2, 100, 1)),
                new OptInputParameterInfo("Nb Dev Up", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(nbDevUp, -10000, 10000, 0.01, 3)),
                new OptInputParameterInfo("Nb Dev Dn", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(nbDevDown, -10000, 10000, 0.01, 3)), };
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("TENKAN", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
                    { 
                        this.setColor(Color.BLUE);
                    }
                }, new OutputParameterInfo("Upper Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
                    {
                        this.setColor(Color.GREEN);
                    }
                }, new OutputParameterInfo("Lower Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
                    {
                        this.setColor(Color.RED);
                    }
                }, };
       

        indicatorInfo = new IndicatorInfo("TENKANBB", "TenkanSen BB", "My indicators", true, false, false,
                inputParameterInfos.length, optInputParameterInfos.length, outputParameterInfos.length);
       
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {

        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }

        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }

        //Support: pass the output array size only as big as necessary - we don't need to add here lookback size
        int outputSize = endIndex - startIndex + 1;
       
        double[] tenkanMaxD = new double[outputSize];
        tenkanMax.setInputParameter(0, inputs[0][HIGH]);
        tenkanMax.setOptInputParameter(0, tenkanPeriod);
        tenkanMax.setOutputParameter(0, tenkanMaxD);
        //Support: calculate over the same interval as this indicator, please check if this matches your algorithm
        IndicatorResult dtenkanMaxResult = tenkanMax.calculate(startIndex, endIndex);

        double[] tenkanMinD = new double[outputSize];
        tenkanMin.setInputParameter(0, inputs[0][LOW]);
        tenkanMin.setOptInputParameter(0, tenkanPeriod);
        tenkanMin.setOutputParameter(0, tenkanMinD);
        IndicatorResult dtenkanMinResult = tenkanMin.calculate(startIndex, endIndex);

        double[] stdDevUpOutput = new double[outputSize];
        stdDevUp.setInputParameter(0, inputs[0][HIGH]);
        //Support: you were missing an optional input here, you can find out the necessary ones by
        // running the following example strategy https://www.dukascopy.com/wiki/#Indicator_metadata/Retrieve_indicator_metadata
        stdDevUp.setOptInputParameter(0, stdDevPeriod);
        stdDevUp.setOptInputParameter(1, nbDevUp);
        stdDevUp.setOutputParameter(0, stdDevUpOutput);
        IndicatorResult stdDevUpResult = stdDevUp.calculate(startIndex, endIndex);

        double[] stdDevDownOutput = new double[outputSize];
        stdDevDown.setInputParameter(0, inputs[0][LOW]);
        stdDevDown.setOptInputParameter(0, stdDevPeriod);
        stdDevDown.setOptInputParameter(1, nbDevDown);
        stdDevDown.setOutputParameter(0, stdDevDownOutput);
        IndicatorResult stdDevDownResult = stdDevDown.calculate(startIndex, endIndex);

        //Support: we simplified the indices here, please check if this matches your algorithm
        for (int i = 0; i < outputSize; i++) {
            double tenkanMedian = (tenkanMaxD[i] + tenkanMinD[i]) / 2;
            outputs[0][i] = tenkanMedian;
            outputs[1][i] = tenkanMedian + stdDevUpOutput[i];
            outputs[2][i] = tenkanMedian - stdDevDownOutput[i];
        }


        return new IndicatorResult(startIndex, outputSize);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    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) {
        switch (index) {
        case 0:
            tenkanPeriod = (Integer) value;
            break;
        case 1:
            stdDevPeriod = (Integer) value;
            break;
        case 2:
            nbDevUp = (Double) value;
            break;
        case 3:
            nbDevDown = (Double) value;
            break;
        default:
            throw new ArrayIndexOutOfBoundsException(index);
        }
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }

    public int getLookback() {
        return tenkanPeriod;
    }

    public int getLookforward() {
        return 0;
    }

}


Attachments:
TenkanindBB2.java [7.61 KiB]
Downloaded 352 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Error in indicator: java.lang.IllegalArgumentException Post rating: 0   New post Posted: Mon 01 Jul, 2013, 13:51 

User rating: 1
Joined: Wed 07 Mar, 2012, 05:56
Posts: 101
Location: New CaledoniaNew Caledonia
thank you so much for your reply, i understand my mistake but i still have a problem to understand the output array size, i going to learn again :).

thank you one more time.


 

Jump to:  

  © 1998-2026 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com