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.

Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null
 Post subject: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Sun 24 Jun, 2012, 11:53 
User avatar

User rating: 8
Joined: Tue 25 Oct, 2011, 23:02
Posts: 74
Location: Australia, Melbourne
Anyone know why I'm getting a NPE from running this code?
Basically it appears to call to indicatorsProvder.getIndicator("SAR") fails - don't understand why - I also have the same issue with "ATR" (average true range) - wondering if the these base indicators can be called in a strategy but not through the indicator framework because they come direct from ta-lib and haven't been wrapped...

I'm raising this as a bug because I haven't got any traction on this issue for months.

 private IIndicatorContext context = null;
    private IIndicator PSAR = null; 

    private InputParameterInfo inputParas;
   
    private double accFactor = 0.1;
    private double accLimit = 0.1;
   
    public void onStart(IIndicatorContext context)
    {       
        this.context = context;
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
       
        PSAR = indicatorsProvider.getIndicator("SAR");
               
        indicatorInfo = new IndicatorInfo("PSAR2", "Parabolic SAR", "Overlap Studies", true, false, true, 1, 2, 1);
       
        inputParas = new InputParameterInfo("PSAR Applied Price", InputParameterInfo.Type.DOUBLE);
        inputParas.setAppliedPrice(IIndicators.AppliedPrice.CLOSE);
       
        inputParameterInfos = new InputParameterInfo[] { inputParas };
       
        optInputParameterInfos = new OptInputParameterInfo[]
        {
            new OptInputParameterInfo("Acceleration Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(accFactor, 0.001, 0.50, 0.001, 2)),
            new OptInputParameterInfo("Acceleration Limit", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(accLimit, 0.1, 0.50, 0.005, 2))
        };
       
        outputParameterInfos = new OutputParameterInfo[]
        {
            new OutputParameterInfo("PSAR", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
            {{
                setColor(Color.YELLOW);
            }}
           
        };
    }
   
    public IndicatorResult calculate(int startIndex, int endIndex)
    {

        if (startIndex - getLookback() < 0)
        {
            startIndex -= startIndex - getLookback();
        }
       
        if (startIndex > endIndex)
        {
            return new IndicatorResult(0, 0);
        }   
       
        // next line throws Null Pointer Exception when the indicator is run   
        PSAR.setInputParameter(0, inputs);
        PSAR.setOutputParameter(0, outputs[0]);
        PSAR.setOptInputParameter(0, accFactor);
        PSAR.setOptInputParameter(1, accLimit);
       
        IndicatorResult lrPSAR = PSAR.calculate(startIndex, endIndex);


 
 Post subject: Re: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Mon 25 Jun, 2012, 08:11 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
f451 wrote:
Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null
It does not, you can check it by adding the following lines in the onStart method:
        IIndicatorsProvider ip = context.getIndicatorsProvider();
        context.getConsole().getOut().println(String.format("%s %s", ip.getIndicator("SAR"), ip.getIndicator("ATR")));
Make sure you assign the inputs value in IIndicator.setInputParameter
If you still face some problems, then please provide a full indicator which replicates the issue.


 
 Post subject: Re: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Fri 13 Jul, 2012, 07:38 
User avatar

User rating: 8
Joined: Tue 25 Oct, 2011, 23:02
Posts: 74
Location: Australia, Melbourne
I have found the issue - ATR and SAR indicators require input of type PRICE (i.e. double[][] = new inputs[5][]) otherwise the indicator will produce NPE.

Might be helpful to list which indicators require price or double input (i.e. cannot accept IBar) in the indicator documentation.


 
 Post subject: Re: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Fri 13 Jul, 2012, 08:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
f451 wrote:
Might be helpful to list which indicators require price or double input (i.e. cannot accept IBar) in the indicator documentation.
In fact you can find this out programmatically:
https://www.dukascopy.com/wiki/#Indicator_metadata/Retrieve_indicator_metadata


 
 Post subject: Re: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Fri 30 Oct, 2015, 15:43 
User avatar

User rating: 0
Joined: Wed 17 Apr, 2013, 21:19
Posts: 4
Location: FranceFrance
Hi,
I'm facing the same issue as f451.
I'm trying to build a simple MA over ATR indicator but I have a NullPointerException.
Indeed ATR requires PRICE as input and as a consequence I feed the function setInputParameter with a double[5][] but it still causes the error.
I really don't understand why. Please help me!

public class SMAOverATRIndicator implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][5][];
    private double[][] outputs = new double[2][];
    private IIndicatorContext context;
    private IIndicator atrIndicator;
    private IIndicator smaIndicator;
    private IConsole console;
   
    public void onStart(IIndicatorContext context) {           
        //getting interfaces of ATR and SMA indicators
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        atrIndicator = indicatorsProvider.getIndicator("ATR");
        if ( atrIndicator== null) {
            print("pb");
        }
        smaIndicator = indicatorsProvider.getIndicator("SMA");
        //inicator with one input, two optional params and two outputs
        indicatorInfo = new IndicatorInfo("SMA_ATR", "SMA over ATR", "My indicators",
                false, false, false, false, 1, 2, 2);
        //one input array of PRICE
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.PRICE)};
        //two optional params, one for every indicator
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("ATR Time Period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(14, 2, 5000, 1)), new OptInputParameterInfo("SMA Time Period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(14, 2, 5000, 1))};
        //two output arrays, one for ATR and one for SMA over ATR
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("ATR line", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.LINE), new OutputParameterInfo("SMA line", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.LINE)};
        this.console = context.getConsole();
        //I check the type of input for ATR, this is PRICE as it is done in InputParamterInfo
        printIndicatorInfos(atrIndicator);
        printIndicatorInfos(smaIndicator);
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {       
        //calculating atr
        int atrLookback = atrIndicator.getLookback();
        //first alocate buffer for atr results
        double[] atrOutput;
        if (startIndex > endIndex || atrLookback > endIndex) {   
            return new IndicatorResult(0, 0);
        } else {
            //take the greater value (startindex/ lookback)
            atrOutput = new double[endIndex - (atrLookback > startIndex ? atrLookback : startIndex) + 1];
        }
        //init atr indicator with input data and array for output
        // next line create a NullPointerException and I don't know : inputs[0] is a double[5][] as required for PRICE type!
        atrIndicator.setInputParameter(0, inputs[0]);
        atrIndicator.setOutputParameter(0, atrOutput);
        IndicatorResult atrResult = atrIndicator.calculate(startIndex, endIndex);


Attachments:
SMAOverATRIndicator.java [6.62 KiB]
Downloaded 80 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: Calling indicatorsProvider.getIndicator("SAR") or ("ATR") returns null Post rating: 0   New post Posted: Mon 02 Nov, 2015, 07:58 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Replace the following code
    
public void setInputParameter(int index, Object array) {
        /*if (index==0) {
            inputs[index] = (double[][]) array;
        }*/
}


with
    
public void setInputParameter(int index, Object array) {
        if (index==0) {
            inputs[index] = (double[][]) array;
        }
}


 

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