Dukascopy
 
 
Wiki JStore Search Login

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

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

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

Editing the Stop Loss of all long and shor positions at once
 Post subject: Editing the Stop Loss of all long and shor positions at once Post rating: 0   New post Posted: Thu 30 Jun, 2011, 02:19 

User rating: -
Hi,

in the OnBar or the OnTick method, the following code allows editing the stop loss of an open position:

Quote:
if (engine.getOrders().size() > 0) {
for (IOrder orderInMarket : engine.getOrders()) {

if (orderInMarket.isLong() && (order.getState().equals(IOrder.State.FILLED))) {
(...)
order.setStopLossPrice(value, OfferSide.ASK, 10);
}
}

if (!orderInMarket.isLong() && (order.getState().equals(IOrder.State.FILLED))) {
(...)
order.setStopLossPrice(newvas, OfferSide.BID, 10);
}
}
}
}


The code only edits one position though. As my strategy opens up up to 10 positions, i would like all the short positions to have one common stop loss and the long positions to have a common one as well. Is there any way to edit this code to make that happen? Otherwise i would only be able to have one long and one short position at a time.

Thanks in advance. :)


 
 Post subject: Re: Editing the Stop Loss of all long and shor positions at Post rating: 0   New post Posted: Thu 30 Jun, 2011, 13:03 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Check if this example strategy gives you an idea how to do this:
package jforex.strategies.oneorder;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

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

/**
 * The strategy creates 10 orders onStart and on every onBar updates their stop loss
 *
 */
public class TenOrdersStopLoss implements IStrategy {

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

   private Instrument instrument = Instrument.EURUSD;
   private Period period = Period.TEN_MINS;
   
    @SuppressWarnings("serial")
   private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};
   
   @Override
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      console = context.getConsole();
      history = context.getHistory();
      console.getOut().println("Start");
      
      //create 5 buy orders and 5 sell orders
      for(int i = 0; i < 10; i++){
         engine.submitOrder("order" + (i+1), instrument, i < 5 ? OrderCommand.BUY : OrderCommand.SELL, 0.001 * (i+1));
      }
   }

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

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if(period != this.period || instrument != this.instrument)
         return;
      //update stop loss for every filled order
      for(IOrder o : engine.getOrders()){
         if(o.getState() == IOrder.State.FILLED){
            o.setStopLossPrice(getMyStopLossPrice(o));
         }
      }
   }
   
   //TODO: add your own logic
   private double getMyStopLossPrice(IOrder o) throws JFException{
      //stop loss at 5 pips
      return o.isLong() ? history.getLastTick(instrument).getBid() - 0.0005 : history.getLastTick(instrument).getAsk() + 0.0005;
   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      //print all order related messsages
      if (message.getOrder() != null)
         print(sdf.format(message.getCreationTime()) + " " + message.getOrder().getLabel() + " " + message.getContent());
   }

   private void print(Object o){
      console.getOut().println(o);
   }

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

   @Override
   public void onStop() throws JFException {
      for (IOrder o : engine.getOrders())
         o.close();
   }

}


 
 Post subject: Re: Editing the Stop Loss of all long and shor positions at Post rating: 0   New post Posted: Thu 30 Jun, 2011, 14:19 

User rating: -
Perfect! Thanks a lot. :)


 

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