Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Contest Compliant Strategy Example [September]
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=21&t=21691
Page 1 of 1

Author:  Contest Support [ Thu 26 Aug, 2010, 14:29 ]
Post subject:  Contest Compliant Strategy Example [September]

package jforex;

import java.util.Date;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.IEngine.OrderCommand;

public class MovingAverageExample implements IStrategy {

   // these are strategy interfaces which are used to execute trades, request indicators, print messages
   private IEngine engine;
   private IIndicators indicators;
   private IConsole console;

   // these are strategy parameters
   private Instrument myInstrument = Instrument.EURUSD;
   private Period myPeriod = Period.ONE_HOUR;
   private int fastMAPeriod = 7;
   private int slowMAPeriod = 14;
   private boolean tradingAllowed;
   private double takeProfit = 10 / Math.pow(10, myInstrument.getPipScale());
   private double stopLoss = 20 / Math.pow(10, myInstrument.getPipScale());
   private boolean loggingEnabled = true;

   @Override
   public void onStart(IContext context) throws JFException {

      // we initialize interfaces in the onStart method
      engine = context.getEngine();
      indicators = context.getIndicators();
      console = context.getConsole();
   }

   
   // we put the trading algorithms either in onBar or onTick methods. Once the bar is closed, the script will be run

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar,
         IBar bidBar) throws JFException {


      // in the line below we indicate that we are oging to trade on EURUSD and on 1 min chart. And we will trade only when the trade will be allowed
      if (myPeriod == period && myInstrument == instrument && tradingAllowed) {
         

         double fastMA = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, fastMAPeriod, 0);
         double fastMAonPreviousBar = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, fastMAPeriod, 1);
         double slowMA = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, slowMAPeriod, 0);
         double slowMAonPreviousBar = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, slowMAPeriod, 1);
         
         
         // condition when we BUY
         if (fastMAonPreviousBar < slowMAonPreviousBar && fastMA > slowMA) {

            engine.submitOrder("BUY", instrument, OrderCommand.BUY, 0.1, 0, 5, bidBar.getClose() - stopLoss, bidBar.getClose() + takeProfit);

            print("BUY signal! " + stopLoss + takeProfit + new Date(bidBar.getTime()));
            
         // condition when we SELL
         } else if (fastMAonPreviousBar > slowMAonPreviousBar
               && fastMA < slowMA) {

            engine.submitOrder("SELL", instrument, OrderCommand.SELL, 0.1, 0, 5, askBar.getClose() + stopLoss, askBar.getClose() - takeProfit);

            print("SELL signal!" + stopLoss + takeProfit + new Date(bidBar.getTime()));
         }

      }

   }
   
   public void print(String string) {

      if (loggingEnabled) console.getOut().println(string);
      
   }
   
   @Override
   public void onAccount(IAccount account) throws JFException {
      
      if (account.getUseOfLeverage()==0) tradingAllowed=true;

   }

   @Override
   public void onMessage(IMessage message) throws JFException {

   }

   @Override
   public void onStop() throws JFException {

   }

   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {

   }
   
}

Author:  Contest Support [ Thu 26 Aug, 2010, 14:30 ]
Post subject:  Re: Contest Compliant Strategy Example [Sptember]

Please test the strategy before using on the contest account. This only an example how the strategy can look. Feel free to ask any question and add comments.

This version of strategy is much lighter than the previous one. The signal is simple fast and slow MA crossover with fixed take profit and stop loss.

Author:  Victor [ Thu 26 Aug, 2010, 14:36 ]
Post subject:  Re: Contest Compliant Strategy Example [Sptember]

Contest Support wrote:
Please test the strategy before using on the contest account. This only an example how the strategy can look. Feel free to ask any question and add comments.

This version of strategy is much lighter than the previous one. The signal is simple fast and slow MA crossover with fixed take profit and stop loss.

Yes works fine. no compilation errors (like July one)
Thanks

Author:  Contest Support [ Thu 26 Aug, 2010, 14:37 ]
Post subject:  Re: Contest Compliant Strategy Example [Sptember]

@Surya

We have updated the version of the strategy for September. You can check it in the next thread.

  Page 1 of 1