|
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.
| java.lang.integer error |
|
ericbiz
|
| Post subject: java.lang.integer error |
Post rating: 0
|
Posted: Sat 22 Jun, 2013, 09:26
|
|
User rating: 1
Joined: Wed 07 Mar, 2012, 05:56 Posts: 101 Location: New CaledoniaNew Caledonia
|
hello support, i try to use my indicators in a strategy but i have this error message when i run it : java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer at jforex.indicators.SMAATRoverRSIInd.setOptInputParameter(SMAATRoverRSIInd.java:222) when i use the indicator manually, i don't have any problem. this is the indicator : package jforex.indicators;
import com.dukascopy.api.IConsole; import com.dukascopy.api.indicators.DoubleRangeDescription; import com.dukascopy.api.indicators.IIndicator; import com.dukascopy.api.indicators.IIndicatorContext; import com.dukascopy.api.indicators.IndicatorInfo; import com.dukascopy.api.indicators.IndicatorResult; import com.dukascopy.api.indicators.InputParameterInfo; import com.dukascopy.api.indicators.IntegerListDescription; import com.dukascopy.api.indicators.IntegerRangeDescription; import com.dukascopy.api.indicators.OptInputParameterInfo; import com.dukascopy.api.indicators.OutputParameterInfo; import java.awt.Color; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.TimeZone;
public class SMAATRoverRSIInd implements IIndicator { public static final int OPEN = 0; public static final int CLOSE = 1; public static final int MIN = 2; public static final int MAX = 3; public static final int AVE = 4; private IndicatorInfo indicatorInfo;
// INPUTS private InputParameterInfo[] inputParameterInfos; private double[][] inputPrices; // OPT INPUTS private OptInputParameterInfo[] optInputParameterInfos; private int appliedPrice; private int rsiPeriod; private int smaPeriod; private double atrMult; // OUTPUTS private OutputParameterInfo[] outputParameterInfos; private double[][] output;
// CONTEXT private IConsole console;
private IIndicator sma; private IIndicator rsi; public int getLookback() { return rsiPeriod + (smaPeriod - 1); }
public int getLookforward() { return 0; }
/************** * CALCULATE **************/ public IndicatorResult calculate(int startIndex, int endIndex) {
if (startIndex - getLookback() < 0) { startIndex -= startIndex - getLookback(); } if (startIndex > endIndex) { return new IndicatorResult(0, 0); } int len = endIndex - startIndex + 1; int rsiLookback = rsiPeriod; int smaLookback = smaPeriod - 1; // rsi rsi.setInputParameter(0, inputPrices[appliedPrice]); rsi.setOptInputParameter(0, rsiPeriod); double[] rsiOutput = new double[len + smaLookback]; rsi.setOutputParameter(0, rsiOutput); rsi.calculate(rsiLookback, endIndex); // sma sma.setInputParameter(0, rsiOutput); sma.setOptInputParameter(0, smaPeriod); sma.setOutputParameter(0, output[0]); sma.calculate(0, rsiOutput.length - 1); System.arraycopy(rsiOutput, smaLookback, output[1], 0, len); for (int outIndex = 0; outIndex < len; outIndex++) { output[2][outIndex] = output[0][outIndex] + output[0][outIndex]*atrMult ; output[3][outIndex] = output[0][outIndex] - output[0][outIndex]*atrMult ; } return new IndicatorResult(startIndex, len); }
public void onStart(IIndicatorContext context) { this.console = context.getConsole();
int[] priceValues = new int[]{0,1,2,3,4}; String[] priceNames = new String[]{"Open","Close","Min","Max","Average"}; inputParameterInfos = new InputParameterInfo[] {
new InputParameterInfo("prices", InputParameterInfo.Type.PRICE)
};
optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("Applied price", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(CLOSE, priceValues, priceNames)), new OptInputParameterInfo("Rsi period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(10, 2, 100, 1)), new OptInputParameterInfo("Sma period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(10, 2, 100, 1)), new OptInputParameterInfo("ATR multiplier", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.1, 0.1, 100, 0.1, 1)) };
outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("RSI", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) { { this.setColor(Color.BLACK); } }, new OutputParameterInfo("SMA", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) { { this.setColor(Color.ORANGE); } }, new OutputParameterInfo("TOP", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.DASH_LINE) { { this.setColor(Color.BLUE); } }, new OutputParameterInfo("BOTTOM", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.DASH_LINE) { { this.setColor(Color.BLUE); } } };
output = new double[outputParameterInfos.length][];
sma = context.getIndicatorsProvider().getIndicator("SMA"); rsi = context.getIndicatorsProvider().getIndicator("RSI"); indicatorInfo = new IndicatorInfo("SMAATR_RSI", "SMA over RSI", "My indicators", false, false, false, inputParameterInfos.length, optInputParameterInfos.length, outputParameterInfos.length); }
public IndicatorInfo getIndicatorInfo() { return indicatorInfo; }
public InputParameterInfo getInputParameterInfo(int index) { if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; } return null; }
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) { if (array instanceof double[][]) { inputPrices = (double[][]) array; } } public void setOptInputParameter(int index, Object value) { switch (index) { case 0: appliedPrice = (Integer) value; break; case 1: rsiPeriod = (Integer) value; break; case 2: smaPeriod = (Integer) value; break; case 3: atrMult = (Double) value; break; } }
public void setOutputParameter(int index, Object array) { output[index] = (double[]) array; }
// ________________________________ // Support: Some helper methods for printing
private void print(Object... o) { for (Object ob : o) { console.getOut().print(ob + " "); } console.getOut().println(); }
private void print(Object o) { console.getOut().println(o); }
private void print(double[] arr) { print(arrayToString(arr)); }
private void print(double[][] arr) { print(arrayToString(arr)); }
private void printIndicatorInfos(IIndicator ind) { for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Input " + ind.getInputParameterInfo(i).getName() + " " + ind.getInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Opt Input " + ind.getOptInputParameterInfo(i).getName() + " " + ind.getOptInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++) { print(ind.getIndicatorInfo().getName() + " Output " + ind.getOutputParameterInfo(i).getName() + " " + ind.getOutputParameterInfo(i).getType()); } }
public static String arrayToString(double[] arr) { String str = ""; for (int r = 0; r < arr.length; r++) { str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; "; } return str; }
public static String arrayToString(double[][] arr) { String str = ""; if (arr == null) return "null"; for (int r = 0; r < arr.length; r++) { for (int c = 0; c < arr[r].length; c++) { str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]); } str += "; "; } return str; }
public String toDecimalToStr(double d) { return (new DecimalFormat("#.#######")).format(d); }
public String dateToStr(long time) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") { { setTimeZone(TimeZone.getTimeZone("GMT")); } }; return sdf.format(time); }
// ________________________________
}
note : it's impossible for me to attach any file with your widget.I do not know if the problem comes from the fact that I am under linux.
|
|
|
|
|
 |
