Dukascopy
 
 
Wiki JStore Search Login

Optimization problem
 Post subject: Optimization problem Post rating: 0   New post Posted: Sat 28 Apr, 2012, 12:47 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Dukascopy Bank ver. 2.15.3
JForex API ver. 2.6.65

If I am using optimization in the Historical Tester and use at least 2 different starting parameters, then the tester is not working properly, the problem it opens too many positions.
If I am using 1 test only, then it is fine.


 
 Post subject: Re: Optimization problem Post rating: 0   New post Posted: Tue 08 May, 2012, 11:13 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
The problem is still there ...


 
 Post subject: Re: Optimization problem Post rating: 0   New post Posted: Wed 09 May, 2012, 09:01 
JForex Master
User avatar

User rating:
Joined: Wed 16 Sep, 2009, 18:23
Posts: 1054
Location: Geneva, Switzerland
The problem is in the code of the strategy most probably. Please provide the strategy to investigate


 
 Post subject: Re: Optimization problem Post rating: 0   New post Posted: Mon 14 May, 2012, 21:33 

User rating: 0
Joined: Tue 11 Oct, 2011, 20:13
Posts: 64
Location: India,
Let's take this strategy.
With the previous JForex versions it was working fine.
I use optimization at the historical tester, I setup the stop loss from 10 to 100 step 10.
Take profit 200.
Trailing step 10.
You'll see it will open too many positions at the same time.
What's wrong?

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 = 10;
   @Configurable("Take profit")
   public int takeProfitPips = 200;
   @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, 4), 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;
   
      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: Optimization problem Post rating: 0   New post Posted: Fri 18 May, 2012, 16:01 
JForex Master
User avatar

User rating:
Joined: Wed 16 Sep, 2009, 18:23
Posts: 1054
Location: Geneva, Switzerland
We are looking into this


 
 Post subject: Re: Optimization problem Post rating: 0   New post Posted: Wed 23 May, 2012, 14:43 
JForex Master
User avatar

User rating:
Joined: Wed 16 Sep, 2009, 18:23
Posts: 1054
Location: Geneva, Switzerland
Fixed. Will be out in the next release on demo.


 

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