Dukascopy
 
 
Wiki JStore Search Login

Contest Compliant Strategy Example [September]
 Post subject: Contest Compliant Strategy Example [September] Post rating: 0   New post Posted: Thu 26 Aug, 2010, 14:29 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
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 {

   }
   
}


 
 Post subject: Re: Contest Compliant Strategy Example [Sptember] Post rating: 0   New post Posted: Thu 26 Aug, 2010, 14:30 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
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.


 
 Post subject: Re: Contest Compliant Strategy Example [Sptember] Post rating: 0   New post Posted: Thu 26 Aug, 2010, 14:36 
User avatar

User rating: 2
Joined: Thu 19 May, 2011, 09:37
Posts: 86
Location: India, Chennai
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


 
 Post subject: Re: Contest Compliant Strategy Example [Sptember] Post rating: 0   New post Posted: Thu 26 Aug, 2010, 14:37 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
@Surya

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


 

Jump to:  

  © 1998-2024 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