IOrder.close() does close pending LIMIT and STOP orders. Consider the following strategy which makes a BUYSTOP order and closes it on the next 10 SEC bar:
package jforex.strategies.oneorder;
import com.dukascopy.api.*;
/**
* The strategy tries to make a BUYSTOP order in every 10 seconds - it does so
* if all previous orders have been closed or canceled
*
*/
public class OneOrderStopOrder implements IStrategy {
private IConsole console;
private IEngine engine;
private Instrument instrument = Instrument.EURUSD;
private Period period = Period.TEN_SECS;
private int counter;
//user defined
private final double buyStopPriceStep = 0.0005;
private double slippage = 1;
private IOrder order;
@Override
public void onStart(IContext context) throws JFException {
engine = context.getEngine();
console = context.getConsole();
print("Start");
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if(!instrument.equals(this.instrument) || ! period.equals(this.period))
return;
double stopLossPrice = askBar.getClose() - 0.0010;
double takeProfitPrice = askBar.getClose() + 0.0010;
//make a new order if there are no orders or the previous orders have been either closed or cancelled
if(engine.getOrders().size() == 0){
order = engine.submitOrder("order" + counter++, this.instrument, IEngine.OrderCommand.BUYSTOP, 0.01,
askBar.getClose() + buyStopPriceStep, slippage, stopLossPrice, takeProfitPrice);
} else {
//close the order otherwise
order.close();
}
}
@Override
public void onMessage(IMessage message) throws JFException {}
@Override
public void onAccount(IAccount account) throws JFException {}
@Override
public void onStop() throws JFException {
for(IOrder o : engine.getOrders())
o.close();
}
private void print(Object o){
console.getOut().println(o);
}
}