Sorry, the code in my previous post had a bug and hid the real error. I can't edit previous post so I'm posting the corrected code below. I've added the exception as well:.
A few other important things I didn't mention:
1) If you have the JForex GUI client running while this test is running, everything works fine.
2) This problem only seems to occur with OPENED orders and not FILLED orders.
3) If you do not exit the jvm between opening and closing an order, it all works fine.
java.lang.NullPointerException
at com.dukascopy.api.impl.connect.j.b(Unknown Source)
at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
at BuySellTest$SellStrat.onTick(BuySellTest.java:166)
at com.dukascopy.api.impl.execution.t.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at com.dukascopy.api.impl.execution.l$a.a(Unknown Source)
at com.dukascopy.api.impl.execution.l$a.run(Unknown Source)
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
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.Period;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ISystemListener;
public class BuySellTest {
private static final Logger _log = LoggerFactory.getLogger(BuySellTest.class);
private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
private static String userName = "";
private static String password = "";
public BuySellTest(String[] args) throws Exception {
if (args.length != 1 || (!args[0].equals("buy") && !args[0].equals("sell"))) {
System.out.println("Usage: BuySellTest [buy|sell]");
System.exit(1);
}
String bs = args[0];
final IClient client = ClientFactory.getDefaultInstance();
client.setSystemListener(new ISystemListener() {
private int lightReconnects = 3;
public void onStart(long processId) {
_log.info("Strategy started: " + processId);
}
public void onStop(long processId) {
_log.info("Strategy stopped: " + processId);
if (client.getStartedStrategies().size() == 0) {
System.exit(0);
}
}
public void onConnect() {
_log.info("Connected");
lightReconnects = 3;
}
public void onDisconnect() {
_log.warn("Disconnected");
if (lightReconnects > 0) {
client.reconnect();
--lightReconnects;
} else {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {}
try {
client.connect(jnlpUrl, userName, password);
} catch (Exception e) {
_log.error(e.getMessage(), e);
}
}
}
});
_log.info("Connecting...");
client.connect(jnlpUrl, userName, password);
int i = 60;
while (i > 0 && !client.isConnected()) {
Thread.sleep(1000);
i--;
}
if (!client.isConnected()) {
_log.error("Failed to connect Dukascopy servers");
System.exit(1);
}
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.GBPUSD);
_log.info("Subscribing instruments...");
client.setSubscribedInstruments(instruments);
_log.info("Starting strategy");
if (bs.equals("buy")) {
client.startStrategy(this.new BuyStrat());
} else {
client.startStrategy(new SellStrat());
}
_log.info("running...");
}
public static void main(String[] args) throws Exception {
new BuySellTest(args);
}
public class BuyStrat implements IStrategy {
private IEngine engine;
public void onAccount(IAccount account) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
}
public void onStop() throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {
List<IOrder> orders = engine.getOrders();
if (orders.size() > 0) return;
double pip = instrument.getPipValue();
double bid = tick.getBid();
double ask = tick.getAsk();
String label = BuySellTest.class.getSimpleName() + System.currentTimeMillis();
engine.submitOrder(label, instrument, IEngine.OrderCommand.SELLSTOP, 0.001, bid
- (pip * 100), 0, ask, ask - (pip * 200));
_log.info("order submitted");
}
}
public class SellStrat implements IStrategy {
private IEngine engine;
public void onAccount(IAccount account) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
}
public void onStop() throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {
List<IOrder> orders = engine.getOrders();
for (IOrder order : orders) {
_log.info("closing order: " + order.getId());
order.close();
}
}
}
}
Mark