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