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.

how do I get shifted ma values in IIndicator?
 Post subject: how do I get shifted ma values in IIndicator? Post rating: 0   New post Posted: Thu 29 May, 2014, 20:11 
User avatar

User rating: 0
Joined: Mon 17 Mar, 2014, 19:04
Posts: 2
Location: Italy, San Bartolomeo in Galdo
Hello, i'm trying to access previous values of moving average but i'm having a hard time. Let's take the following code as an example:

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators;

public class Provando implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];
    private IIndicator ma;
    private IIndicatorsProvider indicatorsProvider;
   
    public void onStart(IIndicatorContext context) {
        indicatorsProvider = context.getIndicatorsProvider();
        ma = indicatorsProvider.getIndicator("SMA");
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
                false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        double[] maOutput = new double[endIndex - startIndex + 1 ];
        ma.setInputParameter(0, inputs[0]);
        ma.setOutputParameter(0, maOutput);
        ma.setOptInputParameter(0, timePeriod);
        ma.calculate(startIndex, endIndex);
       
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k]- maOutput[i - k];
            }
            outputs[0][j] = value;
        }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

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

    public int getLookback() {
        return timePeriod;
    }

    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) {
        timePeriod = (Integer) value;
    }

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


I'm a newbie, please try to be comprehensive.

Regards, Paolo.


 
 Post subject: Re: how do I get shifted ma values in IIndicator? Post rating: 0   New post Posted: Fri 30 May, 2014, 09:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        double[] maOutput = new double[endIndex - startIndex + 1 ];
        ma.setInputParameter(0, inputs[0]);
        ma.setOutputParameter(0, maOutput);
        ma.setOptInputParameter(0, timePeriod);
        ma.calculate(startIndex, endIndex);
         
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            /**
             * We added here two extra conditions for range checking,
             * please make sure you either change logic or that the range check arguments
             * make sense to your algorithm
             */
            for (int k = timePeriod; k > 0 && i - k >=0 && j - k >=0; k--) {
               /**
                * you use i for iterating over inputs
                * you use j for iterating over outputs
                *
                */
                value += inputs[0][i - k]- maOutput[j - k];
            }
            outputs[0][j] = value;
        }
        return new IndicatorResult(startIndex, j);
    }
 
To understand more about how the indexes work and the array sizes change, consider adding some logging to your indicator as it is done in the given example in the second post of the following topic:
viewtopic.php?t=48828&f=65


 
 Post subject: Re: how do I get shifted ma values in IIndicator? Post rating: 0   New post Posted: Fri 30 May, 2014, 14:32 
User avatar

User rating: 0
Joined: Mon 17 Mar, 2014, 19:04
Posts: 2
Location: Italy, San Bartolomeo in Galdo
Thank you very much for the quick and exhaustive reply.

Paolo


 

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