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.

Automatic re-entry of positions after a stop
 Post subject: Automatic re-entry of positions after a stop Post rating: 0   New post Posted: Fri 25 Feb, 2011, 17:42 

User rating: 0
Joined: Fri 19 Nov, 2010, 16:34
Posts: 2
Dear Support,

I am looking to change my auto-trading program to re-enter a position if it gets stopped out and the prices then reverses past the original re-entry point.

For example, I would like to place a trade for EUR/USD at 1.35000 with a stop-loss at 1.34500. If this trade then gets stopped out, I would like to re-enter if it goes above 1.35000 again.

My trading points are decided by looking at graphs, so I am fine entering the parameters manually. What would be the best way to implement this plan?

The problem I had is how to determine that the stop got triggered.

Thank you in advance.


 
 Post subject: Re: Automatic re-entry of positions after a stop Post rating: 0   New post Posted: Fri 10 Jun, 2011, 13:52 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You might consider checking in onTick method if the previous order has been closed. Consider the following strategy:

package jforex.strategies.oneorder;

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

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

public class OneOrderStopTrigger implements IStrategy {

   private IConsole console;
   private IEngine engine;
   private IHistory history;
   
   private IOrder order;
   
   private final String label = "OneOrder";
   private SimpleDateFormat dateFormat;
   
   @Override
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      console = context.getConsole();
      history = context.getHistory();
      console.getOut().println("Start");
      
      dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
      
      //the first order
      submitOrder(history.getLastTick(Instrument.EURUSD));
   }

   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      
      if(!instrument.equals(Instrument.EURUSD))
         return;
      
      if (!engine.getOrders().contains(order)){
         //order is likely to have been closed on SL - create a new order
         submitOrder(tick);
      }
   }
   
   private void submitOrder(ITick tick) throws JFException{
      order = engine.submitOrder(label, Instrument.EURUSD, OrderCommand.BUYSTOP,
            0.01, //amount
            tick.getAsk() + 0.0005, //price of Buy stop
            20, //slippage
            history.getLastTick(Instrument.EURUSD).getBid() - 0.0005, //SL
            0);//TP
   }

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      // TODO Auto-generated method stub

   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      console.getOut().println(dateFormat.format(message.getCreationTime()) + " " + message.getContent());

   }

   @Override
   public void onAccount(IAccount account) throws JFException {
      // TODO Auto-generated method stub

   }

   @Override
   public void onStop() throws JFException {
      if (engine.getOrder(label) != null)
         engine.getOrder(label).close();

   }

}


 

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