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.

16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85)
 Post subject: 16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85) Post rating: 0   New post Posted: Fri 21 Jun, 2013, 17:25 

User rating: 0
Joined: Tue 28 May, 2013, 09:39
Posts: 9
Location: ChinaChina
Hi,

When I test the strategy "TrendLineCross", the message showa "16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85)".
I don't know why? Pls. give me a hand. Thanks advance.

Strategy as following:

package charts.test;
 
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.UUID;
 
import javax.swing.SwingConstants;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IChartObject.ATTR_INT;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.IHorizontalLineChartObject;
import com.dukascopy.api.drawings.ILongLineChartObject;
import com.dukascopy.api.drawings.IShortLineChartObject;
 
/**
 * The strategy draws two lines on the chart - one below the current tick price on strategy start and one above it.
 * On every tick the strategy checks if any of the lines have been crossed. On every such cross an order gets created.
 * Lines are added to the chart in an unlocked mode which mens that they can be selected by the user and moved around.
 *
 */
public class TrendLineCross implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IChart chart;
   
 
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Amount")
   public double amount = 0.02;
   
   private int orderCount;
   private SimpleDateFormat sdf;
   private ITick lastTick;
 
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.chart = context.getChart(instrument);
 
      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
       
      print("start");
       
      lastTick = history.getLastTick(instrument);
      drawLines(lastTick.getAsk());
   }
 
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (instrument != this.instrument)
         return;
       
      //bid price crosses the above line - make BUY order
      if ( chart.get("lineAbove") != null
            && tick.getAsk() > chart.get("lineAbove").getPrice(0)
            && lastTick.getAsk() < chart.get("lineAbove").getPrice(0)){
         print(sdf.format(tick.getTime()) + " Above line crossed, make BUY");
         engine.submitOrder("buyOrder" + (++orderCount), instrument, OrderCommand.BUY, amount);
      }
       
      //bid price crosses the below line - make SELL order
      if (chart.get("lineBelow") != null
            && tick.getBid() < chart.get("lineBelow").getPrice(0)
            && lastTick.getBid() > chart.get("lineBelow").getPrice(0)){
         print(sdf.format(tick.getTime()) + " Below line crossed, make SELL");
         engine.submitOrder("buyOrder" + (++orderCount), instrument, OrderCommand.SELL, amount);
      }
       
      lastTick = tick;
   }
 
   
   //draws two horizontal lines - one 5 pips above the price, the other - 5 pips below the price
   private void drawLines(double price){
       
    IChartObjectFactory factory = chart.getChartObjectFactory();     

        IHorizontalLineChartObject lineAbove = factory.createHorizontalLine("lineAbove", price + 0.0005);
        lineAbove.setColor(Color.GREEN);
        lineAbove.setAttrInt(ATTR_INT.WIDTH, 2);
        lineAbove.setText(" Above line ", SwingConstants.CENTER);
        //unlocked means that you can select the line and move it while strategy is running
        chart.addToMainChartUnlocked(lineAbove);
         
        IHorizontalLineChartObject lineBelow = factory.createHorizontalLine("lineBelow", price - 0.0005);
        lineBelow.setColor(Color.RED);
        lineBelow.setAttrInt(ATTR_INT.WIDTH, 2);
        lineBelow.setText(" Below line ", SwingConstants.CENTER);
        chart.addToMainChartUnlocked(lineBelow);
         
   }
 
   private void print(Object message) {
      console.getOut().println(message);
   }
   
   //close all orders on strategy stop and remove both lines if they have not been removed already
   public void onStop() throws JFException {
      for (IOrder order : engine.getOrders()) {
         engine.getOrder(order.getLabel()).close();
      }
      chart.remove("lineAbove");
      chart.remove("lineBelow");
   }
   
 
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {   }
 
 
   public void onAccount(IAccount account) throws JFException {   }
 
   public void onMessage(IMessage message) throws JFException {   }
 
}


 
 Post subject: Re: 16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85) Post rating: 1   New post Posted: Fri 21 Jun, 2013, 22:19 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
Consider the following post:
https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=128&t=48349&p=67857&hilit=getChartObjectFactory#p67857

If it turns out to be relevant then consider covering the dependent functionality via a null pointer check:

/* either in onStart() */

if(this.chart != null)
{
drawLines(lastTick.getAsk());
}

/* or in drawLines(double) */
{
if(chart == null) return;
IChartObjectFactory factory = chart.getChartObjectFactory();
...
}


 
 Post subject: Re: 16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85) Post rating: 0   New post Posted: Sat 22 Jun, 2013, 11:48 

User rating: 0
Joined: Tue 28 May, 2013, 09:39
Posts: 9
Location: ChinaChina
Thank you very much.

another question is that today(saturday) I want to get a right BAR at right time in daily period, by: " sdf.format(history.getBar(instrument, period,side,0).getTime()) " that show the time is 22/06/2013 00:00:00

and I use "sdf.format(history.getBar(instrument, period,side,17).getTime())" it shows 05/06/2013 00:00:00

obviously, when I get a BAR it give me include weekends. There is filter function in getBars, no filter in getBar. How I can filter weekends out?


 
 Post subject: Re: 16:07:19 Strategy tester: java.lang.NullPointerException @ charts.test.TrendLineCross.drawLines(TrendLineCross.java:85) Post rating: 0   New post Posted: Sat 22 Jun, 2013, 19:04 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
These should help:
JForex WIKI - https://www.dukascopy.com/wiki/#History_bars/Filter_usage
JForex Javadoc - https://www.dukascopy.com/client/javadoc/com/dukascopy/api/IHistory.html#getBars


 

Jump to:  

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