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.

The question of strategy based on DMI Oscillator
 Post subject: The question of strategy based on DMI Oscillator Post rating: 0   New post Posted: Tue 05 Jul, 2011, 13:26 

User rating: -
How can I use an indicator DMI, to build a strategy for using the language JAVA and Jforex API.


 
 Post subject: Re: The question of strategy based on DMI Oscillator Post rating: 0   New post Posted: Tue 05 Jul, 2011, 14:19 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
For usage of Indicators in your strategy see:
https://www.dukascopy.com/wiki/index.php ... Indicators
Also see an example strategy which uses an indicator:
https://www.dukascopy.com/wiki/index.php ... SMA_Simple
Consider an example strategy which makes an order on every -DI and +DI cross: a SELL order if +DI goes below -DI and a BUY order if +DI goes above -DI:
package jforex.bugtests;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

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

public class DMIstrategy implements IStrategy {
   private IEngine engine;
   private IIndicators indicators;
   private IConsole console;
   private IChart chart;

   // macd specific params
   private int dmiPeriod = 14;

   // strategy specific params
   private Instrument instrument = Instrument.EURUSD;
   private Period period = Period.TEN_MINS;

    @SuppressWarnings("serial")
   private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};
   private int orderCount;

   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      indicators = context.getIndicators();
      this.console = context.getConsole();
      this.chart = context.getChart(instrument);
      console.getOut().println("Started");

      /* uncomment to see the ordering of optional parameters
      IIndicator indicator = indicators.getIndicator("DMI");
      for (int i = 0; i < indicator.getIndicatorInfo().getNumberOfOptionalInputs(); i++){
          console.getOut().println( i + ": name = '"+ indicator.getOptInputParameterInfo(i).getName() + "' type = "+ indicator.getOptInputParameterInfo(i).getType());
      }
      */
      chart.addIndicator(indicators.getIndicator("DMI"), new Object[]{dmiPeriod});
   }

   public void onStop() throws JFException {
      for (IOrder order : engine.getOrders()) {
         order.close();
      }
      console.getOut().println("Stopped");
   }

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

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

      if (period != this.period || instrument != this.instrument)
         return;

      double[] dmi = indicators.dmi(instrument, period, OfferSide.BID, dmiPeriod, 1);
      double[] dmiLast = indicators.dmi(instrument, period, OfferSide.BID, dmiPeriod, 2);
      
      print(sdf.format(bidBar.getTime()) + " ADX=" + dmi[0] +" +DI=" + dmi[1] +" -DI=" + dmi[2]);
      //+DI and -DI cross condition
      if ((dmi[1] > dmi[2] && dmiLast[1] < dmiLast[2]) || (dmi[1] < dmi[2] && dmiLast[1] > dmiLast[2])){
         //SELL if +DI goes below -DI, BUY otherwise
         OrderCommand cmd = (dmi[1] > dmi[2] && dmiLast[1] < dmiLast[2]) ? OrderCommand.SELL : OrderCommand.BUY;
         IOrder order = engine.submitOrder("order"+ ++orderCount, Instrument.EURUSD, cmd, 0.001, 0, 20,
               bidBar.getClose() - 0.005, //Stop Loss
               askBar.getClose() + 0.005);//Take Profit
         print("+DI and -DI cross!! Make a " + cmd + " order: " + order.getLabel());
      }
   }

   private void print(Object o) {
      console.getOut().println(o);
   }

   public void onMessage(IMessage message) throws JFException {}
   public void onAccount(IAccount account) throws JFException {}
}


 

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