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 to calculate an indicator value
 Post subject: How to calculate an indicator value Post rating: 0   New post Posted: Mon 10 Mar, 2014, 13:05 

User rating: 0
Joined: Tue 28 Jan, 2014, 16:45
Posts: 9
Location: Spain,
I'm trying to calculate an indicator that is the addition of two values, like this:

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


Of course "value[0] = sprdResult.getFirstValueIndex() - inputs[0];" is not working. I have not figured out the way to subtract a value from the first indicator. Could you provide a clean way to do this?

Best regards,
Bernabe


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Mon 10 Mar, 2014, 17:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
could you please provide a full example indicator?


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Mon 10 Mar, 2014, 18:41 

User rating: 0
Joined: Tue 28 Jan, 2014, 16:45
Posts: 9
Location: Spain,
Here you go:

package jforex;
 
import com.dukascopy.api.indicators.*;
 
public class SpreadmedIndi implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int[] timePeriod = new int[3];
    private double[][] outputs = new double[3][];
    private double value[] = new double[2014];
    private IIndicator sprd;
 
    public void onStart(IIndicatorContext context) {
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        sprd = indicatorsProvider.getIndicator("LaloIndi");
        indicatorInfo = new IndicatorInfo("Spreadmed", "Spread to Median", "LF indicators",
                        false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period Spreadmed", OptInputParameterInfo.Type.OTHER,
        new IntegerRangeDescription(54, 2, 1000, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("Spreadmed", 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 i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
 
            sprd.setInputParameter(0, inputs[0]);
   
            //calculate first ema
            sprd.setOptInputParameter(0, timePeriod[0]);
            sprd.setOutputParameter(0, outputs[0]);
 
            IndicatorResult sprdResult = sprd.calculate(startIndex, endIndex);                     
 
            value[0] = sprdResult.getFirstValueIndex() - inputs[0];
        }
     
        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() {
        sprd.setOptInputParameter(0, timePeriod[0]);
        int sprdLookback = sprd.getLookback();
        return sprdLookback;
    }
 
    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[index] = (Integer) value;
    }
 
    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 11 Mar, 2014, 09:23 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The indicator does not even compile (for the particular case you need to use 2 inputs array dimensions instead of one). For instant assistance for compile-time errors, consider using some IDE:
https://www.dukascopy.com/wiki/#Use_in_Eclipse
https://www.dukascopy.com/wiki/#Use_in_NetBeans
Secondly "LaloIndi" is a custom indicator, for further inquiries please also provide that indicator.


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 11 Mar, 2014, 09:49 

User rating: 0
Joined: Tue 28 Jan, 2014, 16:45
Posts: 9
Location: Spain,
I know the indicator does not even compile. I'm not really familiar with Java, nor with the Jforex logic. So this is a very raw learning curve.

As I know inputs is holding the prices (open, close, high, low). I can't figure out how to use two dimensions to solve my problem. Could you be more concise?

Sometimes it helps to see an example of what you want to do in order to learn how to do it. I will appreciate any help.

Best regards
Bernabe


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 11 Mar, 2014, 10:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Bernabe wrote:
I know the indicator does not even compile. I'm not really familiar with Java, nor with the Jforex logic. So this is a very raw learning curve.
If you are only learning java then you should definitely use an IDE to learn different language concepts by launching small example programs and only thereafter full-fledged indicators.
Bernabe wrote:
As I know inputs is holding the prices (open, close, high, low). I can't figure out how to use two dimensions to solve my problem. Could you be more concise?
The best way is to see platform indicators that use the InputParameterInfo.Type.PRICE, for instance BullsPowerIndicator, see:
https://www.dukascopy.com/wiki/#Platform_indicator_source_files


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 11 Mar, 2014, 10:49 

User rating: 0
Joined: Tue 28 Jan, 2014, 16:45
Posts: 9
Location: Spain,
I will follow your advices.

Thank you!!


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Wed 12 Mar, 2014, 17:36 

User rating: 0
Joined: Tue 28 Jan, 2014, 16:45
Posts: 9
Location: Spain,
Quote:
Platform indicator source files are available in Standalone API. There are three ways how to retrieve them:
If you are using JForex-API in an IDE, import JForex-SDK in Eclipse or Netbeans. Locate in the project hierarchy JForex-SDK/Referenced Libraries/JForex-API-2.9.4.1.jar/com.dukascopy.indicators.
Alternatively you can download the sources from Dukascopy public repository by path com/dukascopy/api/JForex-API/2.9.4.1/JForex-API-2.9.4.1-sources.jar. Unpack the archive (e.g. with 7-zip) and then browse the folder com\dukascopy\indicators\.
Note use the latest JForex-API version in place of 2.9.4.1. Not all of the indicator source files are in JForex-API, the rest of the indicator source files can be found in ta-lib repository.


I have tried to download the file from the link you provided and I can't get the 2.9.4.1-sources.jar files. The only thing I have found is a directory 2.6.38 and 2.6.33 with these contents:

Quote:
Name Last Modified Size Type
Parent Directory/ - Directory
JForex-API-sources-2.6.38.jar.md5 2011-Dec-01 13:01:36 0.1K application/octet-stream
JForex-API-sources-2.6.38.jar.sha1 2011-Dec-01 13:01:36 0.1K application/octet-stream
JForex-API-sources-2.6.38.pom 2011-Dec-01 13:01:36 0.4K application/octet-stream
JForex-API-sources-2.6.38.pom.md5 2011-Dec-01 13:01:36 0.1K application/octet-stream
JForex-API-sources-2.6.38.pom.sha1 2011-Dec-01 13:01:36 0.1K application/octet-stream


Is one of this the file with the indacator sources? Could you provide a verified link to the sources?

Best regards
Bernabe


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 18 Mar, 2014, 15:00 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Hi,

Here is the link:
https://www.dukascopy.com/client/jforexl ... ources.jar


@Support:
This is a constant madness. Whenever you release new version of the API/Platform, or you reorganize the public-repo structure, links in the wiki pages and forum topics are not working anymore...

I suggest to create a "../latest" symlink that points to the latest release, and you update this link only whenever a new version is out there.


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Tue 18 Mar, 2014, 18:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
we have fixed the link


 
 Post subject: Re: How to calculate an indicator value Post rating: 0   New post Posted: Thu 20 Mar, 2014, 15:20 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
API Support wrote:
we have fixed the link

which will work again until a change in the repo-server structure...

Why not creating the symlink I've mentioned? :?
You wont have to update any wiki anymore, whenever a new version is out there?


 

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