Dukascopy
 
 
Wiki JStore Search Login

Change TP and SL simultaneously
 Post subject: Change TP and SL simultaneously Post rating: 0   New post Posted: Mon 01 Oct, 2012, 00:44 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
I have a Live open position in a JForex Strategy, with both TP and SL set.

It's been longer than 1 second since the position was opened.

Am I allowed to alter both the TP price, and the SL price simultaneously?

Will this violate the "pacing restrictions".

Or, do I have to change the TP first, and then wait 1 second before changing the SL ?

Thanks!


 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Mon 01 Oct, 2012, 14:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Try the following strategy:
package jforex.orders;

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

public class SLTPmarketOrder implements IStrategy {

    IConsole console;
    IEngine engine;
    IHistory history;
    Instrument instrument = Instrument.EURUSD;

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        engine = context.getEngine();
        history = context.getHistory();
       
        IOrder order = engine.submitOrder("order1", instrument, OrderCommand.BUY, 0.001);
        order.waitForUpdate(2000, IOrder.State.FILLED);
        double ask = history.getLastTick(instrument).getAsk();
        order.setStopLossPrice(ask - instrument.getPipValue() * 10);
        order.setTakeProfitPrice(ask + instrument.getPipValue() * 10);

    }

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

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

    @Override
    public void onMessage(IMessage message) throws JFException {
        if(message.getOrder() != null){
            console.getOut().println(message);
        }
    }

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

    @Override
    public void onStop() throws JFException {}
}



Attachments:
SLTPmarketOrder.java [1.34 KiB]
Downloaded 341 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Mon 01 Oct, 2012, 18:28 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
In initially submitting my Market Orders with Stop Loss and Take Profit, I am just doing this.

The TP and SL are assigned at the same time the Market Order is submitted.

Later, when I want to move my TP and SL I was just asking whether they could also be assigned in the same call.

   final public static class SubmitMarketOrderTask implements Callable<Object> {
      
      boolean buyFlag = false;
      IEngine engine = null;
      Instrument instrument = null;
      double sizeInMillions = 0.0d;
      double price = 0.0d;
      double stopLossPrice = 0.0d;
      double takeProfitPrice = 0.0d;
      
      public SubmitMarketOrderTask(boolean buyFlagArg, IEngine engineArg,
            Instrument instrumentArg,
            double sizeInMillionsArg,
            double priceArg,
            double stopLossPriceArg,
            double takeProfitPriceArg) {
         // constructor
         buyFlag = buyFlagArg;
         engine = engineArg;
         instrument = instrumentArg;
         sizeInMillions = sizeInMillionsArg;
         price = priceArg;
         stopLossPrice = stopLossPriceArg;
         takeProfitPrice = takeProfitPriceArg;
         System.out.println("LotSizeMillions: "+sizeInMillions+" Stop: "+stopLossPrice+" Target: "+takeProfitPrice);
         System.out.println("SubmitMarketOrderTask ctor...");
      }
      
      public Object call() throws Exception { // returns an IOrder
         IOrder newOrder = null;
         try {
            System.out.println("SubmitMarketOrderTask start...");
            if (sizeInMillions>SIZE_LIMIT) {
               System.out.println("SubmitMarketOrderTask requested size: "+sizeInMillions+" is too large...");
               System.out.println("... setting MAX size to: "+SIZE_LIMIT+" and submitting...");
               sizeInMillions = SIZE_LIMIT;
               //return null;
            }
            //OrderDelay.waitOrderOpMinDelay();
            if (buyFlag) {
               System.out.println("SubmitMarketOrderTask BUY...");
               newOrder = engine.submitOrder(orderLabelPrefix+"_LONG",
                  instrument,
                  IEngine.OrderCommand.BUY,
                  sizeInMillions,
                  price,
                  SLIPPAGE, // 10 pips
                  stopLossPrice,
                  takeProfitPrice);
               System.out.println("   SubmitMarketOrderTask after BUY...");
            }
            else { // sell
               System.out.println("SubmitMarketOrderTask SELL...");
               newOrder = engine.submitOrder(orderLabelPrefix+"_SHORT",
                     instrument,
                     IEngine.OrderCommand.SELL,
                     sizeInMillions,
                     price,
                     SLIPPAGE, // 10 pips
                     stopLossPrice,
                     takeProfitPrice);
               System.out.println("   SubmitMarketOrderTask after SELL...");
            }
            if (newOrder==null) {
               System.out.println("   SubmitMarketOrderTask newOrder is NULL");
            }
         }
         catch(Exception e) {
            e.printStackTrace();
            throw e;
         }
         System.out.println("SubmitMarketOrderTask end...");
         return newOrder;
      }
   }
   



 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Mon 01 Oct, 2012, 18:45 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
In other words, are you saying this code will generate an error? That they cannot be changed simultaneously?

   final public static class AdjustStopLossAndTakeProfitTask implements Callable<Void> {
      
      IOrder order = null;
      double takeProfitPrice = 0.0d;
      double stopLossPrice = 0.0d;
      
      public AdjustStopLossAndTakeProfitTask(IOrder orderArg, double stopLossPriceArg, double takeProfitPriceArg) {
         // constructor
         order = orderArg;
         stopLossPrice = stopLossPriceArg;
         takeProfitPrice = takeProfitPriceArg;
      }
      
      public Void call() throws Exception {
         try {
            order.setStopLossPrice(stopLossPrice);
            // no delay...
            order.setTakeProfitPrice(takeProfitPrice); // will generate an error ?
         }
         catch(Exception e) {
            e.printStackTrace();
            throw e;
         }
         return null;
      }
   }
   



 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Mon 01 Oct, 2012, 19:35 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
hyperscalper wrote:
In other words, are you saying this code will generate an error?
We posted the example so that you can both make sure that it is allows to change SL and TP simultaneously and that it trivial to check that. What makes you think that the snippet you posted will cause an error if our sample which does the same order actions did not? Did you test your example strategy? If you encounter any exception then please post all the relevant messages here.


 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Mon 01 Oct, 2012, 19:39 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
hyperscalper wrote:
In other words, are you saying this code will generate an error?
We posted the example so that you can both make sure that it is allows to change SL and TP simultaneously and that it trivial to check that. What makes you think that the snippet you posted will cause an error if our sample which does the same order actions did not? Did you test your example strategy? If you encounter any exception then please post all the relevant messages here.


 
 Post subject: Re: Change TP and SL simultaneously? Post rating: 0   New post Posted: Tue 02 Oct, 2012, 01:32 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
OK, I'll do it by Trial and Error, as usual.

There is a lack of clarity on limitations on order operations, so I'll just try it and see...

My example submits a Market order and both TP and SL simultaneously. Yours does not.

So I'll try changing SL and TP simultaneously, and report to you the errors I get, if any...

Thanks!


 

Jump to:  

  © 1998-2024 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