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