|
tcsabina
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Mon 24 Jun, 2013, 12:23
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
You cannot cast from Object to Double. You need conversion. You can try a very general conversion like atrMult = Double.parseDouble(value.toString());
But of course this could throw exceptions, if (for example) value.toString() is not relevant. If you know what is stored in the value parameter (a String, an Integer, or maybe a `real` Double?), you can use safer conversions. And why it is not happening when you use it alone? Maybe this switch branch (case 3) is never reached when you use the indicator not from the strategy?
|
|
|
|
|
 |
|
CriticalSection
|
| Post subject: Re: java.lang.integer error |
Post rating: 1
|
Posted: Mon 24 Jun, 2013, 17:39
|
|
User rating: 70
Joined: Sat 22 Sep, 2012, 17:43 Posts: 118 Location: Brazil, Fortaleza, Ceará
|
|
The line numbers really aren't making sense but homing in on the actual error implies the problem relates to one of the three Integer casts: java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
Java automatically wraps primitives as they are presented 5 -> new Integer(5), 2.3 -> new Double(2.3) so somewhere the contract to present an integer is being broken leading to a Double object entering the method as 'value' and hitting an Integer cast. It appears that a double value is being placed into one of the integer optional parameters by mistake (perhaps the 'Applied Price'). The code posted above appears sound so without the example strategy calling this indicator it's hard to solve.
|
|
|
|
|
 |
