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.

time shifted indicator value in IIndicator
 Post subject: time shifted indicator value in IIndicator Post rating: 0   New post Posted: Tue 28 Sep, 2010, 03:14 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
I am trying to write an indicator where outputs[i] = sma[i - shift], such that the output is a SMA lagging by 'shift' number of bars. Here's my code in calculate():

        double[] smaData = new double[endIndex - startIndex + 1 + timePeriod];
        sma.setInputParameter(0, inputs[0]);
        sma.setOptInputParameter(0, timePeriod);
        sma.setOutputParameter(0, smaData);
        IndicatorResult dSmaResult = sma.calculate(startIndex - timePeriod, endIndex);       

        int i, k, j;
        k = dSmaResult.getNumberOfElements();
        j = dAtrResult.getNumberOfElements();
               
        for (i = 0; i < Math.min(k, j); i++) {
           outputs[0][i] = smaData[i + timePeriod];
        }
       
        return new IndicatorResult(startIndex, i);


The problem though, is that instead of receiving lagging bars, the outputs are shifted forward. How would I fix this please?


 
 Post subject: Re: time shifted indicator value in IIndicator Post rating: 0   New post Posted: Tue 28 Sep, 2010, 14:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Try to change to
        for (i = 0; i < Math.min(k - 1, j); i++) {
           outputs[0][i] = smaData[i + timePeriod + 1];
        }
        outputs[0][i] = Double.NaN
        i++;

In this case, to calculate value for the current candle, you need to know sma value for the next candle. For correct calculations on the chart you will need to set lookforward to 1 (getLookforward() method must return 1)

If the shifting of the output on the chart is the only thing you want to get, then consider setting shift parameter to the output. Setting it to +1 or -1 will shift it by one candle


 

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