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.

Problem with a indikator
 Post subject: Problem with a indikator Post rating: 0   New post Posted: Sun 19 Jun, 2011, 19:12 

User rating: -
I programmed an indicator. But unfortunately I beckomme after the execution this error message.
( 17:29:13 Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 1 @ jforex.SMAOverCCI.calculate(SMAOverCCI.java:61) ).

The SMA is calculated over the CCI.

I am very grateful if you could find the solution to the problem.

Thank you
//------------------------------------------------------------------------------------
package jforex;

import com.dukascopy.api.indicators.*;
import java.util.*;
import com.dukascopy.api.*;

public class SMAOverCCI 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[2][];
    private IIndicatorContext context;
    private IIndicator cciIndicator;
    private IIndicator smaIndicator;
 
 
   
   
    public void onStart(IIndicatorContext context) {
   
       
         // print("-----------------FEHLER FEHLER FEHELER--------------- " );
                 
        //getting interfaces of CCI and SMA indicators
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        cciIndicator = indicatorsProvider.getIndicator("CCI");
        smaIndicator = indicatorsProvider.getIndicator("SMA");
        //inicator with one input, two optional params and two outputs
        indicatorInfo = new IndicatorInfo("SMA_CCI", "SMA over CCI", "My indicators",
                false, false, false, false, 1, 2, 2);
        //one input array of doubles
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        //two optional params, one for every indicator
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("CCI Time Period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(18, 0, 1000, 1)), new OptInputParameterInfo("SMA Time Period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(16, 0, 1000, 1))};
        //two output arrays, one for RSI and one for SMA over CCI
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("CCI line", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.LINE), new OutputParameterInfo("SMA line", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
           
        //calculating cci
        int cciLookback = cciIndicator.getLookback();
        //first alocate buffer for cci results
        double[] cciOutput;
        if (startIndex > endIndex || cciLookback > endIndex) {   
           
            return new IndicatorResult(0, 0);
           
        } else {
            //take the greater value (startindex/ lookback)
           cciOutput = new double[endIndex - (cciLookback > startIndex ? cciLookback : startIndex) + 1];
         
        }
        //init cci indicator with input data and array for output
        cciIndicator.setInputParameter(0, inputs[0]);
        cciIndicator.setOutputParameter(0, cciOutput);
        IndicatorResult cciResult = cciIndicator.calculate(startIndex, endIndex);
        if (cciResult.getNumberOfElements() < smaIndicator.getLookback()) {
            //not enough data to calculate sma
            return new IndicatorResult(0, 0);
        }
       
        //calculating sma
        smaIndicator.setInputParameter(0, cciOutput);
        smaIndicator.setOutputParameter(0, outputs[1]);
        IndicatorResult smaResult = smaIndicator.calculate(0, cciResult.getNumberOfElements() - 1);
        if (smaResult.getNumberOfElements() == 0) {
            //sma returned 0 values
            return new IndicatorResult(0, 0);
        }
        //copy rsi values to output excluding first values used for sma lookback
        System.arraycopy(cciOutput, smaResult.getFirstValueIndex(), outputs[0], 0, smaResult.getNumberOfElements());
        //creating result, first value index for our input is FVI for rsi + FVI for sma, because we calculated sma starting from 0 element
        IndicatorResult result = new IndicatorResult(cciResult.getFirstValueIndex() + smaResult.getFirstValueIndex(), smaResult.getNumberOfElements());
        return result;
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

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

    public int getLookback() {
        return cciIndicator.getLookback() + smaIndicator.getLookback();
    }

 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) {
        //set optional params in indicators
        switch (index) {
            case 0:
                cciIndicator.setOptInputParameter(0, value);
                break;
            case 1:
                smaIndicator.setOptInputParameter(0, value);
                break;
        }
    }

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


 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Tue 21 Jun, 2011, 09:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please have a look at the following:
package jforex.indicators;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle;

