Immediate Or Cancel Order

Immediate Or Cancel (IOC) principle gets applied automatically to market orders. Consider application of the IOC principle to a conditional order. Consider a strategy which on its start creates an order and right after the order fill cancels the unfilled part (if any).

package jforex.orders2;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.util.DateUtils;

 * The strategy on its start creates an order and right after the order fill cancels the unfilled part
public class ImmediateOrCancel2 implements IStrategy {

    public Instrument instrument = Instrument.EURUSD;

    private IConsole console;
    private IEngine engine;
    private IHistory history;
    private IOrder order;

    private final String label = "iocOrder";

    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);

        //price 5 pips below the last bid price
        double price = history.getLastTick(instrument).getBid() - 5 * instrument.getPipValue();
        order = engine.submitOrder(label, instrument, OrderCommand.BUYLIMIT, 0.01, price, 0, 0, 0);

    public void onTick(Instrument instrument, ITick tick) throws JFException {}

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    public void onMessage(IMessage message) throws JFException {
        //our order just got filled AND it has an unfilled part
        if(message.getOrder() == order && order.getState() == IOrder.State.FILLED 
                &&, order.getAmount()) != 0){
            // cancel the unfilled part 

        //print all order related messages
        if(message.getOrder() != null){
            console.getOut().println(DateUtils.format(message.getCreationTime()) + " " +message);


    public void onAccount(IAccount account) throws JFException {}

    //close order on stop
    public void onStop() throws JFException {
        if (engine.getOrder(label) != null)



The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.