Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Cancelling trailing step doesn't work
 Post subject: Cancelling trailing step doesn't work Post rating: 0   New post Posted: Thu 20 Feb, 2014, 22:28 
User avatar

User rating: 3
Joined: Wed 18 Sep, 2013, 23:40
Posts: 13
Location: SloveniaSlovenia
IOrder documentation states:
"trailingStep - if < 0 then adds stop loss order without trailing step. Should be 0 or >= 10".

Passing trailingStep = 0 does nothing, passing trailingStep = -1 throws following exception:
"com.dukascopy.api.JFException: Trailing step must be >= 10 or equals to 0 (cancel) @ com.dukascopy.api.impl.connect.c.c.a(Unknown Source)"

It seems that passing trailingStep = 0 should cancel it, but it doesn't happen.

package jforex;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

import java.util.Date;

public class CancelTrailingStepTest implements IStrategy {
   
    private IContext context;
    private IEngine engine;
    private IConsole console;
   
    @Override
    public void onStart(IContext context) throws JFException {
        this.context = context;
        this.engine = context.getEngine();
        this.console = context.getConsole();

        try {
            Instrument instrument = Instrument.EURUSD;
            long waitTimeout = 2000;
            long sleepTimeout = 2000;
           
            IOrder order = engine.submitOrder("label_" + (new Date()).getTime(), instrument, IEngine.OrderCommand.BUY, 0.001, 0, 10, 0, 0);
            order.waitForUpdate(waitTimeout, IOrder.State.FILLED);
           
            Thread.sleep(sleepTimeout);
           
            if (order.getState() == IOrder.State.FILLED) {
                double sl = order.getOpenPrice() - 10 * instrument.getPipValue();

                order.setStopLossPrice(sl, OfferSide.BID, 10);
                order.waitForUpdate(waitTimeout);
               
                Thread.sleep(sleepTimeout);

                console.getOut().println("Stop loss: " + order.getStopLossPrice() + ", trailing step: " + order.getTrailingStep());
                               
                order.setStopLossPrice(sl, OfferSide.BID, 0);
                order.waitForUpdate(waitTimeout);
   
                console.getOut().println("Stop loss: " + order.getStopLossPrice() + ", trailing step: " + order.getTrailingStep());

            } else {
                console.getOut().println("Order was not filled.");
            }           
        } catch (InterruptedException ex) {
            console.getOut().println("Interrupted.");   
        }
    }

    @Override
    public void onStop() throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


 
 Post subject: Re: Cancelling trailing step doesn't work Post rating: 0   New post Posted: Wed 26 Feb, 2014, 09:28 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider removing SL and then adding it again, but this time without trailing step.


 
 Post subject: Re: Cancelling trailing step doesn't work Post rating: 0   New post Posted: Wed 26 Feb, 2014, 13:22 
User avatar

User rating: 3
Joined: Wed 18 Sep, 2013, 23:40
Posts: 13
Location: SloveniaSlovenia
API Support wrote:
Consider removing SL and then adding it again, but this time without trailing step.


I tried this, but the problem is that SL can't be modified more than once per second. So the code must cancel the SL with trailing step, then wait one second, then apply SL without trailing step, which is a bit cumbersome/slow. I wonder how the platform does it so smoothly, it seems it doesn't have one SL update per second limitation.

Anyhow, this is currently best solution, thanks!


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com