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.

Input arrays different lengths
 Post subject: Input arrays different lengths Post rating: 0   New post Posted: Fri 10 Apr, 2015, 08:37 
User avatar

User rating: 0
Joined: Fri 10 Oct, 2014, 00:35
Posts: 58
Location: New Zealand, Auckland
Hi

I am struggling with a simple indicator using 2 inputs, which gives the following error when I change instrument or time period:

Image

I have studied the Wiki example with multiple inputs, but checking for null array does not solve the problem.

I assumed there would always be one array element for every candle that the indicator is calculated for, but are the input arrays supposed to be the same length?

My onStart() & calculate() code is below.

I would appreciate any advice.

Cheers
Tony

    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("Spread", "Plots the bid-ask spread", "My indicators", false, false, false, 2, 0, 1)
            {{ setRecalculateOnNewCandleOnly( true ); }};
        inputParameterInfos = new InputParameterInfo[] {
            new InputParameterInfo("Bid bar data", InputParameterInfo.Type.BAR) {{ setOfferSide(OfferSide.BID); }},
            new InputParameterInfo("Ask bar data", InputParameterInfo.Type.BAR) {{ setOfferSide(OfferSide.ASK); }}
            };
        outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
            };
       
        this.console = context.getConsole();
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        if (startIndex < 0) {    // not sure if this is necesary
            startIndex = 0;
        }
       
        console.getOut().println( String.format( "inputs[0].length = " + inputs[0].length + ", inputs[1].length = " + inputs[1].length ) );
       
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            if ( inputs[1] != null ) {    // check that 2nd input (ask bars) is fully loaded
                outputs[0][j] = inputs[1][i].getClose() - inputs[0][i].getClose();    // ask close - bid close
            }
            else {    // not fully loaded yet
                outputs[0][j] = Double.NaN;
                console.getOut().println( "2nd input array is not fully loaded." );
            }
        }
       
        return new IndicatorResult(startIndex, j);
    }


Attachments:
Input_Array_Lengths_1.png [20.2 KiB]
Downloaded 296 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: Input arrays different lengths Post rating: 0   New post Posted: Fri 10 Apr, 2015, 15:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
JForex Wiki says:
Indicators always have at least one input (main input). Main input's instrument, period and offer side are chart's instrument, period and offer side. Even if the main input wasn't defined in the indicator, it still exists.
Indicator's calculate method receives indexes for this main input. (See IIndicator interface section for more details)
When custom inputs are used in the indicator, the following rules apply:
Chart data is loaded from the disk or internet and is ready to be displayed on the chart. Data for indicator's custom inputs may not be fully loaded yet.
As soon as main input's data is fully loaded it is passed to the indicator.
When data for other inputs is fully loaded it is passed to the indicator which then gets recalculated.
(https://www.dukascopy.com/wiki/#Indicat ... ple_inputs)

This means that there may be a situation when input arrays are of different length.
One workaround could be to skip the calculation if input arrays don't have equal length.


 
 Post subject: Re: Input arrays different lengths Post rating: 0   New post Posted: Mon 13 Apr, 2015, 23:37 
User avatar

User rating: 0
Joined: Fri 10 Oct, 2014, 00:35
Posts: 58
Location: New Zealand, Auckland
Thank you for your response.

I misunderstood the wiki article (https://www.dukascopy.com/wiki/#Indicator_with_multiple_inputs). It says that the indicator get recalculated when the data for the other inputs is fully loaded, but I wrongly assumed that recalculation would not occur until the other data is fully loaded.

I think it would be useful for others if an additional comment was added to the wiki article to say that recalculation may also occur earlier, before the data is fully loaded. Please consider this.

I offer this code snippet for others experiencing this issue:

    public IndicatorResult calculate(int startIndex, int endIndex) {
        if (startIndex < 0) {    // not sure if this is necesary
            startIndex = 0;
        }
       
        // calculate values for the available input data
        int i = startIndex;    // input index
        int j = 0;             // output index
        if ( inputs[1] != null ) {
            int availableRange = Math.min( inputs[0].length, inputs[1].length ) - startIndex;    // input arrays may be different lengths
           
//            console.getOut().println( "startIndex = " + startIndex + ", endIndex = " + endIndex
//                                    + ", inputs[0].length = " + inputs[0].length + ", inputs[1].length = " + inputs[1].length
//                                    + ", availableRange = " + availableRange
//                                    );
            while ( i <= endIndex && j < availableRange ) {
                outputs[0][j] = inputs[1][i].getClose() - inputs[0][i].getClose();    // ask close - bid close
                i++;
                j++;
            }
        }
//        if ( i <= endIndex ) {
//            console.getOut().println( "An input array is not fully loaded." );
//        }

        // fill any remaining requested values with Double.NaN
        while ( i <= endIndex ) {
            outputs[0][j] = Double.NaN;
            i++;
            j++;
        }
       
        return new IndicatorResult(startIndex, j);
    }


Regards
Tony


 

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