|
ericbiz
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Thu 27 Jun, 2013, 16:57
|
|
User rating: 1
Joined: Wed 07 Mar, 2012, 05:56 Posts: 101 Location: New CaledoniaNew Caledonia
|
|
thanks you for your reply but for the moment i don't find the solution
|
|
|
|
|
 |
|
API Support
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Fri 28 Jun, 2013, 06:50
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
Please provide an empty strategy which replicates the case.
|
|
|
|
|
 |
|
ericbiz
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Sun 30 Jun, 2013, 07:26
|
|
User rating: 1
Joined: Wed 07 Mar, 2012, 05:56 Posts: 101 Location: New CaledoniaNew Caledonia
|
ok i give you again the indicator and an empty strategy with the same problem, package jforex.indicators;
import com.dukascopy.api.IConsole; import com.dukascopy.api.indicators.DoubleRangeDescription; import com.dukascopy.api.indicators.IIndicator; import com.dukascopy.api.indicators.IIndicatorContext; import com.dukascopy.api.indicators.IndicatorInfo; import com.dukascopy.api.indicators.IndicatorResult; import com.dukascopy.api.indicators.InputParameterInfo; import com.dukascopy.api.indicators.IntegerListDescription; import com.dukascopy.api.indicators.IntegerRangeDescription; import com.dukascopy.api.indicators.OptInputParameterInfo; import com.dukascopy.api.indicators.OutputParameterInfo; import java.awt.Color; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.TimeZone;
public class SMAATRoverRSIInd implements IIndicator { public static final int OPEN = 0; public static final int CLOSE = 1; public static final int MIN = 2; public static final int MAX = 3; public static final int AVE = 4; private IndicatorInfo indicatorInfo;
// INPUTS private InputParameterInfo[] inputParameterInfos; private double[][] inputPrices; // OPT INPUTS private OptInputParameterInfo[] optInputParameterInfos; private int appliedPrice; private int rsiPeriod; private int smaPeriod; private double atrMult; // OUTPUTS private OutputParameterInfo[] outputParameterInfos; private double[][] output;
// CONTEXT private IConsole console;
private IIndicator sma; private IIndicator rsi; public int getLookback() { return rsiPeriod + (smaPeriod - 1); }
public int getLookforward() { return 0; }
/************** * CALCULATE **************/ public IndicatorResult calculate(int startIndex, int endIndex) {
if (startIndex - getLookback() < 0) { startIndex -= startIndex - getLookback(); } if (startIndex > endIndex) { return new IndicatorResult(0, 0); } int len = endIndex - startIndex + 1; int rsiLookback = rsiPeriod; int smaLookback = smaPeriod - 1; // rsi rsi.setInputParameter(0, inputPrices[appliedPrice]); rsi.setOptInputParameter(0, rsiPeriod); double[] rsiOutput = new double[len + smaLookback]; rsi.setOutputParameter(0, rsiOutput); rsi.calculate(rsiLookback, endIndex); // sma sma.setInputParameter(0, rsiOutput); sma.setOptInputParameter(0, smaPeriod); sma.setOutputParameter(0, output[0]); sma.calculate(0, rsiOutput.length - 1); System.arraycopy(rsiOutput, smaLookback, output[1], 0, len); for (int outIndex = 0; outIndex < len; outIndex++) { output[2][outIndex] = output[0][outIndex] + output[0][outIndex]*atrMult ; output[3][outIndex] = output[0][outIndex] - output[0][outIndex]*atrMult ; } return new IndicatorResult(startIndex, len); }
public void onStart(IIndicatorContext context) { this.console = context.getConsole();
int[] priceValues = new int[]{0,1,2,3,4}; String[] priceNames = new String[]{"Open","Close","Min","Max","Average"}; inputParameterInfos = new InputParameterInfo[] {
new InputParameterInfo("prices", InputParameterInfo.Type.PRICE)
};
optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("Applied price", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(CLOSE, priceValues, priceNames)), new OptInputParameterInfo("Sma period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(9, 2, 100, 1)), new OptInputParameterInfo("Rsi period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(9, 2, 100, 1)), new OptInputParameterInfo("ATR multiplier", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.1, 0.1, 100, 0.1, 1)) };
outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("SMA", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) { { this.setColor(Color.ORANGE); } }, new OutputParameterInfo("RSI", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) { { this.setColor(Color.BLACK); } }, new OutputParameterInfo("TOP", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.DASH_LINE) { { this.setColor(Color.BLUE); } }, new OutputParameterInfo("BOTTOM", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.DASH_LINE) { { this.setColor(Color.BLUE); } } };
output = new double[outputParameterInfos.length][];
sma = context.getIndicatorsProvider().getIndicator("SMA"); rsi = context.getIndicatorsProvider().getIndicator("RSI"); indicatorInfo = new IndicatorInfo("SMAATR_RSI", "SMA over RSI", "My indicators", false, false, false, inputParameterInfos.length, optInputParameterInfos.length, outputParameterInfos.length); }
public IndicatorInfo getIndicatorInfo() { return indicatorInfo; }
public InputParameterInfo getInputParameterInfo(int index) { if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; } return null; }
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) { if (array instanceof double[][]) { inputPrices = (double[][]) array; } } public void setOptInputParameter(int index, Object value) { switch (index) { case 0: appliedPrice = (Integer) value; break; case 1: rsiPeriod = (Integer) value; break; case 2: smaPeriod = (Integer) value; break; case 3: atrMult = (Double) value; break; } }
public void setOutputParameter(int index, Object array) { output[index] = (double[]) array; }
// ________________________________ // Support: Some helper methods for printing
private void print(Object... o) { for (Object ob : o) { console.getOut().print(ob + " "); } console.getOut().println(); }
private void print(Object o) { console.getOut().println(o); }
private void print(double[] arr) { print(arrayToString(arr)); }
private void print(double[][] arr) { print(arrayToString(arr)); }
private void printIndicatorInfos(IIndicator ind) { for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Input " + ind.getInputParameterInfo(i).getName() + " " + ind.getInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Opt Input " + ind.getOptInputParameterInfo(i).getName() + " " + ind.getOptInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++) { print(ind.getIndicatorInfo().getName() + " Output " + ind.getOutputParameterInfo(i).getName() + " " + ind.getOutputParameterInfo(i).getType()); } }
public static String arrayToString(double[] arr) { String str = ""; for (int r = 0; r < arr.length; r++) { str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; "; } return str; }
public static String arrayToString(double[][] arr) { String str = ""; if (arr == null) return "null"; for (int r = 0; r < arr.length; r++) { for (int c = 0; c < arr[r].length; c++) { str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]); } str += "; "; } return str; }
public String toDecimalToStr(double d) { return (new DecimalFormat("#.#######")).format(d); }
public String dateToStr(long time) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") { { setTimeZone(TimeZone.getTimeZone("GMT")); } }; return sdf.format(time); }
// ________________________________
}
package scalp4hv2;
import com.dukascopy.api.Configurable; import com.dukascopy.api.DataType; import com.dukascopy.api.Filter; import com.dukascopy.api.IAccount; import com.dukascopy.api.IBar; import com.dukascopy.api.IChart; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IEngine; import com.dukascopy.api.IHistory; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IIndicators.AppliedPrice; import com.dukascopy.api.IMessage; import com.dukascopy.api.IOrder; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.OfferSide; import com.dukascopy.api.Period; import com.dukascopy.api.feed.FeedDescriptor; import com.dukascopy.api.indicators.IIndicator; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Currency; import java.util.HashSet; import java.util.Set; import java.util.TimeZone;
/** * * @author eric */ public class SmaRsi implements IStrategy{ private IEngine engine; private IConsole console; private IHistory history; private IIndicators indicators; private IContext context; private IAccount account; private IOrder order; private IIndicator indicator; private IChart chart; /**configuration de base*/ @Configurable ("instrument") public Instrument instrument = Instrument.EURUSD; @Configurable ("bid or ask") public OfferSide offerside = OfferSide.BID; @Configurable ("close,open...") public AppliedPrice appliedPrice = AppliedPrice.CLOSE; @Configurable ("filter") public Filter filter = Filter.ALL_FLATS; @Configurable("slippage") public double slippage = 0; @Configurable("period") public Period selectedPeriod = Period.ONE_HOUR;
/**configuration spécifique*/ @Configurable("rsi period") public int rsiPeriod = 9; private FeedDescriptor feedDescr; int SMA = 0, RSI = 1, TOP = 2, BOTTOM = 3, CURR = 1, PREV =0; public void onStart(IContext context) throws JFException { this.console = context.getConsole(); this.indicators = context.getIndicators(); this.history = context.getHistory(); this.engine = context.getEngine(); this.context = context; this.account = context.getAccount(); this.chart = context.getChart(instrument); feedDescr = new FeedDescriptor(); feedDescr.setDataType(DataType.TIME_PERIOD_AGGREGATION); feedDescr.setOfferSide(offerside); feedDescr.setInstrument(instrument); feedDescr.setFilter(filter); feedDescr.setPeriod(selectedPeriod); }
public void onTick(Instrument instrument, ITick tick) throws JFException { }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { Object[] smaAtrRsiFeed = indicators.calculateIndicator(feedDescr, new OfferSide[] { offerside }, "SMAATR_RSI",new AppliedPrice[] { appliedPrice }, new Object[] {9,9,0.1}, 2, bidBar.getTime(), 0); double [][] smaAtrRsi = { (double[]) smaAtrRsiFeed[SMA],(double[])smaAtrRsiFeed[RSI],(double[])smaAtrRsiFeed[TOP],(double[])smaAtrRsiFeed[BOTTOM]}; if(smaAtrRsi[CURR][SMA]>smaAtrRsi[CURR][TOP] && smaAtrRsi[PREV][SMA]<smaAtrRsi[PREV][TOP] ){ engine.submitOrder(null, instrument, IEngine.OrderCommand.BUY, 1); } }
public void onMessage(IMessage message) throws JFException { }
public void onAccount(IAccount account) throws JFException { }
public void onStop() throws JFException { }
/**money management*/ static class XRate {
private IHistory history; private IContext context; private IConsole console;
public XRate(IContext context) { this.history = context.getHistory(); this.context = context; this.console = context.getConsole();
System.out.println(history +" "+ context +" "+ console); } private String[] mainCurrencies = {"USD", "GBP", "AUD", "CAD", "CHF", "GBP", "HKD", "NZD"};
private double getExchangeRate(Currency c1, Currency c2) throws JFException {
Instrument instr = getInstrument(c1.getCurrencyCode(), c2.getCurrencyCode());
if (instr != null) { subscribe(instr); return getPrice(instr); } else { instr = getInstrument(c2.getCurrencyCode(), c1.getCurrencyCode()); if (instr != null) { subscribe(instr); return 1 / getPrice(instr); } }
double EURCUR1 = exchangeRateEUR(c1); double EURCUR2 = exchangeRateEUR(c2); return EURCUR2 / EURCUR1; }
private double exchangeRateEUR(Currency CUR) throws JFException {
if (CUR.equals(Currency.getInstance("EUR"))) { return 1; }
Instrument instr2 = null; Instrument instr = getInstrument("EUR", CUR.getCurrencyCode());
if (instr == null) { for (String mainCUR : mainCurrencies) {
if (mainCUR.equals(CUR.getCurrencyCode())) { instr = getInstrument("EUR", mainCUR); break; }
instr = getInstrument(mainCUR, CUR.getCurrencyCode()); if (instr != null) { instr2 = getInstrument("EUR", mainCUR); if (instr2 == null) { throw new JFException("Error in currency exchange rate calculation."); } break; } } }
if (instr != null) { if (instr2 == null) { return getPrice(instr); // EUR <-> MAIN_CUR } else { return getPrice(instr) * getPrice(instr2); // EUR <-> MAIN_CUR + MAIN_CUR <-> CUR } } else { if (CUR.equals(Currency.getInstance("XAU")) || CUR.equals(Currency.getInstance("XAG"))) { instr = Instrument.EURUSD; instr2 = getInstrument(CUR.getCurrencyCode(), "USD"); } else { throw new JFException("Error in currency exchange rate calculation."); } subscribe(instr); subscribe(instr2);
// EURUSD * (1 / XXXUSD) = EURUSD * USDXXX = EURXXX return getPrice(instr) * (1 / getPrice(instr2)); // EUR <-> MAIN_CUR + MAIN_CUR <-> CUR } }
private Instrument getInstrument(String c1, String c2) {
if (Instrument.contains(c1 + "/" + c2)) { return Instrument.valueOf(c1 + c2); } return null; }
private void subscribe(Instrument instr) throws JFException { Set<Instrument> instruments = context.getSubscribedInstruments(); Set<Instrument> newInstr = new HashSet<Instrument>(); if (!instruments.contains(instr)) {
newInstr.add(instr); context.setSubscribedInstruments(newInstr);
int i = 10; while (!context.getSubscribedInstruments().containsAll(newInstr) && i>0) { try { console.getOut().println("Instruments not subscribed yet " + i); Thread.sleep(100); } catch (InterruptedException e) { console.getOut().println(e.getMessage()); } i--; }
} }
private double getPrice(Instrument instr) throws JFException { subscribe(instr); ITick tick = history.getLastTick(instr); return (tick.getAsk() + tick.getBid()) / 2; }
static private double getAmount(IContext context, IAccount account, Instrument instrument, double risk, double slPips) throws JFException { return getAmount(context, account, instrument, account.getEquity(), risk, slPips); }
static private double getAmount(IContext context, IAccount account, Instrument instrument, double riskAmount, double risk, double slPips) throws JFException { // !!! USD EUR and JPY in variable names are used only for simplicity // this method is universal and USD EUR and JPY can be any other currencies // EURJPY - pInstrument, USD account currency
risk = risk / 100; if (risk < 0.0 || risk > 1.0) { throw new JFException("Risk must be from 0.0 to 1.0."); } double riskUSD = riskAmount * risk;
double pipJPY = instrument.getPipValue(); // USDJPY pip value in JPY XRate xrate = new XRate(context);
double USDJPY = xrate.getExchangeRate(account.getCurrency(), instrument.getSecondaryCurrency());
double riskJPY = riskUSD * USDJPY; double amountEUR = riskJPY / (slPips * pipJPY);
return amountEUR/ 1000000; }
static void print(double d, IContext context) { context.getConsole().getOut().println((new DecimalFormat("#.#######")).format(d)); } } /**print*/ private void print(Object... o) { for (Object ob : o) { //console.getOut().print(ob + " "); if (ob instanceof Double) { print2(toStr((Double) ob)); } else if (ob instanceof double[]) { print((double[]) ob); } else if (ob instanceof double[][]) { print((double[][]) ob); } else if (Long.class.isInstance(ob)) { print2(toStr((Long) ob)); } else if (ob instanceof IBar) { print2(toStr((IBar) ob)); } else { print2(ob); } print2(" "); } console.getOut().println(); }
private void print2(Object o) { console.getOut().print(o); }
private void print(double[] arr) { print(toStr(arr)); }
private void print(double[][] arr) { print(toStr(arr)); }
private void print(IBar bar) { print(toStr(bar)); }
private void printIndicatorInfos(IIndicator ind) { for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Input " + ind.getInputParameterInfo(i).getName() + " " + ind.getInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++) { print(ind.getIndicatorInfo().getName() + " Opt Input " + ind.getOptInputParameterInfo(i).getName() + " " + ind.getOptInputParameterInfo(i).getType()); } for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++) { print(ind.getIndicatorInfo().getName() + " Output " + ind.getOutputParameterInfo(i).getName() + " " + ind.getOutputParameterInfo(i).getType()); } console.getOut().println(); }
public static String toStr(double[] arr) { String str = ""; for (int r = 0; r < arr.length; r++) { str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; "; } return str; }
public static String toStr(double[][] arr) { String str = ""; if (arr == null) { return "null"; } for (int r = 0; r < arr.length; r++) { for (int c = 0; c < arr[r].length; c++) { str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]); } str += "; "; } return str; }
public String toStr(double d) { return (new DecimalFormat("#.#######")).format(d); }
public String toStr(long time) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") { { setTimeZone(TimeZone.getTimeZone("GMT")); } }; return sdf.format(time); }
private String toStr(IBar bar) { return toStr(bar.getTime()) + " O:" + bar.getOpen() + " C:" + bar.getClose() + " H:" + bar.getHigh() + " L:" + bar.getLow(); }
private void print(Long time) { console.getOut().println(toStr(time)); }
private void print(Throwable th) { StackTraceElement[] elem = th.getStackTrace();
// print stack trace in reverse order because console in jforex client prints in reverse for(int i = elem.length - 1; i >= 0; i--) { console.getErr().println(elem[i]); } }
}
|
|
|
|
|
 |
|
API Support
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Mon 01 Jul, 2013, 07:40
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
In the indicator you define 4 optional inputs: optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("Applied price", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(CLOSE, priceValues, priceNames)), new OptInputParameterInfo("Sma period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(9, 2, 100, 1)), new OptInputParameterInfo("Rsi period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(9, 2, 100, 1)), new OptInputParameterInfo("ATR multiplier", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(0.1, 0.1, 100, 0.1, 1))
But in the strategy you only use 3: new Object[] {9,9,0.1}, Apparently you don't pass value for the first optional input. This should work, but check it with your strategy logic: new Object[] {1,9,9,0.1},
|
|
|
|
|
 |
|
ericbiz
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Mon 01 Jul, 2013, 15:08
|
|
User rating: 1
Joined: Wed 07 Mar, 2012, 05:56 Posts: 101 Location: New CaledoniaNew Caledonia
|
|
thank for your reply but it don't work,
i understand what you say about the optInputParameter,it's logic but why the number one?
i have this error message : 12:55:17 Strategy tester: java.lang.ArrayIndexOutOfBoundsException
|
|
|
|
|
 |
|
API Support
|
| Post subject: Re: java.lang.integer error |
Post rating: 0
|
Posted: Tue 02 Jul, 2013, 08:20
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
ericbiz wrote: i understand what you say about the optInputParameter,it's logic but why the number one? It is just an example, pass the value that you need for your strategy logic for the first optional input. ericbiz wrote: thank for your reply but it don't work, It does work, the exception that you get is on another line and is due to another error - you are mixing the array dimensions - the first dimension stands for output number (in your case you have 4 of them) and the second dimension - for an element in the output array. For further inquiries please don't cut the error message content and the line number the error occurred, as it is essential in finding the cause.
|
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|