Dukascopy
 
 
Wiki JStore Search Login

JFOREX-3670 Tester is not working properly
 Post subject: JFOREX-3670 Tester is not working properly Post rating: 0   New post Posted: Sat 31 Mar, 2012, 19:40 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
I tested the attached strategy in
Dukascopy 2.15
JForex API 2.6.63
and the tester is not working properly.
I ran the test from 2012. 1. 1. to 2012. 3. 31.
and it opened only 1 position.
However it is working fine with the previous version (Dukascopy 2.14.28.6 JForex API 2.6.60.1) opened positions every day.

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.DAILY;   
   @Configurable("Amount")
   public double amount = 0.001;
   @Configurable("Stop loss")
   public int stopLossPips = 30;
   @Configurable("Take profit")
   public int takeProfitPips = 20;
   
   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();
      }
   }
 
}


 
 Post subject: Re: Tester is not working properly Post rating: 0   New post Posted: Sat 31 Mar, 2012, 20:50 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Another problem with the optimization.
If I setup more options, like stop loss from 20 to 100 step 10
then the tester is opening too many positions at the same time. It will open as many positions as many options I am testing.
For example in the mentioned case there are 9 options (stop loss from 20 to 100 step 10) so when I start it, it is opening 9 positions all the time, instead of 1 position.
Let's see other case. If I setup stop loss from 20 to 40 step 10, it means 3 options and the tester will open 3 positions all the time, instead of 1.
So something is completely wrong here.


See the attached strategy and try it.


package jforex.strategies;
 
import java.text.SimpleDateFormat;
import java.util.TimeZone;
 
import java.util.*;
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;

 
 
public class trend implements IStrategy {
 
   @Configurable("Instrument")
   public  Instrument instrument = Instrument.XAGUSD;
   
    public Period period;
   
 
   @Configurable("Amount")
   public double amount = 0.001;
   @Configurable("Stop loss")
   public int stopLossPips = 20;
   @Configurable("Take profit")
   public int takeProfitPips = 20;
   @Configurable("TrailingStep")
   public int TrailingStep = 10;
   
 
   
   
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IIndicators indicators;

   
   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");
   
 
 
   context.subscribeToBarsFeed(Instrument.XAGUSD, Period.createCustomPeriod(Unit.Hour, 8), OfferSide.BID,
           new IBarFeedListener() {
               public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bidBar) {
                 
                 
 
   try {
       
       
     IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);
         
     OrderCommand cmd = bidBar.getClose() > prevBar.getClose()
         ? OrderCommand.BUY
         : OrderCommand.SELL;
       
      submitOrder(cmd);
   
   
   }
       
       
           
   catch (JFException jfe) {
                         console.getOut().println(jfe.toString());
                      }
               
             
             
               
               }
           }
  );
 
               }
   
   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);
         
      for(IOrder o : engine.getOrders()){
         if (!o.isLong() && o.getInstrument().equals(instrument)) {
              print("Closing Short position");
           o.close();
         }
      }
     
     }
     else {
         stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
         takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
         
        for(IOrder o : engine.getOrders()){
            if (o.isLong() && o.getInstrument().equals(instrument)) {
             print("Closing Long position");
             o.close();
            }
       }
      }
 
     
      // Submitting an order for the specified instrument at the current market price
         
       
      IOrder order = engine.submitOrder("order" + ++orderCount , this.instrument, orderCmd, this.amount, 0, 10, stopLossPrice, 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, TrailingStep);
      }
     
      return order;
      }
       
       
   
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
           



           
   @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.equals(Instrument) || this.period != period){
         return;     
      }
     
     
     
   }
 
   
   
       
   
 
   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 {
     
     
   }
 
     public void print(String message) {
        console.getOut().println(message);
    }
}


 
 Post subject: Re: Tester is not working properly Post rating: 0   New post Posted: Mon 02 Apr, 2012, 09:54 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please do not post two different issues in a one topic.


 
 Post subject: Re: JFOREX-3670 Tester is not working properly Post rating: 0   New post Posted: Mon 02 Apr, 2012, 11:02 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Sorry, but I thought the problems are related with each other.
Then please move them to 2 different topics.


 

Jump to:  

cron
  © 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