You might consider checking in onTick method if the previous order has been closed. Consider the following strategy:
package jforex.strategies.oneorder;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class OneOrderStopTrigger implements IStrategy {
private IConsole console;
private IEngine engine;
private IHistory history;
private IOrder order;
private final String label = "OneOrder";
private SimpleDateFormat dateFormat;
@Override
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
console = context.getConsole();
history = context.getHistory();
console.getOut().println("Start");
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
//the first order
submitOrder(history.getLastTick(Instrument.EURUSD));
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
if(!instrument.equals(Instrument.EURUSD))
return;
if (!engine.getOrders().contains(order)){
//order is likely to have been closed on SL - create a new order
submitOrder(tick);
}
}
private void submitOrder(ITick tick) throws JFException{
order = engine.submitOrder(label, Instrument.EURUSD, OrderCommand.BUYSTOP,
0.01, //amount
tick.getAsk() + 0.0005, //price of Buy stop
20, //slippage
history.getLastTick(Instrument.EURUSD).getBid() - 0.0005, //SL
0);//TP
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
// TODO Auto-generated method stub
}
@Override
public void onMessage(IMessage message) throws JFException {
console.getOut().println(dateFormat.format(message.getCreationTime()) + " " + message.getContent());
}
@Override
public void onAccount(IAccount account) throws JFException {
// TODO Auto-generated method stub
}
@Override
public void onStop() throws JFException {
if (engine.getOrder(label) != null)
engine.getOrder(label).close();
}
}