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.

Trouble creating my strategy
 Post subject: Trouble creating my strategy Post rating: 0   New post Posted: Sun 22 Jan, 2012, 21:01 
User avatar

User rating: 0
Joined: Sun 10 Jul, 2011, 15:44
Posts: 33
Location: FranceFrance
Here's what i've done for the moment but i've many errors during trying to backtest :
public class SimpleScalpingStrategy implements IStrategy {
   
   public Instrument instrument = Instrument.EURUSD;   
   public double amount = 0.1;
   public int stopLossPips = 10;
   public int takeProfitPips = 3;
   IOrder ordreBuy, ordreSell = null;
   
   private HashMap<String, IOrder> createdOrders = new HashMap <String, IOrder>();
   
   private IEngine engine;   
   private IHistory history;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();      
      this.history = context.getHistory();
      }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
      if (message.getType().equals(IMessage.Type.ORDER_FILL_OK)) {
            IOrder order = message.getOrder();
            if (order.isLong()) {
                    createdOrders.get("short").close();
            } else {
                    createdOrders.get("long").close();
            }
      }
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {   
      
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)throws JFException { 
       IBar prevBar = history.getBar(this.instrument, Period.FIVE_MINS, OfferSide.BID, 1);
       double haut = prevBar.getHigh();
       double bas = prevBar.getLow();
       double priceBUY = haut+0.00050;
       double priceSELL = bas-0.00050;
       List<IOrder> ordres = engine.getOrders();
      if(ordres.isEmpty()){
          ordreBuy = engine.submitOrder("BUYSTOP", Instrument.EURUSD, OrderCommand.BUYSTOP, amount, priceBUY);
          ordreSell = engine.submitOrder("SELLSTOP", Instrument.EURUSD, OrderCommand.SELLSTOP, amount, priceSELL);
      }
       if(ordreBuy.getState() != null || ordreSell.getState() != null){
          if(ordreBuy.getState() == State.OPENED){
             ordreBuy.setOpenPrice(priceBUY);
          }
          if(ordreSell.getState() == State.OPENED){
             ordreSell.setOpenPrice(priceSELL);
          }
       }
    }
   
}


What i wanted to :
- On each 5min candle opening, put 2 orders based on previous bar high and low
- If an order is executed, delete the other order
- If nothing is executed, change the 2 orders matching to the new candle information.


 
 Post subject: Re: Trouble creating my strategy Post rating: 0   New post Posted: Mon 23 Jan, 2012, 17:24 
User avatar

User rating: 0
Joined: Sun 10 Jul, 2011, 15:44
Posts: 33
Location: FranceFrance
Here's the source entire source code :


Attachments:
SimpleScalpingStrategy.java [2.24 KiB]
Downloaded 317 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: Trouble creating my strategy Post rating: 0   New post Posted: Mon 23 Jan, 2012, 22:29 
User avatar

User rating: 0
Joined: Sun 10 Jul, 2011, 15:44
Posts: 33
Location: FranceFrance
Here's a new version i made, it works, but not sure i've set correctly the SL and the TP.
Could someone take a look to it ?


Attachments:
SimpleScalpingStrategy.java [2.52 KiB]
Downloaded 329 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: Trouble creating my strategy Post rating: 0   New post Posted: Tue 24 Jan, 2012, 09:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See comments in the changed places in the source code.
package jforex.strategies;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Queue;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentLinkedQueue;

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

//if we use console, we need @RequiresFullAccess for JForex-API 2.6.38 with java versions > 1.6.0_26
@RequiresFullAccess
public class SimpleScalpingStrategy implements IStrategy {
   
   @Configurable("")
   public Instrument instrument = Instrument.EURUSD;   
   @Configurable("")
   public Period period = Period.FIVE_MINS;
   
   public double amount = 0.1;
   public int stopLossPips = 10;
   public int takeProfitPips = 3;
   IOrder ordreBuy, ordreSell = null;
   
   
   private IEngine engine;   
   private IHistory history;
   private IConsole console;
   
   private Queue<IOrder> justFilledOrders = new ConcurrentLinkedQueue<IOrder>();
   
