Use Timer

The following strategy example creates a market order and adds an order stop loss after the certain time. The strategy has 4 reconfigurable parameters: instrument, stop loss in pips, order command and minutes delay for stop loss creation.

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

public class TimingStopLoss implements IStrategy {
        private IEngine engine;
        private IConsole console;
        private long fillTime = 0;
        private long stopLossTime = 0;
        private IOrder order;

        @Configurable("Instrument")
        public Instrument selectedInstrument = Instrument.EURUSD;
        @Configurable("Stop Loss")
        public int stopLoss     = 10;
        @Configurable("Order Command")
        public OrderCommand command     = OrderCommand.BUY;
        @Configurable("SL delay in minute")
        public int distanceInMinute = 2;

Method onStart creates a market order with amount 10,000.

       public void onStart(IContext context) throws JFException {
                this.engine = context.getEngine();
                this.console = context.getConsole();
                order = engine.submitOrder("OrderLabel", selectedInstrument, command, 0.01);
        }

        public void onAccount(IAccount account) throws JFException {
        }

Method onMessage calculates stop loss time after the order is filled. Stop loss time equals order fill time plus specified delay in minutes, because order fill time stored in milliseconds, we need to multiple time with 60 (seconds count in the minute) and 1000 (milliseconds count in the second)

        public void onMessage(IMessage message) throws JFException {
                if (message.getType().equals(IMessage.Type.ORDER_FILL_OK) ) {
                        fillTime = message.getOrder().getFillTime();
                        stopLossTime = fillTime + (distanceInMinute * 60 * 1000);
                }
        }

        public void onStop() throws JFException {
                for (IOrder order : engine.getOrders()) {
                        engine.getOrder(order.getLabel()).close();
                }
        }

Method onTick calls setStopLoss method if tick time is great or equal stop loss time and stop loss is not set before

        public void onTick(Instrument instrument, ITick tick) throws JFException {
                if (!instrument.equals(selectedInstrument))  return;

                if (tick.getTime() >= stopLossTime && order.getStopLossPrice() == 0 ) {
                        setStopLoss();
                }
        }

Method ''setStopLoss" calculates and sets stop loss depend of order type.

        private void setStopLoss() throws JFException {
                if (order.isLong()) {
                        order.setStopLossPrice(order.getOpenPrice() - (stopLoss * selectedInstrument.getPipValue()));
                } else {
                        order.setStopLossPrice(order.getOpenPrice() + (stopLoss * selectedInstrument.getPipValue()));
                }
                order.waitForUpdate(1000);
        }

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

}

TimingStopLoss.java

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