Trying to write an indicator which uses the Price inputs to draw 3 emas on the chart - ema (period) of price close, price high and price low. Fairly new to both java and the JForex inidcator framework - can't quite figure out what's wrong with the code. Can anyone help, and perhaps explain where I've gone wrong?
package com.dukascopy.indicators;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators;
import java.awt.Color;
/**
* @author Simon Kaufmann
* Date: Mar 18, 2011
* Time: 9:35:00 PM
*/
public class Wave implements IIndicator {
public static final Color LightSkyBlue = new Color(135, 206, 250);
public static final Color RoyalBlue = new Color(65, 105, 225);
public static final Color Pink = new Color(255, 192, 203);
public static final Color MediumOrchid = new Color(186, 85, 211);
public static final Color PaleGreen = new Color(0x80, 0xFF, 0x80);
public static final Color IndianRed = new Color(0xFF, 0x80, 0x80);
public static final Color DarkYellow = new Color(0x80, 0x80, 0x00);
private IndicatorInfo indicatorInfo;
private InputParameterInfo inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private int Period = 34;
private Color emaClose = LightSkyBlue;
private Color emaHigh = RoyalBlue;
private Color emaLow = MediumOrchid;
private double[][] inputs = new double[5][]; // Prices for PRICE type are in the following order: open, close, high, low, volume.
private double[][] outputs = new double[3][]; // need 3 arrays of double for 3 ema outputs
private int Lookback = 900;
private IIndicator ema;
public void onStart(IIndicatorContext context)
{
IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
ema = indicatorsProvider.getIndicator("EMA");
indicatorInfo = new IndicatorInfo("WAVE", "Wave Indicator", "Overlay Studies", true, false, true, 1, 1, 3);
inputParameterInfos = new InputParameterInfo("Input Data", InputParameterInfo.Type.PRICE);
/* optInputParameterInfos = new OptInputParameterInfo[]
{ new OptInputParameterInfo("EMA Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(34, 1, 100, 1)),
new OptInputParameterInfo("EMA(High) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.GREEN.ordinal(), availColorsValues, availColorsNames)),
new OptInputParameterInfo("EMA(Close) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.RED.ordinal(), availColorsValues, availColorsNames)),
new OptInputParameterInfo("EMA(Low) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.RED.ordinal(), availColorsValues, availColorsNames))
}; */
optInputParameterInfos = new OptInputParameterInfo[]
{ new OptInputParameterInfo("EMA Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(34, 1, 100, 1)) };
outputParameterInfos = new OutputParameterInfo[]
{ new OutputParameterInfo("EMA(close)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaClose); }},
new OutputParameterInfo("EMA(high)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaHigh); }},
new OutputParameterInfo("EMA(low)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaLow); }}
};
}
public IndicatorResult calculate(int startIndex, int endIndex)
{
//calculating startIndex taking into account lookback value
if (startIndex - getLookback() < 0) {
startIndex -= startIndex - getLookback();
}
// set input to ema as close prices
ema.setInputParameter(0, inputs[1]);
//calculate first ema
ema.setOptInputParameter(0, Period);
ema.setOutputParameter(0, outputs[0]);
ema.calculate(startIndex, endIndex);
// set input to ema as high prices
ema.setInputParameter(0, inputs[2]);
//calculate second ema
ema.setOptInputParameter(0, Period);
ema.setOutputParameter(0, outputs[1]);
ema.calculate(startIndex, endIndex);
// set input to ema as low prices
ema.setInputParameter(0, inputs[3]);
//calculate third ema
ema.setOptInputParameter(0, Period);
ema.setOutputParameter(0, outputs[2]);
return ema.calculate(startIndex, endIndex);
}
public IndicatorInfo getIndicatorInfo() {
return indicatorInfo;
}
public InputParameterInfo getInputParameterInfo(int index) {
return inputParameterInfos;
}
public int getLookback() {
ema.setOptInputParameter(0, Period);
return ema.getLookback();
}
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){
switch (index) {
case 0:
Period = (Integer) value;
ema.setOptInputParameter(0, Period);
break;
default:
throw new ArrayIndexOutOfBoundsException(index);
}
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
public int getLookforward() {
return 0;
}
}