public class SMAOverCCI 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[2][];
   private IIndicatorContext context;
   private IIndicator cciIndicator;
   private IIndicator smaIndicator;

   public void onStart(IIndicatorContext context) {
   
   
      // print("-----------------FEHLER FEHLER FEHELER--------------- " );
      
      //getting interfaces of CCI and SMA indicators
      IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
      cciIndicator = indicatorsProvider.getIndicator("CCI");
      smaIndicator = indicatorsProvider.getIndicator("SMA");
      //inicator with one input, two optional params and two outputs
      indicatorInfo = new IndicatorInfo("SMA_CCI", "SMA over CCI", "My indicators", false, false, false, 1, 2, 2);
      //one input array of doubles
      inputParameterInfos = new InputParameterInfo[] {            
            new InputParameterInfo("Input CCI", InputParameterInfo.Type.PRICE)};
      //two optional params, one for every indicator
      optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("CCI Time Period", OptInputParameterInfo.Type.OTHER,
      new IntegerRangeDescription(18, 0, 1000, 1)), new OptInputParameterInfo("SMA Time Period", OptInputParameterInfo.Type.OTHER,
      new IntegerRangeDescription(16, 0, 1000, 1))};
      //two output arrays, one for RSI and one for SMA over CCI
      outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("CCI line", OutputParameterInfo.Type.DOUBLE, DrawingStyle.LINE),
            new OutputParameterInfo("SMA line", OutputParameterInfo.Type.DOUBLE, DrawingStyle.LINE)};
   }

   public IndicatorResult calculate(int startIndex, int endIndex) {

      //calculating cci
      int cciLookback = cciIndicator.getLookback();
      //first alocate buffer for cci results
      double[] cciOutput;
      if (startIndex > endIndex || cciLookback > endIndex) {
         return new IndicatorResult(0, 0);
      } else {
         //take the greater value (startindex/ lookback)
         cciOutput = new double[endIndex - (cciLookback > startIndex ? cciLookback : startIndex) + 1];
      }
      //init cci indicator with input data and array for output
      cciIndicator.setInputParameter(0, inputs[0]);
      cciIndicator.setOutputParameter(0, cciOutput);
      IndicatorResult cciResult = cciIndicator.calculate(startIndex, endIndex);
      if (cciResult.getNumberOfElements() < smaIndicator.getLookback()) {
         //not enough data to calculate sma
         return new IndicatorResult(0, 0);
      }

      //calculating sma
      smaIndicator.setInputParameter(0, cciOutput);
      smaIndicator.setOutputParameter(0, outputs[1]);
      IndicatorResult smaResult = smaIndicator.calculate(0, cciResult.getNumberOfElements() - 1);
      if (smaResult.getNumberOfElements() == 0) {
         //sma returned 0 values
         return new IndicatorResult(0, 0);
      }
      //copy rsi values to output excluding first values used for sma lookback
      System.arraycopy(cciOutput, smaResult.getFirstValueIndex(), outputs[0], 0, smaResult.getNumberOfElements());
      //creating result, first value index for our input is FVI for rsi + FVI for sma, because we calculated sma starting from 0 element
      IndicatorResult result = new IndicatorResult(cciResult.getFirstValueIndex() + smaResult.getFirstValueIndex(), smaResult.getNumberOfElements());
      
      return result;
   }

   public IndicatorInfo getIndicatorInfo() {
      return indicatorInfo;
   }

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

   public int getLookback() {
      return cciIndicator.getLookback() + smaIndicator.getLookback();
   }
   
   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) {
      //set optional params in indicators
      switch (index) {
         case 0:
         cciIndicator.setOptInputParameter(0, value);
         break;
         case 1:
         smaIndicator.setOptInputParameter(0, value);
         break;
      }
   }
   
   public void setOutputParameter(int index, Object array) {
      outputs[index] = (double[]) array;
   }
}



 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Wed 22 Jun, 2011, 18:27 

User rating: 2
Joined: Wed 18 May, 2011, 17:36
Posts: 49
Location: FranceFrance
strange....! :?
I get 12 errors with this indicator....


 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Thu 23 Jun, 2011, 07:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
MATH06 wrote:
strange....! :?
I get 12 errors with this indicator....
Please elaborate


 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Thu 23 Jun, 2011, 18:43 

User rating: 2
Joined: Wed 18 May, 2011, 17:36
Posts: 49
Location: FranceFrance
Hi,
sorry, I paste the code again and it compiles now (no errors).

Cheers,

Math


 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Wed 13 Jul, 2011, 19:53 

User rating: -
Hello
If I want to use this indicator in a strategy. How can I call him then?

Thank you


 
 Post subject: Re: Problem with a indikator Post rating: 0   New post Posted: Thu 14 Jul, 2011, 07:19 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://www.dukascopy.com/wiki/index.php ... strategies


 

Jump to:  

cron
  © 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