Regarding this ticket:
viewtopic.php?f=65&t=46909Using this sample strategy:
import java.util.HashSet;
import java.util.Set;
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.TickBarSize;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import de.invesdwin.forexhandel.ForexProperties;
import de.invesdwin.gemeinsam.log.Log;
import de.invesdwin.gemeinsam.log.error.Err;
/**
* This small program demonstrates how to initialize Dukascopy client and start a strategy
*/
@NotThreadSafe
public final class TickBarsTest {
//url of the DEMO jnlp
private static final Log LOGGER = new Log(TickBarsTest.class);
private TickBarsTest() {}
//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
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 bar = context.getHistory().getTickBar(Instrument.EURUSD, OfferSide.ASK,
TickBarSize.valueOf(2), 1);
LOGGER.info("" + bar);
}
}
@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 will get the following output:
2012-04-27 00:37:32,342 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,343 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
2012-04-27 00:37:32,344 [ |StrategyRunner Thre] INFO d.i.forextrading.sample.tickbars.TickBarsTest.onTick - StartTime: 2012-04-22 23:59:57.812 EndTime: 2012-04-22 23:59:58.359 O: 1.32079 C: 1.32079 H: 1.32079 L: 1.32079 V: 3.0 FEC: 2
This shows that IHistory is buggy on backtests, because for TickBar, RenkoBar, etc it always returns the same Bar even if the backtest got much more data after that bar returned.
The only thing that works is TimeBar via getBars(), all other bar types always return the same data and thus do not work correctly. You can change the sample to print other bar types, to see that.
This is tested for JForex 2.6.64 and 2.6.65. Before that, an exception was thrown instead (see other forum ticket).
Reporting this here so that it gets treated as a bug instead of a "Knowledge Base" question.