With this sample:
package de.invesdwin.forextrading.sample.bars;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.NotThreadSafe;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
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.PriceRange;
import com.dukascopy.api.feed.IRenkoBar;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import de.invesdwin.common.log.Log;
import de.invesdwin.common.log.error.Err;
import de.invesdwin.forextrading.ForexProperties;
/**
* This small program demonstrates how to initialize Dukascopy client and start a strategy
*/
@NotThreadSafe
public final class RenkoBarsSample {
//url of the DEMO jnlp
private static final Log LOGGER = new Log(RenkoBarsSample.class);
private RenkoBarsSample() {}
//CHECKSTYLE:OFF
public static void main(final String[] args) throws Exception {
//CHECKSTYLE:ON
//get the instance of the IClient interface
final ITesterClient client = TesterFactory.getDefaultInstance();
//set the listener that will receive system events
client.setSystemListener(new ISystemListener() {
private int lightReconnects = 3;
@Override
public void onStart(final long processId) {
LOGGER.info("Strategy started: " + processId);
}
@Override
public void onStop(final long processId) {
LOGGER.info("Strategy stopped: " + processId);
if (client.getStartedStrategies().size() == 0) {
System.exit(0);
}
}
@Override
public void onConnect() {
LOGGER.info("Connected");
lightReconnects = 3;
}
@Override
public void onDisconnect() {
LOGGER.warn("Disconnected");
if (lightReconnects > 0) {
client.reconnect();
--lightReconnects;
} else {
try {
//sleep for 10 seconds before attempting to reconnect
Thread.sleep(10000);
} catch (final InterruptedException e) {
Err.process(e);
}
try {
client.connect(ForexProperties.JFOREX_JNLP_URL.toString(),
ForexProperties.JFOREX_JNLP_USERNAME, ForexProperties.JFOREX_JNLP_PASSWORD);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
});
LOGGER.info("Connecting...");
//connect to the server using jnlp, user name and password
client.connect(ForexProperties.JFOREX_JNLP_URL.toString(), ForexProperties.JFOREX_JNLP_USERNAME,
ForexProperties.JFOREX_JNLP_PASSWORD);
//wait for it to connect
int i = 10; //wait max ten seconds
while (i > 0 && !client.isConnected()) {
Thread.sleep(1000);
i--;
}
if (!client.isConnected()) {
LOGGER.error("Failed to connect Dukascopy servers");
System.exit(1);
}
//subscribe to the instruments
final Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURUSD);
LOGGER.info("Subscribing instruments...");
client.setSubscribedInstruments(instruments);
//start the strategy
Thread.sleep(1000);
LOGGER.info("Starting strategy");
client.startStrategy(new IStrategy() {
private IContext context;
@Override
public void onTick(final Instrument instrument, final ITick tick) throws JFException {
if (instrument == Instrument.EURUSD) {
final IBar lastBar = context.getHistory().getRenkoBar(Instrument.EURUSD, OfferSide.ASK,
PriceRange.valueOf(2), 0);
long timeTo = tick.getTime();
if (lastBar != null && lastBar.getTime() < timeTo) {
timeTo = lastBar.getTime();
}
final long timeFrom = timeTo - TimeUnit.MINUTES.toMillis(5);
final List<IRenkoBar> previousBars = context.getHistory().getRenkoBars(Instrument.EURUSD,
OfferSide.ASK, PriceRange.valueOf(2), timeFrom, timeTo);
LOGGER.info("" + lastBar);
LOGGER.info("" + previousBars);
}
}
@Override
public void onStop() throws JFException {}
@Override
public void onStart(final IContext context) throws JFException {
this.context = context;
}
@Override
public void onMessage(final IMessage message) throws JFException {}
@Override
public void onBar(final Instrument instrument, final Period period, final IBar askBar, final IBar bidBar)
throws JFException {}
@Override
public void onAccount(final IAccount account) throws JFException {}
});
//now it's running
}
}
You can produce this exception:
2012-06-01 17:04:38,872 [ |StrategyRunner Thre] ERROR c.d.d.g.agent.strategy.tester.StrategyRunner.handleException - null
java.lang.NullPointerException: null
at com.dukascopy.api.impl.History$41.run(History.java:1904) ~[greed-common-190.jar:190]
at com.dukascopy.api.impl.History$41.run(History.java:1898) ~[greed-common-190.jar:190]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_24]
at com.dukascopy.api.impl.History.getRenkoBars(History.java:1898) ~[greed-common-190.jar:190]
at com.dukascopy.dds2.greed.agent.strategy.tester.TesterHistory.getRenkoBars(TesterHistory.java:1654) ~[greed-common-190.jar:190]
at de.invesdwin.forextrading.sample.bars.RenkoBarsSample$2.onTick(RenkoBarsSample.java:157) ~[bin/:na]
at com.dukascopy.dds2.greed.agent.strategy.tester.AbstractStrategyRunner.historicalTickReceived(AbstractStrategyRunner.java:609) ~[greed-common-190.jar:190]
at com.dukascopy.dds2.greed.agent.strategy.tester.StrategyRunner.run(StrategyRunner.java:469) ~[greed-common-190.jar:190]
at java.lang.Thread.run(Thread.java:679) [na:1.6.0_24]
Strategy tester: java.lang.NullPointerException @ de.invesdwin.forextrading.sample.bars.RenkoBarsSample$2.onTick(RenkoBarsSample.java:157)
null: java.lang.NullPointerException
at com.dukascopy.api.impl.History$41.run(History.java:1904)
at com.dukascopy.api.impl.History$41.run(History.java:1898)
at java.security.AccessController.doPrivileged(Native Method)
at com.dukascopy.api.impl.History.getRenkoBars(History.java:1898)
at com.dukascopy.dds2.greed.agent.strategy.tester.TesterHistory.getRenkoBars(TesterHistory.java:1654)
at de.invesdwin.forextrading.sample.bars.RenkoBarsSample$2.onTick(RenkoBarsSample.java:157)
at com.dukascopy.dds2.greed.agent.strategy.tester.AbstractStrategyRunner.historicalTickReceived(AbstractStrategyRunner.java:609)
at com.dukascopy.dds2.greed.agent.strategy.tester.StrategyRunner.run(StrategyRunner.java:469)
at java.lang.Thread.run(Thread.java:679)
2012-06-01 17:04:38,878 [ |StrategyRunner Thre] ERROR c.d.a.i.c.TesterClientImpl$DefaultStrategyExceptionHandler.o - Exception thrown while running onTick method: null
java.lang.NullPointerException: null
at com.dukascopy.api.impl.History$41.run(History.java:1904) ~[greed-common-190.jar:190]
at com.dukascopy.api.impl.History$41.run(History.java:1898) ~[greed-common-190.jar:190]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_24]
at com.dukascopy.api.impl.History.getRenkoBars(History.java:1898) ~[greed-common-190.jar:190]
at com.dukascopy.dds2.greed.agent.strategy.tester.TesterHistory.getRenkoBars(TesterHistory.java:1654) ~[greed-common-190.jar:190]
at de.invesdwin.forextrading.sample.bars.RenkoBarsSample$2.onTick(RenkoBarsSample.java:157) ~[bin/:na]
at com.dukascopy.dds2.greed.agent.strategy.tester.AbstractStrategyRunner.historicalTickReceived(AbstractStrategyRunner.java:609) ~[greed-common-190.jar:190]
at com.dukascopy.dds2.greed.agent.strategy.tester.StrategyRunner.run(StrategyRunner.java:469) ~[greed-common-190.jar:190]
at java.lang.Thread.run(Thread.java:679) [na:1.6.0_24]
This has been tested with JForex 2.6.69.