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.

ArrayIndexOutOfBoundsException
 Post subject: ArrayIndexOutOfBoundsException Post rating: 0   Post Posted: Wed 07 Sep, 2011, 17:17 
User avatar

User rating: 0
Joined: Wed 07 Sep, 2011, 16:30
Posts: 7
Location: China,
Hi, support,

In my Indicators, can get basis value, but can't get the average value.

For example, the following codes. The Messages show:"14:16:19 Error in indicator: java.lang.ArrayIndexOutOfBoundsException: -1 @ jforex.BarFour2.calculate(BarFour2.java:66)".

If the output "outputs[1][resIndex] = 2*AB[i];", can get the right result.
If the output "outputs[1][resIndex] = x3/3;", will an error has occurred.

would you please tell me how to get the correct "x3 += AB[j]" ?

Thank you very much.

package jforex;

import com.dukascopy.api.indicators.*;

//+===================================================================================+

public class BarFour2 implements IIndicator
{
    public static final int OPEN  = 0;
    public static final int CLOSE = 1;
    public static final int MIN   = 2;
    public static final int MAX   = 3;
    public static final int AVE   = 4;

    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[2][];
       
  //+---------------------------------------------------------------------------------+
    public void onStart(IIndicatorContext context)
    {
        indicatorInfo = new IndicatorInfo("ABsum", "ABsum", "Custom", false, true, false, 1, 0, 2);
        inputParameterInfos = new InputParameterInfo[]
        {
            new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)
        };

        outputParameterInfos = new OutputParameterInfo[]
        {
          new OutputParameterInfo("Line1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),
          new OutputParameterInfo("Line2", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
        };
    }
  //+--------------------------------------------------------------------------------+
    public IndicatorResult calculate(int startIndex, int endIndex)
    {
        if (startIndex - getLookback() < 0)
        {
            startIndex -= startIndex - getLookback();
        }
        if (startIndex > endIndex)
        {
            return new IndicatorResult(0, 0);
        }

        double[] AB = new double[endIndex - startIndex + 1];
        int resIndex = 0;
        for (int i = startIndex; i <= endIndex; i++, resIndex++)
        {
            double[] candle = new double[4];
           
            candle[OPEN]  = inputs[0][0][i];
            candle[CLOSE] = inputs[0][1][i] ;
            candle[MAX]   = inputs[0][2][i];
            candle[MIN]   = inputs[0][3][i];
           
            AB[i] = (inputs[0][2][i] - inputs[0][3][i])/2;
           
            outputs[0][resIndex] = AB[i];
           
            double x3 = 0;
            for(int j=i; j>=i-2; j--)
            {
                x3 += AB[j];
            }
                               
            outputs[1][resIndex] = x3/3;        //2*AB[i];
           
        }
        return new IndicatorResult(startIndex, resIndex);
    }
  //+--------------------------------------------------------------------------------+
    public IndicatorInfo getIndicatorInfo()
    {
        return indicatorInfo;
    }
   
  //+--------------------------------------------------------------------------------+
    public InputParameterInfo getInputParameterInfo(int index)
    {
        if (index <= inputParameterInfos.length)
        {
            return inputParameterInfos[index];
        }
        return null;
    }

  //+--------------------------------------------------------------------------------+
    public int getLookback()
    {
        return 0;
    }

  //+--------------------------------------------------------------------------------+
    public OutputParameterInfo getOutputParameterInfo(int index)
    {
        if (index <= outputParameterInfos.length)
        {
            return outputParameterInfos[index];
        }
        return null;
    }

  //+--------------------------------------------------------------------------------+
    public void setInputParameter(int index, Object array)
    {
        inputs[0] = (double[][]) array;
    }

  //+--------------------------------------------------------------------------------+
    public OptInputParameterInfo getOptInputParameterInfo(int index)
    {
        return null;
    }

  //+--------------------------------------------------------------------------------+
    public void setOptInputParameter(int index, Object value)
    {

    }

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

  //+--------------------------------------------------------------------------------+
    public int getLookforward()
    {
        return 0;
    }
}
//+////////////////////////////////////////////////////////////////////////////+


 
 Post subject: Re: ArrayIndexOutOfBoundsException Post rating: 0   Post Posted: Thu 08 Sep, 2011, 13:00 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please note that indicator output array is shorter than indicator input:
indicator output index = indicator input index - lookback
For more information on indicators see https://www.dukascopy.com/wiki/index.php ... _interface.

Please consider the following code:

package jforex.indicators;

import com.dukascopy.api.IConsole;
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.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

//+===================================================================================+

public class BarFour2 implements IIndicator {
   public static final int OPEN = 0;
   public static final int CLOSE = 1;
   public static final int MIN = 2;
   public static final int MAX = 3;
   public static final int AVE = 4;