   //for logging
   @SuppressWarnings("serial")
   public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
      {
         setTimeZone(TimeZone.getTimeZone("GMT"));
      }
   };
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();      
      this.history = context.getHistory();
      this.console = context.getConsole();
      }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
      //log messages to see what happens
      console.getOut().println(sdf.format(message.getCreationTime()) + " " +message);
      
      //in JForex-API 2.6.38 one can't change orders in onMessage, so we enqueue order change request and process it in next onTick
      if(message.getType() == IMessage.Type.ORDER_FILL_OK){
         justFilledOrders.add(message.getOrder());
      }
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {   
      
       //filter ticks
       if(instrument != this.instrument){
          return;
       }
       
       //process just filled orders
       while(!justFilledOrders.isEmpty()){
          IOrder order = justFilledOrders.poll();
          if(ordreBuy == order){
             ordreBuy.setStopLossPrice(ordreBuy.getOpenPrice()-stopLossPips * instrument.getPipValue());
             ordreBuy.setTakeProfitPrice(ordreBuy.getOpenPrice()+takeProfitPips * instrument.getPipValue());
          }
          if(ordreSell == order){
             ordreSell.setStopLossPrice(ordreSell.getOpenPrice()+stopLossPips * instrument.getPipValue());
             ordreSell.setTakeProfitPrice(ordreSell.getOpenPrice()+takeProfitPips * instrument.getPipValue());
          }
       }
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)throws JFException { 
       
       //filter bars
       if(period != this.period || instrument != this.instrument){
          return;
       }
       
       IBar prevBar = history.getBar(this.instrument, Period.FIVE_MINS, OfferSide.BID, 1);
       double haut = prevBar.getHigh();
       double bas = prevBar.getLow();
       double priceBUY = haut+0.00050;
       double priceSELL = bas-0.00050;
       List<IOrder> ordres = engine.getOrders();
      if(ordres.isEmpty()){
          ordreBuy = engine.submitOrder("BUYSTOP", Instrument.EURUSD, OrderCommand.BUYSTOP, amount, priceBUY);
          ordreSell = engine.submitOrder("SELLSTOP", Instrument.EURUSD, OrderCommand.SELLSTOP, amount, priceSELL);
      }
       if(ordreBuy.getState() != null || ordreSell.getState() != null){
          if(ordreBuy.getState() == State.OPENED){
             ordreBuy.setOpenPrice(priceBUY);
          }
          if(ordreSell.getState() == State.OPENED){
             ordreSell.setOpenPrice(priceSELL);
          }
          if(ordreBuy.getState() == State.FILLED && ordreSell.getState() == State.OPENED){
             engine.getOrder("SELLSTOP").close();
          }
          if(ordreSell.getState() == State.FILLED && ordreBuy.getState() == State.OPENED){
             engine.getOrder("BUYSTOP").close();
          }
       }
    }
   
}


Attachments:
SimpleScalpingStrategy.java [3.97 KiB]
Downloaded 337 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: Trouble creating my strategy Post rating: 0   New post Posted: Tue 24 Jan, 2012, 11:24 
User avatar

User rating: 0
Joined: Sun 10 Jul, 2011, 15:44
Posts: 33
Location: FranceFrance
Thank you, now it works.


 
 Post subject: Re: Trouble creating my strategy Post rating: 0   New post Posted: Tue 24 Jan, 2012, 18:31 

User rating: 2
Joined: Wed 18 May, 2011, 17:36
Posts: 49
Location: FranceFrance
Hi,
I backtest your strat on the last week (Historical tester) with default parameters: it doesn't open so much trade than it should.
Absolutely all the short trades are losing with -most of the time- a "-0.8" or "-1" pip of loss.
There is 2 orders which are moving if the previous high and low are not broken but sometimes, there is no line order for buying...
And at last, it miss all the profit during the big trend if the candle are trending with approximatively the same size...
sorry for my poor english, hope to be understood !
cheers,
Math


 
 Post subject: Re: Trouble creating my strategy Post rating: 0   New post Posted: Wed 25 Jan, 2012, 08:25 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider logging the relevant values to see what happens - what execution paths get taken and why, see:
https://www.dukascopy.com/wiki/#IConsole/Logging_values


 

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