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.

Why is last component of inputs in Indicators usually unused
 Post subject: Why is last component of inputs in Indicators usually unused Post rating: 0   New post Posted: Thu 06 Oct, 2011, 12:27 

User rating: 0
Joined: Thu 06 Oct, 2011, 10:43
Posts: 3
Location: DE
Hello,

I read a lot in the JForex Wiki and Support Board on different indicators. Now, I am wondering why in the calculate-routine of many indicators (e.g. https://www.dukascopy.com/wiki/index.php?title=Simple_Indicator) the element inputs[0][inputs[0].length-1] is usually not considered in the computation.

In case of the above Simple_Indicator the question is also: Why is in the computation of outputs[0][j] the input element input[0][i] not used?

Is there a special reason for that?

Best regards,
red


 
 Post subject: Re: Why is last component of inputs in Indicators usually un Post rating: 0   New post Posted: Fri 07 Oct, 2011, 08:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
red wrote:
the element inputs[0][inputs[0].length-1] is usually not considered in the computation
This is because the last input element mostly corresponds to the currently forming bar (inputs might be bars, prices or price arrays, see InputParameterInfo.Type). Hence, unless the indicator is designed to work with in-progress data, the inputs[0][inputs[0].length-1] value does not get used.

As an example indicator which considers current bar data, see com.dukascopy.indicators.VolumeIndicator.class in JForex Standalone API.

In Simple Indicator the indicator value on current bar is the sum of prices of the previous 2 bars => the current bar's price does not get used => we don't use the inputs[0][inputs[0].length-1] value. To see what happens please consider adding some logging to the indicator:
package jforex.indicators;

import java.text.DecimalFormat;

import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.indicators.*;

public class SimpleIndicatorLog 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 IConsole console;
   
    public void onStart(IIndicatorContext context) {
        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)};
       
        console = context.getConsole();
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
       

        String inputElLog = "input elements: ";
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
                //inputElLog+="take input " + (i - k) + " for output " + j + "; ";
                inputElLog+="take " + (i - k) + "; ";
            }
            outputs[0][j] = value;
        }
        if(endIndex < 100){

            print("calculate, startIndex="+startIndex + " endIndex="+endIndex + " lookback=" + getLookback()
                + " inputs: " + arrayToString(inputs[0]) + " outputs: "  + arrayToString(outputs[0]));
            print(inputElLog);
        }
        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;
    }
   
    private void print(Object o){
        this.console.getOut().println(o);
    }
   
    private void printErr(Object o){
        this.console.getErr().println(o);
    }
   
    public static String arrayToString(IBar [] arr){
        String str = "";
        for (int r=0; r<arr.length; r++) {
            str += "[" + r + "] "+ arr[r] + "; ";
        }
        return str;
     }
   
    public static String arrayToString(double [] arr){
        String str = "";
        for (int r=0; r<arr.length; r++) {
            str += "[" + r + "] "+ (new DecimalFormat("0.00000")).format(arr[r]) + "; ";
        }
        return str;
     }
   
    public static String arrayToString(double [][] arr){
        String str = "";
        for (int r=0; r<arr.length; r++) {
            for (int c=0; c<arr[r].length; c++) {
                str += " [" + r + "][" + c + "] " + (new DecimalFormat("0.00000")).format(arr[r][c]);
            }
            str += "; ";
        }
        return str;
    }
}


 
 Post subject: Re: Why is last component of inputs in Indicators usually un Post rating: 0   New post Posted: Fri 07 Oct, 2011, 09:50 

User rating: 0
Joined: Thu 06 Oct, 2011, 10:43
Posts: 3
Location: DE
Thanks a lot for your reply!

I was just confused because in Simple Indicator InputParameterInfo.Type.DOUBLE (not PRICE) is used but the decision whether the last element of inputs[0] is meaningful is left to the indicator but not to the used strategy. In my opinion this should be a decision of the strategy which may call the indicator with shift=0 or shift=1.

What is inputs[0][inputs[0].length-1] for the case that the indicator is used with Period.TICK and shift=0? Is it a already formed bar/tick?

Is the last element of inputs[0] (usually) empty (=0?) when a strategy calls that indicator immediately after its onBar method is called (shift=0)?

Is there a important difference between the onTick and onBar method of a strategy if in the onBar method the Period.TICK is filtered?

red


 
 Post subject: Re: Why is last component of inputs in Indicators usually un Post rating: 0   New post Posted: Fri 07 Oct, 2011, 11:15 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
red wrote:
I was just confused because in Simple Indicator InputParameterInfo.Type.DOUBLE (not PRICE) is used
The type name is DOUBLE (not, for example, SINGLEPRICE or something else) is given on purpose to denote that the values that the user passes to the indicator need not necessarily be prices, they can also be some calculated values of another indicator which have nothing to do with prices, see:
https://www.dukascopy.com/wiki/index.php ... _indicator
And as another example, see:
viewtopic.php?f=6&t=42385&p=54313#p54313
red wrote:
In my opinion this should be a decision of the strategy which may call the indicator with shift=0 or shift=1.
This has nothing to do with the indicator "imposing" the shift - not considering the not-finished-bar's values is rather a common approach, thus this is "as designed" for the particular indicator. In general, at decision points one can always introduce and apply additional optional inputs.
red wrote:
What is inputs[0][inputs[0].length-1] for the case that the indicator is used with Period.TICK and shift=0? Is it a already formed bar/tick?
For indicators Period.TICK is applied a bit differently than for strategies - for indicators it is used as if it were a ONE_SEC bar.
red wrote:
Is the last element of inputs[0] (usually) empty (=0?) when a strategy calls that indicator immediately after its onBar method is called (shift=0)?
No it is not empty, it is the same value that you would see at the given moment on the chart (of course if the indicator was plotted on it).
red wrote:
Is there a important difference between the onTick and onBar method of a strategy if in the onBar method the Period.TICK is filtered?
No, the results of indicator calculation depend only on inputs and optional inputs and other strategy characteristics don't affect them.


 

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