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.

Timed entry
 Post subject: Timed entry Post rating: 0   Post Posted: Mon 29 Aug, 2011, 04:31 
User avatar

User rating: 0
Joined: Mon 29 Aug, 2011, 04:00
Posts: 4
Location: Canada, Halifax
Hello,

Looking for a strategy that simply trades the difference between price at two different times.

ie: if price at 7:15 is 3 pips greater than price at 7:00, take a long position at 7:15 with preset TP/SL. If reverse, take a short position.

Many Thanks,

Nachtschwarmer


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Tue 30 Aug, 2011, 07:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please take a look at the following strategy:
package jforex.strategies;

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

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

public class TradeOnDirection implements IStrategy {

   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Instrument")
   public Period period = Period.TEN_SECS;   
   @Configurable("Amount")
   public double amount = 0.001;
   @Configurable("Stop loss")
   public int stopLossPips = 3;
   @Configurable("Take profit")
   public int takeProfitPips = 5;
   
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   
   private int orderCount;
   
   @Override
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();      

      console.getOut().println("start");
   }

   @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( this.instrument != instrument || this.period != period){
         return;      
      }
      //take previous bar from historical data
      IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);

      OrderCommand cmd = bidBar.getClose() > prevBar.getClose()
         ? OrderCommand.BUY
         : OrderCommand.SELL;
      
      submitOrder(cmd);
   }
   
   private IOrder submitOrder(OrderCommand orderCmd) throws JFException {

      double stopLossPrice, takeProfitPrice;

      // Calculating stop loss and take profit prices
      if (orderCmd == OrderCommand.BUY) {
         stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
      } else {
         stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
      }

      // Submitting an order for the specified instrument at the current market price
      return engine.submitOrder("order" + ++orderCount, this.instrument, orderCmd, this.amount, 0, 5, stopLossPrice, takeProfitPrice);
   }
   
   private double getPipPrice(int pips) {
      return pips * this.instrument.getPipValue();
   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      //if the message is related to order print its content
      if (message.getOrder() != null){
         console.getOut().println(message.getOrder().getLabel() + " " + message.getType() + " " + message.getContent());
      }
   }

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

   }

   @Override
   public void onStop() throws JFException {
      //close all orders on strategy stop
      for(IOrder o : engine.getOrders()){
         o.close();
      }
   }

}
For more on strategies, start here:
https://www.dukascopy.com/wiki/index.php?title=Overview


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Thu 08 Sep, 2011, 02:18 
User avatar

User rating: 0
Joined: Mon 29 Aug, 2011, 04:00
Posts: 4
Location: Canada, Halifax
Thank you for the reply.

This code opens trades on every occurrence at the set configurable time period (5 min, hourly, etc) 24 hours a day. Is there a way to add daily configurable times during which the strategy will open trades between?

Example: Strategy only opens trades between 0:00 and 7:00 daily, with trades only closing on Take Profit/Stop Loss.

Another solution would be a universal code for any strategy that starts and stops a strategy the same times daily while leaving open trades open until their SL/TP is realized, regardless of time.

Thank You Again

Nachtschwarmer


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Thu 08 Sep, 2011, 12:34 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See the following example:
viewtopic.php?f=31&t=42386


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Wed 19 Oct, 2011, 16:25 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Thanks for the program.
I have one request.
Could you tell me, how to insert the trailing step into the program?
I know
setStopLossPrice(double price,
OfferSide side,
double trailingStep)
but don't know how and where to insert it into the program.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Thu 20 Oct, 2011, 07:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider doing something something similar to this:
      IOrder order = engine.submitOrder("order" + ++orderCount , this.instrument, orderCmd, this.amount, 0, 5, 0, takeProfitPrice);
      //wait max 2 secs for order to get opened
      order.waitForUpdate(2000);
      //normally state here should be OPENED, but for a more general case we add also FILLED
      if(order.getState() == IOrder.State.OPENED || order.getState() == IOrder.State.FILLED){
         order.setStopLossPrice(stopLossPrice, OfferSide.BID, 10);
      }
See:
https://www.dukascopy.com/wiki/index.php ... ting_order


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Sun 23 Oct, 2011, 00:25 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Thanks, working fine.

I don't understand this line:

//take previous bar from historical data
IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);


Why do you use number 2, why not number 1?
shift - number of candle back in time staring from current bar. 1 - previous bar, 2 - current bar minus 2 bars and so on
1 means the previous bar.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Mon 24 Oct, 2011, 07:31 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
jozsi74 wrote:
Why do you use number 2, why not number 1?
It depends on what we take as current - bar with shift=2 is used for "previous bar" in onBar, since:
0 - bar currently formed (just started)
1 - bar that just arrived in onBar (since it just got finished)
2 - previous bar - one bar before the onBar bar.
Thus, in general shift=1 returns the pervious bar, but if by "previous bar" we mean - previous from onBar bar, then we use shift 2.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Mon 24 Oct, 2011, 20:41 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
So, this strategy checks the last fully finished bar? If this bar is green then opens long position, if this bar is red, then opens short position?


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Tue 25 Oct, 2011, 09:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
jozsi74 wrote:
So, this strategy checks the last fully finished bar? If this bar is green then opens long position, if this bar is red, then opens short position?
Yes


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Tue 25 Oct, 2011, 19:06 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
I wanted to try this program with custom time periods, but it is not working.

