|
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.
Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 3000 for Close data. |
tjozsa
|
Post subject: Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 3000 for Close data. |
Post rating: 0
|
Posted: Sun 11 Nov, 2012, 15:39
|
|
User rating: 0
Joined: Fri 09 Nov, 2012, 21:32 Posts: 1 Location: Hungary, Kistarcsa
|
Hi, I have implemented a small indicator that supposed to analyze each candle. When I compile and put the it on the chart I get: 2012-11-09 22:00:01 Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 3000 @ jforex.CandleType.calculate(CandleType.java:92) The strange thing is that I cannot work myself around this problem becasue no metter how I try to access for instance candle number 3000 I get the same. I believe to have tried all documented methods to access Open High Close Low data of bars on the chart and all fail with the same issue. A little bit more meta data, should it be required: 5 MIN EURUSD chart is used! Please help me if this is my fault. If this is a bug I would like to help you hunt it down. Regards, Tamas package jforex;
import com.dukascopy.api.indicators.*; import com.dukascopy.api.IConsole; import com.dukascopy.api.Instrument; import com.dukascopy.api.OfferSide; import com.dukascopy.api.IBar;
public class CandleType implements IIndicator { private IndicatorInfo indicatorInfo; private InputParameterInfo[] inputParameterInfos; private OptInputParameterInfo[] optInputParameterInfos; private OutputParameterInfo[] outputParameterInfos; private double[][][] inputs = new double[1][][]; private IBar[] iBidBars; private int timePeriod = 2; private double[][] outputs = new double[5][]; private IConsole console; // Think you use this for console output private double LONG_BODY_FACTOR; private double SMALL_BODY_FACTOR; private double LONG_WICK_FACTOR; private int BACKWARD; // private Instrument instrument; private Instrument instrument = Instrument.EURUSD; // workaround public void onStart(IIndicatorContext context) { console = context.getConsole(); // Used for console indicatorInfo = new IndicatorInfo("CandleType", "CandleType", "Trend", true, false, true, 2, 4, 5); inputParameterInfos = new InputParameterInfo[]{ new InputParameterInfo("Price", InputParameterInfo.Type.PRICE), new InputParameterInfo("Bar", InputParameterInfo.Type.BAR) { { setInstrument(instrument); setOfferSide(OfferSide.BID); } } }; optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("Long Body Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 1.1, 10.0, 0.1, 1)), new OptInputParameterInfo("Small Body Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.3, 0.1, 0.9, 0.1, 1)), new OptInputParameterInfo("Long Wick Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 1.1, 10.0, 0.1, 1)), new OptInputParameterInfo("Backward", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(15, 2, 100, 1)), }; outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("DOJI", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//DOJI new OutputParameterInfo("SMALL", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//BODY_SMALL new OutputParameterInfo("LONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//BODY_LONG new OutputParameterInfo("WULONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//WICK_LONG_UPPER new OutputParameterInfo("WLLONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP)//WICK_LONG_LOWER }; outputParameterInfos[0].setArrowSymbol('D'); outputParameterInfos[1].setArrowSymbol('S'); outputParameterInfos[2].setArrowSymbol('L'); outputParameterInfos[3].setArrowSymbol('X'); outputParameterInfos[4].setArrowSymbol('Y'); }
public IndicatorResult calculate(int startIndex, int endIndex) {
if (startIndex - getLookback() < 0) { // Need some candles before we try to calculate anything startIndex -= startIndex - getLookback(); } if (startIndex > endIndex) { // I think this stops it from calculating unless there is enough bars available. return new IndicatorResult(0, 0); } // Loops for iterating arrays go here int i, j; for (i = startIndex, j = 0; i <= endIndex; i++, j++){ double[] array = inputs[0][1];
console.getOut().println("IBAR: "+ iBidBars[i].getClose()); if(isDoji(i)) outputs[0][i] = iBidBars[i].getHigh(); if(isBodySmall(i)) outputs[1][i] = iBidBars[i].getHigh(); if(isBodyLong(i)) outputs[2][i] = iBidBars[i].getHigh(); if(isWickLong(i, true)) outputs[3][i] = iBidBars[i].getHigh(); if(isWickLong(i, false)) outputs[4][i] = iBidBars[i].getLow(); } return new IndicatorResult(startIndex, j); // Returns final result }
public IndicatorInfo getIndicatorInfo() { return indicatorInfo; }
public InputParameterInfo getInputParameterInfo(int index) { if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; } return null; }
public int getLookback() { return 1000; }
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) { switch (index) { case 0: inputs[index] = (double[][]) array; break; case 1: iBidBars = (IBar[]) array; break; default: throw new ArrayIndexOutOfBoundsException(index); } }
public void setOutputParameter(int index, Object array) { outputs[index] = (double[]) array; } public void setOptInputParameter(int index, Object value) { switch (index) { case 0: LONG_BODY_FACTOR = (Double) value; break; case 1: SMALL_BODY_FACTOR = (Double) value; break; case 2: LONG_WICK_FACTOR = (Double) value; break; case 3: BACKWARD = (Integer) value; break; default: throw new ArrayIndexOutOfBoundsException(index); } } public boolean isWhite(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose();
boolean retVal = o < c; return retVal; } public boolean isBlack(int shift){ double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose();
boolean retVal = o > c; return retVal; } public boolean isDoji(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose();
boolean retVal = o == c; return retVal; } public boolean isBodyLong(int shift) { double candleSize = candleSize(shift); double avgCandleSize = avgCandleSize(shift); double avgCandleSizeLongBodyFactor = avgCandleSize * LONG_BODY_FACTOR;
boolean retVal = candleSize > avgCandleSizeLongBodyFactor; return retVal; } public boolean isBodySmall(int shift) { double candleSize = candleSize(shift); double avgCandleSize = avgCandleSize(shift); double avgCandleSizeSmallBodyFactor = avgCandleSize * SMALL_BODY_FACTOR;
boolean retVal = candleSize < avgCandleSizeSmallBodyFactor; return retVal; } public boolean isWickLong(int shift, boolean upper) { double wickSize = wickSize(shift, upper); double avgWickSize = avgWickSize(shift, upper); double avgWickSizeLongWickFactor = avgWickSize * LONG_WICK_FACTOR;
boolean retVal = wickSize > avgWickSizeLongWickFactor; return retVal; } public double candleSize(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose();
double retVal = Math.abs(o - c); return retVal; } public double avgCandleSize(int shift) { double retVal = 0; for (int i = shift-BACKWARD; i < shift; i++) { retVal += candleSize(i); } retVal = retVal / BACKWARD; return retVal; } public double wickSize(int shift, boolean upper) { double retVal = 0; double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); double h = iBidBars[shift].getHigh(); double l = iBidBars[shift].getLow();
// we need to branch based on which of the two wicks need to be calculated. retVal = upper ? h - Math.max(o, c) : Math.min(o, c) - l; return retVal; } public double avgWickSize(int shift, boolean upper) { double retVal = 0; for (int i = 0; i < BACKWARD; i++) { retVal += wickSize(shift + i, upper); } retVal = retVal / BACKWARD; return retVal; } }
|
|
|
|
 |
