This a code sample that opens two orders, BUYLIMIT and BUYSTOP and closes an order if the other one is filled.
package jforex.strategies;
import com.dukascopy.api.*;
public class Strategy4 implements IStrategy {
private IEngine engine;
private IConsole console;
IOrder order1;
IOrder order2;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
ITick tick = context.getHistory().getLastTick(Instrument.EURUSD);
order1 = engine.submitOrder("oneBuy", Instrument.EURUSD, IEngine.OrderCommand.BUYLIMIT, 0.2, tick.getBid() - Instrument.EURUSD.getPipValue());
order2 = engine.submitOrder("twoBuy", Instrument.EURUSD, IEngine.OrderCommand.BUYSTOP, 0.2, tick.getBid() + Instrument.EURUSD.getPipValue());
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
console.getOut().println(message);
if( message.getType().equals(IMessage.Type.ORDER_FILL_OK) ) {
if( message.getOrder().equals(order1) ) {
concelOrder(order2);
} else if( message.getOrder().equals(order2) ) {
concelOrder(order1);
}
}
}
private void concelOrder(IOrder order) throws JFException {
if(order == null) {
return;
}
if(order.getState() == IOrder.State.CREATED) {
order.waitForUpdate();
}
if (order.getState() == IOrder.State.OPENED) {
order.close();
}
}
public void onStop() throws JFException {
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
}