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.

Indicator works then returns 0
 Post subject: Indicator works then returns 0 Post rating: 0   New post Posted: Tue 19 Feb, 2013, 05:56 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Hi, all. I'm having trouble doing something that should be very simple, which is to set up and use arrays other than the inputs and outputs. I'd like to perform calculations on the underlying inputs and store the calculations in arrays and perform a regression on the calculated values. The problem is that I'm new to programming.

I realize that there is code on the Wiki page which utilizes outputs from other indicators, but it will be much easier for me to use my own arrays because this could get pretty complicated.

I'm trying to do something very simple and I've gotten it to work other than the fact that, once the indicator is run in the chart, the last output and all following outputs (once new bars are created) are 0. I'm not sure why that might be. Here's the code which aims to store the price of each bar in the holders[][] array and then shift the prices forward 3 periods (so call the price 3 periods ago) between the holders[][] and outputs[][] arrays.

package jforex;

import com.dukascopy.api.indicators.*;

public class HolderTest2 implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private int timePeriod = 3;
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("Holder Test2", "Holder Test2", "My indicators",
                false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.PRICE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(3, 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();
        }
       
        int length = endIndex - startIndex + 1;
        double[][] holder = new double[1][length];
       
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {     
            holder[0][j] = inputs[0][2][i];
        }
       
        int y, z;
        for (y = startIndex + 3, z = 3; y <= endIndex; y++, z++) {
            outputs[0][z] = holder[0][z - 3];
        }
        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;
    }
}


The top indicator in the two pictures simply pulls the price for the current bar. The code above is for the bottom indicator. The first picture is what the indicator looks like when showing the last few bars. As you can see, the indicator returns 0 for several periods. The second picture is what it looks like before that, when I scroll backwards in time - it appears to be working exactly like I had hoped.

Any help?
Image
Image


Attachments:
Indicator Error2.jpg [359.41 KiB]
Downloaded 753 times
Indicator Error.jpg [331.82 KiB]
Downloaded 728 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: Indicator works then returns 0 Post rating: 0   New post Posted: Tue 19 Feb, 2013, 09:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You are wrongly processing the output array - its values get assigned only in case of full recalculation. We added logging statements to your indicator, such that you can see what is happening with the value arrays:


Attachments:
HolderTestLog.java [3.81 KiB]
Downloaded 370 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: Indicator works then returns 0 Post rating: 0   New post Posted: Wed 20 Feb, 2013, 07:02 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Can you explain this further? When the indicator first runs, it shows the values held in 10 indexes in the outputs array, but soon it drops to just 1. I'm not sure how this relates to my problem or how it can help me fix it. Printing the array information to the console doesn't explain why it works at first and then changes.

Also, is it possible for me to export my indicator values to a text or Excel file? I'll figure it out - I'm just making sure that JForex doesn't restrict this somehow.


 
 Post subject: Re: Indicator works then returns 0 Post rating: 0   New post Posted: Wed 20 Feb, 2013, 08:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
JHogg11 wrote:
Can you explain this further?
Run the logging indicator and change the timePeriod (lookback) parameter, you will see that the input size depends on it and not the outputs. Consider having a look at the example indicator (the one that gets created in the platform when you choose "new indicator") and checking how multiple previous inputs get used to calculate the output for a candle.
JHogg11 wrote:
When the indicator first runs, it shows the values held in 10 indexes in the outputs array, but soon it drops to just 1. I'm not sure how this relates to my problem or how it can help me fix it. Printing the array information to the console doesn't explain why it works at first and then changes.
It should help you understand how wrong assumptions about input, output array lengths cause the last calculated value to be 0.
JHogg11 wrote:
Also, is it possible for me to export my indicator values to a text or Excel file? I'll figure it out - I'm just making sure that JForex doesn't restrict this somehow.
See:
https://www.dukascopy.com/wiki/#Write_in_Excel_(DDE)


 
 Post subject: Re: Indicator works then returns 0 Post rating: 0   New post Posted: Thu 28 Feb, 2013, 03:31 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Because I'm not very knowledgeable and I don't want to waste hours if it's impossible, can I use another method of exporting to Excel such as Apache? I'd like to be able to write my indicator outputs to Excel and I'm having trouble successfully transferring the code from the UpdateExcel strategy to my indicator. If a template were available for indicators, that would be great. Thanks.


 
 Post subject: Re: Indicator works then returns 0 Post rating: 0   New post Posted: Thu 28 Feb, 2013, 08:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
An alternative would be writing data to csv files and then importing them in excel.


 

Jump to:  

  © 1998-2026 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