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.

Problem with a custom EMA indicator
 Post subject: Problem with a custom EMA indicator Post rating: 0   New post Posted: Tue 12 Jul, 2011, 23:58 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Hello support team,

I'm new in JForex programming and I want to create a custom EMA indicator (with a more fine-tuned smoothing level than normally, using a custom weigthing factor) for later usage in a more complex indicator.
Compilation was successful but unfortunately the indicator line in the chart does not look like a normal moving average ...
Can you please have a look at the code below and tell me what's wrong ?
I assume the error is in the "calculate" method. Is there a special procedure in JForex regarding indicators for which the value of the next period is calculated based on the output value of the previous period ?

package jforex;

import com.dukascopy.api.indicators.*;

public class I_MyEMA implements IIndicator

{
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] dInputs = new double[1][];
    private double[][] dOutputs = new double[1][];
    private int iSmoothingLevel = 5;
    private int iLookback = 2;
   
    public void onStart(IIndicatorContext context)
    {
        indicatorInfo = new IndicatorInfo("I_MyEMA", "My EMA", "My Indicators", true, false, true, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input Data", InputParameterInfo.Type.DOUBLE)};
        inputParameterInfos[0].setAppliedPrice(com.dukascopy.api.IIndicators.AppliedPrice.CLOSE);
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Smoothing Level", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(5, 1, 20, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("Output Data", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)};
    }
   
    public IndicatorResult calculate(int iStartIndex, int iEndIndex)
    {
        if (iStartIndex - getLookback() < 0)
            iStartIndex -= iStartIndex - getLookback();
       
        int iIn, iOut;
        for (iIn = iStartIndex, iOut = 0; iIn <= iEndIndex; iIn++, iOut++)
        {
            if (iOut == 0)
                dOutputs[0][iOut] = dInputs[0][iIn];
            else
                dOutputs[0][iOut] = dOutputs[0][iOut-1] + ((dInputs[0][iIn] - dOutputs[0][iOut-1]) * (3 / (iSmoothingLevel + 3)));
        }
        return new IndicatorResult(iStartIndex, iOut);
    }
   
    public IndicatorInfo getIndicatorInfo()
    {
        return indicatorInfo;
    }
   
    public InputParameterInfo getInputParameterInfo(int iIndex)
    {
        if (iIndex <= inputParameterInfos.length)
            return inputParameterInfos[iIndex];
       
        return null;
    }
   
    public int getLookback()
    {
        return iLookback;
    }
   
    public int getLookforward()
    {
        return 0;
    }
   
    public OptInputParameterInfo getOptInputParameterInfo(int iIndex)
    {
        if (iIndex <= optInputParameterInfos.length)
            return optInputParameterInfos[iIndex];
       
        return null;
    }
   
    public OutputParameterInfo getOutputParameterInfo(int iIndex)
    {
        if (iIndex <= outputParameterInfos.length)
            return outputParameterInfos[iIndex];
       
        return null;
    }
   
    public void setInputParameter(int iIndex, Object array)
    {
        dInputs[iIndex] = (double[]) array;
    }
   
    public void setOptInputParameter(int iIndex, Object value)
    {
        iSmoothingLevel = (Integer) value;
    }
   
    public void setOutputParameter(int iIndex, Object array)
    {
        dOutputs[iIndex] = (double[]) array;
    }
}


Thanks in advance for your help.

Best regards
AbsoluteReturner


 
 Post subject: Re: Problem with a custom EMA indicator Post rating: 0   New post Posted: Wed 13 Jul, 2011, 13:22 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider changing the line 37 to:
dOutputs[0][iOut] = dOutputs[0][iOut-1] + ((dInputs[0][iIn] - dOutputs[0][iOut-1]) * (3d/ (iSmoothingLevel + 3)));
If you don't add cast operator d to double (i.e. 3) then the division operation becomes integer division - the result of it is an integer not a double. Alternatively you might consider writing 3.0 or use Double.valueOf(3).


 
 Post subject: Re: Problem with a custom EMA indicator Post rating: 0   New post Posted: Wed 13 Jul, 2011, 14:37 

User rating: -
Thank you very much, this was the correct solution.

Best regards
AR


 

Jump to:  

cron
  © 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