I found the following simple DMI strategy in the strategies library. Here is the link:
[url]
viewtopic.php?f=61&t=38080[/url]
It makes an order on every -DI and +DI cross: a SELL order if +DI goes below -DI and a BUY order if +DI goes above -DI.
I tried to run the strategy but there is an error that says :
Quote:
2012-03-20 11:50:55 java.lang.NullPointerException @ jforex.bugtests.DMIstrategy.onStart(DMIstrategy.java:40)
Kindly help me fix the strategy. Also tell me where should I enter the lotsize?
Also tell me what is relevance of the date and time in the code. I tried entereing that as well. Why is it required anyway?
Also tell me what does this mean :
IOrder order = engine.submitOrder("order"+ ++orderCount, Instrument.EURUSD, cmd, 0.001, 0, 20,
bidBar.getClose() - 0.005, //Stop Loss
askBar.getClose() + 0.005);//Take Profit
Here is the code :
package jforex.bugtests;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class DMIstrategy implements IStrategy {
private IEngine engine;
private IIndicators indicators;
private IConsole console;
private IChart chart;
// macd specific params
private int dmiPeriod = 14;
// strategy specific params
private Instrument instrument = Instrument.EURUSD;
private Period period = Period.TEN_MINS;
@SuppressWarnings("serial")
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};
private int orderCount;
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
indicators = context.getIndicators();
this.console = context.getConsole();
this.chart = context.getChart(instrument);
console.getOut().println("Started");
/* uncomment to see the ordering of optional parameters
IIndicator indicator = indicators.getIndicator("DMI");
for (int i = 0; i < indicator.getIndicatorInfo().getNumberOfOptionalInputs(); i++){
console.getOut().println( i + ": name = '"+ indicator.getOptInputParameterInfo(i).getName() + "' type = "+ indicator.getOptInputParameterInfo(i).getType());
}
*/
chart.addIndicator(indicators.getIndicator("DMI"), new Object[]{dmiPeriod});
}
public void onStop() throws JFException {
for (IOrder order : engine.getOrders()) {
order.close();
}
console.getOut().println("Stopped");
}
public void onTick(Instrument instrument, ITick tick) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (period != this.period || instrument != this.instrument)
return;
double[] dmi = indicators.dmi(instrument, period, OfferSide.BID, dmiPeriod, 1);
double[] dmiLast = indicators.dmi(instrument, period, OfferSide.BID, dmiPeriod, 2);
print(sdf.format(bidBar.getTime()) + " ADX=" + dmi[0] +" +DI=" + dmi[1] +" -DI=" + dmi[2]);
//+DI and -DI cross condition
if ((dmi[1] > dmi[2] && dmiLast[1] < dmiLast[2]) || (dmi[1] < dmi[2] && dmiLast[1] > dmiLast[2])){
//SELL if +DI goes below -DI, BUY otherwise
OrderCommand cmd = (dmi[1] > dmi[2] && dmiLast[1] < dmiLast[2]) ? OrderCommand.SELL : OrderCommand.BUY;
IOrder order = engine.submitOrder("order"+ ++orderCount, Instrument.EURUSD, cmd, 0.001, 0, 20,
bidBar.getClose() - 0.005, //Stop Loss
askBar.getClose() + 0.005);//Take Profit
print("+DI and -DI cross!! Make a " + cmd + " order: " + order.getLabel());
}
}
private void print(Object o) {
console.getOut().println(o);
}
public void onMessage(IMessage message) throws JFException {}
public void onAccount(IAccount account) throws JFException {}
}
Thanks to anyone who replies to this.
