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.

DMI strategy fix
 Post subject: DMI strategy fix Post rating: 0   New post Posted: Tue 20 Mar, 2012, 13:10 

User rating: 0
Joined: Tue 20 Mar, 2012, 12:49
Posts: 7
Location: Greece, Athens
I found the following simple DMI strategy in the strategies library. Here is the link:

[url] viewtopic.php?f=61&t=38080
[/url]

It 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.

I tried to run the strategy but there is an error that says :

Quote:
2012-03-20 11:50:55 java.lang.NullPointerException @ jforex.bugtests.DMIstrategy.onStart(DMIstrategy.java:40)


Kindly help me fix the strategy. Also tell me where should I enter the lotsize?
Also tell me what is relevance of the date and time in the code. I tried entereing that as well. Why is it required anyway?
Also tell me what does this mean :


        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




Here is the code :



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 {}
}




Thanks to anyone who replies to this. :)


 
 Post subject: Re: DMI strategy fix Post rating: 0   New post Posted: Wed 21 Mar, 2012, 09:48 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Praxieyoman wrote:
I tried to run the strategy but there is an error that says :
2012-03-20 11:50:55 java.lang.NullPointerException @ jforex.bugtests.DMIstrategy.onStart(DMIstrategy.java:40)

You need to open the chart of the instrument.
Praxieyoman wrote:
Also tell me what does this mean :
        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
See the method:
https://www.dukascopy.com/client/javadoc//com/dukascopy/api/IEngine.html#submitOrder(java.lang.String,%20com.dukascopy.api.Instrument,%20com.dukascopy.api.IEngine.OrderCommand,%20double,%20double,%20double,%20double,%20double)
Read more on order creation:
https://www.dukascopy.com/wiki/index.php ... rket_Order
and orders with SL and TP:
https://www.dukascopy.com/wiki/index.php ... Loss_price


 

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