By default, the Dukascopy Historical Tester includes all bars during times that the market is closed, including Saturday bars.
To exclude these bars, we must use the Filter "WEEKENDS" in indicator method calls or getBars method calls:
double[] rsi(Instrument instrument, Period period, OfferSide side, IIndicators.AppliedPrice appliedPrice, int timePeriod, [b]Filter filter[/b], int numberOfCandlesBefore, long time, int numberOfCandlesAfter)
java.util.List<IBar> getBars(Instrument instrument, Period period, OfferSide side, [b]Filter filter[/b], int numberOfCandlesBefore, long time, int numberOfCandlesAfter)
However there is only one getBar method and it does not have a filter parameter:
IBar getBar(Instrument instrument, Period period, OfferSide side, int shift)
So if I wanted to get the hourly bar that was 50 hourly bars ago, if I used getBar then weekend bars would be counted. There is no way to get a single historical bar using getBar and not count weekend bars because there is no getBar method that takes a filter parameter.
I could instead use getBars, however then all 50 bars would be loaded into the List of bars, which is very inefficient if all I need was a single bar. The higher the number of bars ago, the more inefficient it would be.
Please see my code below that tests getBar and getBars with and without a filter.
package test;
import com.dukascopy.api.*;
import java.util.*;
public class FilterTest implements IStrategy {
private static final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE");
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
//private IUserInterface userInterface;
long lastTickTime = 0;
//============================================================================================================================================================
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
//this.userInterface = context.getUserInterface();
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
console.getOut().print("Process started.\n");
}
//============================================================================================================================================================
public void onAccount(IAccount account) throws JFException {
}
//============================================================================================================================================================
public void onMessage(IMessage message) throws JFException {
}
//============================================================================================================================================================
public void onStop() throws JFException {
console.getOut().print("Process stopped.\n");
}
//============================================================================================================================================================
public void onTick(Instrument instrument, ITick tick) throws JFException {
lastTickTime = tick.getTime();
}
//============================================================================================================================================================
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { //called on bar completion and before first onTick call
if (period == Period.ONE_HOUR) {
Calendar calendar1 = new GregorianCalendar();
calendar1.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar1.setTimeInMillis(bidBar.getTime());
if (calendar1.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {//bar open time = Monday 00:00:00
console.getOut().println("Using history.getBar:");
IBar oldBar = history.getBar(instrument, period, OfferSide.BID, 50);
IBar newBar = history.getBar(instrument, period, OfferSide.BID, 0);
console.getOut().println("bidBar.getTime() = " + sdf.format(new Date(bidBar.getTime())));
console.getOut().println("newBar.getTime() = " + sdf.format(new Date(newBar.getTime())));
console.getOut().println("oldBar.getTime() = " + sdf.format(new Date(oldBar.getTime())));
long currentBarTime = bidBar.getTime();
int numberOfCandlesBefore = 50;
int numberOfCandlesAfter = 0;
//long time = bidBar.getTime(); //gets bar open time of just-completed old bar of onBar time frame
console.getOut().println("Using history.getBars:");
Filter filter1 = Filter.NO_FILTER;
List<IBar> historicalBars = history.getBars(instrument, period, OfferSide.BID, filter1, numberOfCandlesBefore, currentBarTime, numberOfCandlesAfter);
console.getOut().println("filter = NO_FILTER, old bar time = " + sdf.format(new Date(historicalBars.get(0).getTime())));
filter1 = Filter.WEEKENDS;
historicalBars = history.getBars(instrument, period, OfferSide.BID, filter1, numberOfCandlesBefore, currentBarTime, numberOfCandlesAfter);
console.getOut().println("filter = WEEKENDS, old bar time = " + sdf.format(new Date(historicalBars.get(0).getTime())));
}
}
}
}
Here is the output:
Using history.getBar:
bidBar.getTime() = 2011-04-04 00:00:00 Mon
newBar.getTime() = 2011-04-04 01:00:00 Mon
oldBar.getTime() = 2011-04-01 23:00:00 Fri
Using history.getBars:
filter = NO_FILTER, old bar time = 2011-04-01 23:00:00 Fri
filter = WEEKENDS, old bar time = 2011-03-30 23:00:00 Wed
You can see that using getBar is the same as using getBars without a filter, but what I really want is the last result that does not count the weekend bars.
I request that you create a new getBar method that accepts a filter parameter for the case that a single historical bar n bars ago (without counting weekend bars) is needed.