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.

clarifications about IndicatorResult
 Post subject: clarifications about IndicatorResult Post rating: 0   New post Posted: Thu 19 Apr, 2012, 17:30 
User avatar

User rating: 4
Joined: Wed 21 Dec, 2011, 10:21
Posts: 30
Location: France, Sallanches
Hello,
I'm a newbie in JForex programming.
I think i need some clarifications about IndicatorResult.
In the simple indicator i've made (which calculate the simple derivative on EMA), the indicator only begin to draw a the end of the graph. But i would like to draw the indicator along all the values.
I think i've not understood something about the return of IndicatorResult.

Here the code:

package jforex;
 
import com.dukascopy.api.*;
import com.dukascopy.api.indicators.*;
import java.awt.Color;
import java.text.DecimalFormat;

 
public class DeriveOnEMA 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[2];
    private double[][] outputs = new double[2][];
    private IIndicator ema;
    private IConsole console;
   
 
 
 
    public void onStart(IIndicatorContext context) {
        console = context.getConsole();
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        ema = indicatorsProvider.getIndicator("EMA");
        indicatorInfo = new IndicatorInfo("DeriveOnEMA", "Shows the derivative of ema", "My indicators",
                        false, false, true, 1, 1, 2);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {
               new OptInputParameterInfo("Time period EMA1", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(5, 2,100 , 1))
                              };
        outputParameterInfos = new OutputParameterInfo[] {
                 new OutputParameterInfo("EMA1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.NONE, false),
                 new OutputParameterInfo("Derive", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
               
        };
       
        outputParameterInfos[1].setColor(Color.BLUE);
       
       
    }
 
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
 
        ema.setInputParameter(0, inputs[0]);
 
        //calculate  ema
        ema.setOptInputParameter(0, timePeriod[0]);
        ema.setOutputParameter(0, outputs[0]);
        ema.calculate(startIndex, endIndex);
 
     
     //Calculate simple derivative
      outputs[1][outputs[0].length-1]=outputs[0][outputs[0].length-2]-outputs[0][outputs[0].length-3];
      return new IndicatorResult(startIndex, outputs[0].length);
     
    }
 
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }
 
    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }
 
    public int getLookback() {
        ema.setOptInputParameter(0, timePeriod[0]);
        int ema1Lookback = ema.getLookback();
        return ema1Lookback;
     
    }
 
    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;
    }
}





Somebody would be so kind to explain me where in the mistake ?

By advance, thanks.


 
 Post subject: Re: clarifications about IndicatorResult Post rating: 0   New post Posted: Fri 20 Apr, 2012, 08:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Your code calculated only one derivative - outputs[1][outputs[0].length-1]. All values of outputs[1] array should have been set.
Here is modified code. that computes the derivative.
Lookback is extended by 1 because the calculation of derivative requires two ema values.
We removed the output of ema because ema and derivative are very different in scale (i.e. 1.3 and 0.001) and therefare cannot be meaningfully displayed together.

package jforex;
 
import com.dukascopy.api.*;
import com.dukascopy.api.indicators.*;
import java.awt.Color;
import java.text.DecimalFormat;
 
 
public class DeriveOnEMA 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[2];
    private double[][] outputs = new double[2][];
    private IIndicator ema;
    private IConsole console;
     
   
 
 
    public void onStart(IIndicatorContext context) {
        console = context.getConsole();
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        ema = indicatorsProvider.getIndicator("EMA");
        indicatorInfo = new IndicatorInfo("DeriveOnEMA2", "Shows the derivative of ema", "My indicators",
                        false, false, true, 1, 1, 2);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {
               new OptInputParameterInfo("Time period EMA1", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(5, 2, 100, 1))
                              };
        outputParameterInfos = new OutputParameterInfo[] {
                 //new OutputParameterInfo("EMA1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.NONE, false),
                 new OutputParameterInfo("Derive", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),
                 new OutputParameterInfo("Level", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LEVEL_DASH_LINE){{
                     setColor(Color.GRAY);
                 }}
        };
         
        //outputParameterInfos[1].setColor(Color.BLUE);
       
         
    }
 
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int len = endIndex - startIndex + 1;

        double[] emaOut = new double[len + 1];
        // len - outputs length; len+1 - length of ema output, one extra value is used to compute derivative
        ema.setInputParameter(0, inputs[0]);
       
        //calculate  ema
        ema.setOptInputParameter(0, timePeriod[0]);
        ema.setOutputParameter(0, emaOut);
        ema.calculate(endIndex - 1 - emaOut.length, endIndex);
       
        //Calculate simple derivative
        for(int i = 0; i < len; i++) {
            outputs[0][i] = emaOut[i+1] - emaOut[i];
            outputs[1][i] = 0;
        }
       

        //outputs[1][outputs[0].length - 1] = outputs[0][outputs[0].length - 2] - outputs[0][outputs[0].length - 3];

        return new IndicatorResult(startIndex, len);

    }
 
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }
 
    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }
 
    public int getLookback() {
        ema.setOptInputParameter(0, timePeriod[0]);
        int ema1Lookback = ema.getLookback();
        return ema1Lookback + 1;
       
    }
 
    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;
    }
}