   private IndicatorInfo indicatorInfo;
   private InputParameterInfo[] inputParameterInfos;
   private OutputParameterInfo[] outputParameterInfos;
   private double[][][] inputs = new double[1][][];
   private double[][] outputs = new double[2][];
   
   private int lookback = 2;

   IConsole console;

   // +---------------------------------------------------------------------------------+
   public void onStart(IIndicatorContext context) {
      this.console = context.getConsole();

      indicatorInfo = new IndicatorInfo("ABsum", "ABsum", "Custom", false,
            true, false, 1, 0, 2);
      inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo(
            "Price", InputParameterInfo.Type.PRICE) };

      outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("Line1",
                  OutputParameterInfo.Type.DOUBLE,
                  OutputParameterInfo.DrawingStyle.LINE),
            new OutputParameterInfo("Line2",
                  OutputParameterInfo.Type.DOUBLE,
                  OutputParameterInfo.DrawingStyle.LINE) };
   }

   // +--------------------------------------------------------------------------------+
   public IndicatorResult calculate(int startIndex, int endIndex) {
      if (startIndex - getLookback() < 0) {
         startIndex -= startIndex - getLookback();
      }
      if (startIndex > endIndex) {
         return new IndicatorResult(0, 0);
      }

      int resLen = endIndex - startIndex + 1;
      double[] AB = new double[resLen];

      //print("startIndex=" + startIndex + " endIndex=" + endIndex);

      for (int i = 0; i < resLen; i++) {
         int inputIndex = i + getLookback();

         double[] candle = new double[4];

         candle[OPEN] = inputs[0][0][inputIndex];
         candle[CLOSE] = inputs[0][1][inputIndex];
         candle[MAX] = inputs[0][2][inputIndex];
         candle[MIN] = inputs[0][3][inputIndex];

         outputs[0][i] = (inputs[0][2][inputIndex] - inputs[0][3][inputIndex]) / 2;

         double x3 = 0;
         for (int j = i; j <= i + getLookback(); j++){
            x3 += (inputs[0][2][j] - inputs[0][3][j]) / 2;
         }
         
         outputs[1][i] = x3 / (getLookback() + 1); // 2*AB[i];
      }
      return new IndicatorResult(startIndex, resLen);
   }

   // +--------------------------------------------------------------------------------+
   public IndicatorInfo getIndicatorInfo() {
      return indicatorInfo;
   }

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

   // +--------------------------------------------------------------------------------+
   public int getLookback() {
      return lookback;
   }

   // +--------------------------------------------------------------------------------+
   public OutputParameterInfo getOutputParameterInfo(int index) {
      if (index <= outputParameterInfos.length) {
         return outputParameterInfos[index];
      }
      return null;
   }

   // +--------------------------------------------------------------------------------+
   public void setInputParameter(int index, Object array) {
      inputs[0] = (double[][]) array;
   }

   // +--------------------------------------------------------------------------------+
   public OptInputParameterInfo getOptInputParameterInfo(int index) {
      return null;
   }

   // +--------------------------------------------------------------------------------+
   public void setOptInputParameter(int index, Object value) {

   }

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

   // +--------------------------------------------------------------------------------+
   public int getLookforward() {
      return 0;
   }
   
    private void print(Object o) {
        this.console.getOut().println(o);
    }
   
}
// +////////////////////////////////////////////////////////////////////////////+


Attachments:
BarFour2.java [4.66 KiB]
Downloaded 295 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.
 

Jump to:  

  © 1998-2025 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