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.

Expiry Date for filled orders
 Post subject: Expiry Date for filled orders Post rating: 0   New post Posted: Tue 29 Nov, 2011, 02:35 

User rating: 0
Joined: Sat 09 Jul, 2011, 10:50
Posts: 7
Location: DE
Hi
I would please like to know an effective way of closing filled orders automaticaly if they have not been close by sl or tp after x amount of bars have passed.

Thanks
froid


 
 Post subject: Re: Expiry Date for filled orders Post rating: 0   New post Posted: Wed 30 Nov, 2011, 10:09 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider the following strategy (see comments in the source code):
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 market orders with SL and TP on its start.
 * In case if the order does not get closed by SL or TP within 40 seconds after
 * its fill, the order expires -> it gets closed.
 *
 */
@RequiresFullAccess
public class MarketOrderExpire implements IStrategy {

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

   @Configurable("")
   public Period period = Period.TEN_SECS;
   @Configurable("")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Order expire time (in bars)")
   public int expireTimeInBars = 4;
   @Configurable("Order count")
   public int orderCount = 10;
   
   private final String label = "order";
   private SimpleDateFormat sdf;

   @Override
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      console = context.getConsole();
      history = context.getHistory();

      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

      double price = history.getLastTick(instrument).getBid();
      print("Start");

      for (int i = 1; i < orderCount + 1; i++){
         engine.submitOrder(label + i, instrument, OrderCommand.BUY, 0.001, 0, 20,
               price - i *instrument.getPipValue(), //SL
               price + i *instrument.getPipValue()); //TP
      }
   }

   @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(instrument != this.instrument || period != this.period){
         return;
      }
      //iterate through all active orders
      for(IOrder order : engine.getOrders()){
         if(order.getState() != IOrder.State.FILLED){
            continue;
         }
         long orderExpireTime = order.getFillTime() + period.getInterval() * expireTimeInBars;
         long lastBarEndTime = bidBar.getTime() + period.getInterval();
         //if order expired within the period of the last bar, then close it
         if(orderExpireTime < lastBarEndTime){
            print(order.getLabel() + " expired: " + sdf.format(orderExpireTime) + " < "+ sdf.format(lastBarEndTime));
            order.close();
         }
      }
      

   }

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

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

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

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

}


Attachments:
MarketOrderExpire.java [2.66 KiB]
Downloaded 279 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: Expiry Date for filled orders Post rating: 0   New post Posted: Fri 16 Dec, 2011, 19:33 

User rating: 0
Joined: Sat 09 Jul, 2011, 10:50
Posts: 7
Location: DE
ok thanks,that works quite well now.
Im trying to now make sure that my algorithm does not trade 24 bars after the closing of the last position, but the method getOrders does not seem to contain past orders that are CLOSED. Is there a more elegant solution that storing the time of the last closed order in a global field?

thanks
froid


 
 Post subject: Re: Expiry Date for filled orders Post rating: 0   New post Posted: Tue 20 Dec, 2011, 09:10 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It is possible to get all orders from last 24 hours using the following code:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

...
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(tick.getTime());
        cal.add(Calendar.HOUR_OF_DAY, -24);

        List<IOrder> orders = history.getOrdersHistory(Instrument.EURUSD, cal.getTimeInMillis(), tick.getTime());
        if (orders.size() == 0) {
            // trade
        }
    }


 

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