Attachments:
DeriveOnEMA.java [4.24 KiB]
Downloaded 316 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: clarifications about IndicatorResult Post rating: 0   New post Posted: Thu 21 Jun, 2012, 16:10 
User avatar

User rating: 4
Joined: Wed 21 Dec, 2011, 10:21
Posts: 30
Location: France, Sallanches
Sorry to dig up this topic, but in fact i have tried to calculate the SMA of the derivative.
It's ok, it works but... It doesnt calculate the last values. I don't know why. There wa ssomething i didn't do, but i dont know what...
Here the code:

package jforex;
 
import com.dukascopy.api.*;
import com.dukascopy.api.indicators.*;
import java.awt.Color;
import java.text.DecimalFormat;
 
 
public class SMADeriveOnEMA 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[2];
    private double[][] outputs = new double[2][];
    private IIndicator ema;
    private IIndicator maDerive;
    private IConsole console;
   
     
   
   
    public void onStart(IIndicatorContext context) {
        console = context.getConsole();
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        ema = indicatorsProvider.getIndicator("EMA");
         maDerive= indicatorsProvider.getIndicator("SMA");
        indicatorInfo = new IndicatorInfo("DeriveOnEMA3", "Shows the derivative of ema", "My indicators",
                        false, false, true, 1, 2, 2);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {
               new OptInputParameterInfo("Time period EMA1", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(30, 2, 100, 1)),
               new OptInputParameterInfo("Period calculation for derive", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(5, 2, 100, 1)),
               
                              };
        outputParameterInfos = new OutputParameterInfo[] {
                  new OutputParameterInfo("Derive", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),
                 new OutputParameterInfo("Level", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LEVEL_DASH_LINE){{
                     setColor(Color.GRAY);
                 }}
        };
         
   
       
         
    }
 
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
       
        if (timePeriod[0]<timePeriod[1]) {
            console.getOut().println("Error: ema cannot be lesser than sma");
            return new IndicatorResult(0,0);
        }
       
             
        int len = endIndex - startIndex + 1;

        double[] emaOut = new double[len + 1];
        // len - outputs length; len+1 - length of ema output, one extra value is used to compute derivative
        ema.setInputParameter(0, inputs[0]);
       
        //calculate  ema
        ema.setOptInputParameter(0, timePeriod[0]);
        ema.setOutputParameter(0, emaOut);
        ema.calculate(endIndex - 1 - emaOut.length, endIndex);
       
       double[] arrayDerive= new double[len+1];
       
         for (int i=0;i<len;i++){
         arrayDerive[i]=emaOut[i+1] - emaOut[i];
         
         }
         
          //   int len2= startIndex -= startIndex - maDerive.getLookback();
          int len2=startIndex-maDerive.getLookback();
       
        double[] maDeriveOut = new double[len2];
        // len - outputs length; len+1 - length of ema output, one extra value is used to compute derivative
        maDerive.setInputParameter(0, arrayDerive);
       
        //calculate  ema
        maDerive.setOptInputParameter(0, timePeriod[1]);
        maDerive.setOutputParameter(0, outputs[0]);
        int maDeriveLoockBack=maDerive.getLookback();
        maDerive.calculate(maDeriveLoockBack, emaOut.length-1);
     
       
        return new IndicatorResult(startIndex, len);

    }
 
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }
 
    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }
 
    public int getLookback() {
        ema.setOptInputParameter(0, timePeriod[0]);
        int ema1Lookback = ema.getLookback();
        maDerive.setOptInputParameter(0, timePeriod[1]);
        int maDeriveLookBack = maDerive.getLookback();
        int lookback= 1+Math.max(ema1Lookback, maDeriveLookBack);
        return lookback;
       
       
    }
 
    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;
    }
   
    private void printDouble (double d, String s) {
        console.getOut().println(s+(new DecimalFormat("#.#####")).format(d));
    }
   
     private void printDouble (double d) {
        console.getOut().println((new DecimalFormat("#.#####")).format(d));
    }
}


If you can help me, i'm becoming a little bit crazy with it...


 
 Post subject: Re: clarifications about IndicatorResult Post rating: 0   New post Posted: Sat 23 Jun, 2012, 08:12 
User avatar

User rating: 4
Joined: Wed 21 Dec, 2011, 10:21
Posts: 30
Location: France, Sallanches
Sorry for the inconvenience, but in fact i have resolved my problem.


 

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