API Support
|
Post subject: Re: Error in indicator: java.lang.ArrayIndexOutOfBoundsException: 3000 for Close data. |
Post rating: 0
|
Posted: Tue 13 Nov, 2012, 08:45
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Find our comments in the source code prefixed with "Support". package jforex.requests; import com.dukascopy.api.indicators.*; import com.dukascopy.api.IConsole; import com.dukascopy.api.Instrument; import com.dukascopy.api.OfferSide; import com.dukascopy.api.IBar; public class CandleType implements IIndicator { private IndicatorInfo indicatorInfo; private InputParameterInfo[] inputParameterInfos; private OptInputParameterInfo[] optInputParameterInfos; private OutputParameterInfo[] outputParameterInfos; private double[][][] inputs = new double[1][][]; private IBar[] iBidBars; private int timePeriod = 2; private double[][] outputs = new double[5][]; private IConsole console; // Think you use this for console output private double LONG_BODY_FACTOR; private double SMALL_BODY_FACTOR; private double LONG_WICK_FACTOR; private int BACKWARD; // private Instrument instrument; private Instrument instrument = Instrument.EURUSD; // workaround public void onStart(IIndicatorContext context) { console = context.getConsole(); // Used for console indicatorInfo = new IndicatorInfo("CandleType", "CandleType", "Trend", true, false, true, 2, 4, 5); inputParameterInfos = new InputParameterInfo[]{ new InputParameterInfo("Price", InputParameterInfo.Type.PRICE), new InputParameterInfo("Bar", InputParameterInfo.Type.BAR) { { setInstrument(instrument); setOfferSide(OfferSide.BID); } } }; optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("Long Body Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 1.1, 10.0, 0.1, 1)), new OptInputParameterInfo("Small Body Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.3, 0.1, 0.9, 0.1, 1)), new OptInputParameterInfo("Long Wick Factor", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 1.1, 10.0, 0.1, 1)), new OptInputParameterInfo("Backward", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(15, 2, 100, 1)), }; outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("DOJI", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//DOJI new OutputParameterInfo("SMALL", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//BODY_SMALL new OutputParameterInfo("LONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//BODY_LONG new OutputParameterInfo("WULONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP),//WICK_LONG_UPPER new OutputParameterInfo("WLLONG", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.ARROW_SYMBOL_UP)//WICK_LONG_LOWER }; outputParameterInfos[0].setArrowSymbol('D'); outputParameterInfos[1].setArrowSymbol('S'); outputParameterInfos[2].setArrowSymbol('L'); outputParameterInfos[3].setArrowSymbol('X'); outputParameterInfos[4].setArrowSymbol('Y'); } public IndicatorResult calculate(int startIndex, int endIndex) { if (startIndex - getLookback() < 0) { // Need some candles before we try to calculate anything startIndex -= startIndex - getLookback(); } if (startIndex > endIndex) { // I think this stops it from calculating unless there is enough bars available. return new IndicatorResult(0, 0); } // Loops for iterating arrays go here int in, out; for (in = startIndex, out = 0; in <= endIndex; in++, out++) {
//double[] array = inputs[0][1];
//console.getOut().println("IBAR: " + iBidBars[i].getClose()); //Support: you should use i for input indices and j for output indices, we changed the variable names //in order to make it straightforward if (isDoji(in)) outputs[0][out] = iBidBars[in].getHigh(); if (isBodySmall(in)) outputs[1][out] = iBidBars[in].getHigh(); if (isBodyLong(in)) outputs[2][out] = iBidBars[in].getHigh(); if (isWickLong(in, true)) outputs[3][out] = iBidBars[in].getHigh(); if (isWickLong(in, false)) outputs[4][out] = iBidBars[in].getLow();
} return new IndicatorResult(startIndex, out); // Returns final result } public IndicatorInfo getIndicatorInfo() { return indicatorInfo; } public InputParameterInfo getInputParameterInfo(int index) { if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; } return null; } //Support: Increase back to 1000 if you really need that much values, otherwise it affects the performance public int getLookback() { return 100; } 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) { switch (index) { case 0: inputs[index] = (double[][]) array; break; case 1: iBidBars = (IBar[]) array; break; default: throw new ArrayIndexOutOfBoundsException(index); } } public void setOutputParameter(int index, Object array) { outputs[index] = (double[]) array; } public void setOptInputParameter(int index, Object value) { switch (index) { case 0: LONG_BODY_FACTOR = (Double) value; break; case 1: SMALL_BODY_FACTOR = (Double) value; break; case 2: LONG_WICK_FACTOR = (Double) value; break; case 3: BACKWARD = (Integer) value; break; default: throw new ArrayIndexOutOfBoundsException(index); } } public boolean isWhite(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); boolean retVal = o < c; return retVal; } public boolean isBlack(int shift){ double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); boolean retVal = o > c; return retVal; } public boolean isDoji(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); boolean retVal = o == c; return retVal; } public boolean isBodyLong(int shift) { double candleSize = candleSize(shift); double avgCandleSize = avgCandleSize(shift); double avgCandleSizeLongBodyFactor = avgCandleSize * LONG_BODY_FACTOR; boolean retVal = candleSize > avgCandleSizeLongBodyFactor; return retVal; } public boolean isBodySmall(int shift) { double candleSize = candleSize(shift); double avgCandleSize = avgCandleSize(shift); double avgCandleSizeSmallBodyFactor = avgCandleSize * SMALL_BODY_FACTOR; boolean retVal = candleSize < avgCandleSizeSmallBodyFactor; return retVal; } public boolean isWickLong(int shift, boolean upper) { double wickSize = wickSize(shift, upper); double avgWickSize = avgWickSize(shift, upper); double avgWickSizeLongWickFactor = avgWickSize * LONG_WICK_FACTOR; boolean retVal = wickSize > avgWickSizeLongWickFactor; return retVal; } public double candleSize(int shift) { double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); double retVal = Math.abs(o - c); return retVal; } public double avgCandleSize(int shift) { double retVal = 0; for (int i = shift-BACKWARD; i < shift && i < iBidBars.length; i++) { retVal += candleSize(i); } retVal = retVal / BACKWARD; return retVal; } public double wickSize(int shift, boolean upper) { double retVal = 0; double o = iBidBars[shift].getOpen(); double c = iBidBars[shift].getClose(); double h = iBidBars[shift].getHigh(); double l = iBidBars[shift].getLow(); // we need to branch based on which of the two wicks need to be calculated. retVal = upper ? h - Math.max(o, c) : Math.min(o, c) - l; return retVal; } public double avgWickSize(int shift, boolean upper) { double retVal = 0; //Support: addded range check shift + i < iBidBars.length - there was another ArrayIndexOutOfBoundsException for (int i = 0; i < BACKWARD && shift + i < iBidBars.length; i++) { retVal += wickSize(shift + i, upper); } retVal = retVal / BACKWARD; return retVal; } }
Attachments: |
CandleType.java [9.93 KiB]
Downloaded 287 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.
|
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|