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.

transfering SAR value to a custom indicator
 Post subject: transfering SAR value to a custom indicator Post rating: 0   New post Posted: Thu 29 Dec, 2011, 00:59 
User avatar

User rating: 1
Joined: Wed 06 Jul, 2011, 23:12
Posts: 42
Location: Romania, Bucharest
Dear API support,

I am trying to create an indicator derived from SAR. For this, I need to pass SAR value to my custom indicator and to perform additional calculation to that value. In order to access values of the SAR indicator I use the framework bellow but unfortunately I can't get it to work.

Can you please look into it and tell me what's wrong? I get this error message:
"23:53:52 Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 2 @ SAR1.calculate(SAR1.java:54)"

Thank you!

import com.dukascopy.api.IBar;
import com.dukascopy.api.ITick;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.indicators.IDrawingIndicator;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IIndicatorDrawingSupport;
import com.dukascopy.api.indicators.IMinMax;
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;
import com.dukascopy.api.indicators.DoubleRangeDescription;
import com.dukascopy.api.indicators.IntegerRangeDescription;

public class SAR1 implements IIndicator
{
   private static final int OPEN = 0;
   private static final int CLOSE = 1;
   private static final int HIGH = 2;
   private static final int LOW = 3;
   //private static final int VOLUME = 4;

    private IIndicator sarIndicator;
    private double[] sar_custom;
       
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
   
    private double[][] inputs = new double[1][];
    private double[][] outputs = new double[1][]; 
   
    public void onStart(IIndicatorContext context)
    {
          indicatorInfo = new IndicatorInfo("SAR1", "SAR1", "Custom", true, false, true, 1, 0, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        sarIndicator = context.getIndicatorsProvider().getIndicator("SAR");
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("SAR1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
                {{
                   setShowValueOnChart(true);
                }},               
        };
    }

    public IndicatorResult calculate(int startIndex, int endIndex)
    {

        int start = startIndex;
        int outputLen = endIndex - start;
        sarIndicator.setInputParameter(0, inputs);
        sarIndicator.setOptInputParameter(0, 0.0003);
        sarIndicator.setOptInputParameter(1, 0.2);
        sar_custom = new double[outputLen];
        sarIndicator.setOutputParameter(0, sar_custom);
        sarIndicator.calculate(startIndex, endIndex);

        for(int i = startIndex; i <= endIndex; i++)
        {
                outputs[0][i] = sar_custom[i];   
        }
       
        return new IndicatorResult(startIndex, endIndex);
    }


    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 int getLookforward() {
        return 0;
    }
   
    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 OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public void setOptInputParameter(int index, Object value) {
        switch (index) {              
         default:
             throw new ArrayIndexOutOfBoundsException(index);
        }
    }

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






 
 Post subject: Re: transfering SAR value to a custom indicator Post rating: 0   New post Posted: Thu 29 Dec, 2011, 13:47 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
This is fixed indicator:

package jforex.indicators;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.IBar;
import com.dukascopy.api.ITick;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.indicators.IDrawingIndicator;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IIndicatorDrawingSupport;
import com.dukascopy.api.indicators.IMinMax;
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;
import com.dukascopy.api.indicators.DoubleRangeDescription;
import com.dukascopy.api.indicators.IntegerRangeDescription;
 
public class SAR1 implements IIndicator
{
   private static final int OPEN = 0;
   private static final int CLOSE = 1;
   private static final int HIGH = 2;
   private static final int LOW = 3;
   //private static final int VOLUME = 4;
 
    private IIndicator sarIndicator;
    private double[] sar_custom;
       
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
     
    private double[][] inputs;
    private double[][] outputs = new double[1][]; 
   
    private IConsole console;
     
    public void onStart(IIndicatorContext context)
    {
        this.console = context.getConsole();
        sarIndicator = context.getIndicatorsProvider().getIndicator("SAR");
       
        // set the parameters of SAR, needed for calculating lookback       
        sarIndicator.setOptInputParameter(0, 0.0003);
        sarIndicator.setOptInputParameter(1, 0.2);
       
        indicatorInfo = new IndicatorInfo("SAR1", "SAR1", "Custom", true, false, true, 1, 0, 1);
       
        // changed to InputParameterInfo.Type.PRICE, since SAR requires PRICE input, not DOUBLE
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.PRICE)};
       
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("SAR1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),               
        };
    }
 
    public IndicatorResult calculate(int startIndex, int endIndex)
    {
        // setting startIndex to take into account the lookback of SAR
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }
        int len = endIndex - startIndex + 1;
       
        sarIndicator.setInputParameter(0, inputs);       
        sar_custom = new double[len];
        sarIndicator.setOutputParameter(0, sar_custom);
        sarIndicator.calculate(startIndex, endIndex);
         
        for (int i = 0; i < len; i++) {
            outputs[0][i] = sar_custom[i];
        }
       
        return new IndicatorResult(startIndex, len);
    }
 
 
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }
 
    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }
 
    // need to set lookback
    public int getLookback() {
        return sarIndicator.getLookback();
    }
 
    public int getLookforward() {
        return 0;
    }
     
    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }
 
    public void setInputParameter(int index, Object array) {
        inputs = (double[][]) array;
    }
 
    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }
 
    public void setOptInputParameter(int index, Object value) {
        switch (index) {             
         default:
             throw new ArrayIndexOutOfBoundsException(index);
        }
    }
 
    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
     
     // ________________________________
    // Support: Some helper methods for printing

    private void print(Object... o) {
        for (Object ob : o) {
            console.getOut().print(ob + "  ");
        }
        console.getOut().println();
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    private void print(double[] arr) {
        print(arrayToString(arr));
    }

    private void print(double[][] arr) {
        print(arrayToString(arr));
    }

    private void printIndicatorInfos(IIndicator ind) {
        for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++) {
            print(ind.getIndicatorInfo().getName() + " Input " + ind.getInputParameterInfo(i).getName() + " " + ind.getInputParameterInfo(i).getType());
        }
        for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++) {
            print(ind.getIndicatorInfo().getName() + " Opt Input " + ind.getOptInputParameterInfo(i).getName() + " " + ind.getOptInputParameterInfo(i).getType());
        }
        for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++) {
            print(ind.getIndicatorInfo().getName() + " Output " + ind.getOutputParameterInfo(i).getName() + " " + ind.getOutputParameterInfo(i).getType());
        }
    }

    public static String arrayToString(double[] arr) {
        String str = "";
        for (int r = 0; r < arr.length; r++) {
            str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; ";
        }
        return str;
    }

    public static String arrayToString(double[][] arr) {
        String str = "";
        if (arr == null)
            return "null";
        for (int r = 0; r < arr.length; r++) {
            for (int c = 0; c < arr[r].length; c++) {
                str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]);
            }
            str += "; ";
        }
        return str;
    }

    public String toDecimalToStr(double d) {
        return (new DecimalFormat("#.#######")).format(d);
    }

    public String dateToStr(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {
            {
                setTimeZone(TimeZone.getTimeZone("GMT"));
            }
        };
        return sdf.format(time);
    }

    // ________________________________
}


Attachments:
SAR1.java [6.83 KiB]
Downloaded 307 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: transfering SAR value to a custom indicator Post rating: 0   New post Posted: Thu 29 Dec, 2011, 14:06 
User avatar

User rating: 1
Joined: Wed 06 Jul, 2011, 23:12
Posts: 42
Location: Romania, Bucharest
Thank you for the fix and for the additional methods!

Happy New Year!

Iulian


 

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