public Period period = Period.createCustomPeriod(Unit.Hour, 12);

If I set up 2 or 6 or 8 or 12 hours, then nothing happening on the historical test, however it is working with 1 hour.
What is the problem?


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Wed 26 Oct, 2011, 07:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
In order to use custom periods, you have to subscribe to them, the same time passing the business logic that should be executed on the particular custom period, see:
https://www.dukascopy.com/wiki/index.php ... tom_period


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Wed 26 Oct, 2011, 13:03 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
In live trading I could set up 20 seconds and it was working fine.
But in the historical tester nothing is happening.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Thu 27 Oct, 2011, 12:06 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please use @RequiresFullAccess annotation (this is due to the fact that the historical value cache gets read from a local directory which is not in IContext.getFilesDir()). As in the following example:
https://www.dukascopy.com/wiki/index.php ... te_to_file


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Sun 30 Oct, 2011, 12:42 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
I cannot make it.
Even I cannot stop the running of the historical tester if I press cancel.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Mon 31 Oct, 2011, 08:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
jozsi74 wrote:
I cannot make it.
Even I cannot stop the running of the historical tester if I press cancel.
Please check what you receive on Java console. See:
viewtopic.php?f=81&t=39073&p=50123


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Tue 06 Dec, 2011, 16:25 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
I tried so many times, still not working.
Why cannot set up simply more time periods, like 8, 12 hours, 2, 3 days etc in the dialog box?


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Wed 07 Dec, 2011, 09:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
jozsi74 wrote:
I tried so many times, still not working.
Why cannot set up simply more time periods, like 8, 12 hours, 2, 3 days etc in the dialog box?
Please provide an example strategy which you are launching and a precise launch scenario. Also please describe what you expect the strategy to do and how it actually behaves.


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Sun 11 Dec, 2011, 06:37 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
I found a solution.
Little bit later I describe the situation.

But now I have a new strange problem.
Let's take the original probgram what you wrote here on Tue 30 Aug, 2011, 07:51
TradeOnDirection

Previously it worked very well with different instruments and time periods.
But since yesterday it is not working with XAUUSD and Daily. It is working well with XAUUSD Hourly, 4 Hours etc, but not daily.
It is working fine with other instruments like EURUSD, XAGUSD daily.
When I start with XAUUSD and Daily the tester is running the time is passing if see the report, but the program is not doing anything.
What can be the problem?


 
 Post subject: Re: Timed entry Post rating: 0   Post Posted: Mon 12 Dec, 2011, 16:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please thy this strategy

package jforex.strategies;
 
import java.text.SimpleDateFormat;
import java.util.TimeZone;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
 
public class TradeOnDirection implements IStrategy {
 
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Instrument")
   public Period period = Period.TEN_SECS;   
   @Configurable("Amount")
   public double amount = 0.001;
   @Configurable("Stop loss")
   public int stopLossPips = 3;
   @Configurable("Take profit")
   public int takeProfitPips = 5;
   
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   
   private int orderCount;
   
   @Override
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
   }
 
   @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( !this.instrument.equals(instrument) || !this.period.equals(period) ){
         return;     
      }

      //take previous bar from historical data
      IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);
     
      console.getOut().println(bidBar.getClose() + " " + prevBar.getClose());
 
      OrderCommand cmd = bidBar.getClose() > prevBar.getClose()
         ? OrderCommand.BUY
         : OrderCommand.SELL;
       
      submitOrder(cmd);
   }
   
   private IOrder submitOrder(OrderCommand orderCmd) throws JFException {
 
      double stopLossPrice, takeProfitPrice;
 
      // Calculating stop loss and take profit prices
      if (orderCmd == OrderCommand.BUY) {
         stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
      } else {
         stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
      }
 
      // Submitting an order for the specified instrument at the current market price
      return engine.submitOrder("order" + ++orderCount, this.instrument, orderCmd, this.amount, 0, 5, stopLossPrice, takeProfitPrice);
   }
   
   private double getPipPrice(int pips) {
      return pips * this.instrument.getPipValue();
   }
 
   @Override
   public void onMessage(IMessage message) throws JFException {
      //if the message is related to order print its content
      if (message.getOrder() != null){
         console.getOut().println(message.getOrder().getLabel() + " " + message.getType() + " " + message.getContent());
      }
   }
 
   @Override
   public void onAccount(IAccount account) throws JFException {
 
   }
 
   @Override
   public void onStop() throws JFException {
      //close all orders on strategy stop
      for(IOrder o : engine.getOrders()){
         o.close();
      }
   }
 
}


Attachments:
TradeOnDirection.java [3.13 KiB]
Downloaded 345 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.
 

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