When instrument is subscribed (e.g. IContext.setSubscribedInstruments(Set<Instrument> instruments)), then we subscribe to a default set of periods:
Period.TICK
Period.TEN_SECS
Period.ONE_MIN
Period.FIVE_MINS
Period.TEN_MINS
Period.FIFTEEN_MINS
Period.THIRTY_MINS
Period.ONE_HOUR
Period.FOUR_HOURS
Period.DAILY
Period.WEEKLY
Period.MONTHLY
If one wants to use custom periods, then he needs to subscribe to these periods, example:
IContext.subscribeToBarsFeed(instrument, customPeriod, OfferSide.ASK, listener);
Try the attached sample strategy (PeriodStrategy.java), the output should be similar to this:
2017-04-04 14:07:25 (Subscribed) 120 Secs 1491314760000[2017-04-04 14:06:00.000+0000] O: 1.01588 C: 1.01572 H: 1.01588 L: 1.01566 V: 188.68
2017-04-04 14:07:23 (Not subscribed by default) 120 Secs null
2017-04-04 14:07:23 Monthly 1491004800000[2017-04-01 00:00:00.000+0000] O: 1.01714 C: 1.01572 H: 1.02063 L: 1.01255 V: 173002.78
2017-04-04 14:07:23 Weekly 1491177600000[2017-04-03 00:00:00.000+0000] O: 1.01691 C: 1.01572 H: 1.02063 L: 1.01255 V: 168506.83
2017-04-04 14:07:23 Daily 1491264000000[2017-04-04 00:00:00.000+0000] O: 1.01808 C: 1.01572 H: 1.01916 L: 1.01352 V: 79433.42
2017-04-04 14:07:23 4 Hours 1491307200000[2017-04-04 12:00:00.000+0000] O: 1.01532 C: 1.01572 H: 1.01664 L: 1.01458 V: 15679.43
2017-04-04 14:07:23 Hourly 1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23 30 Mins 1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23 15 Mins 1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23 10 Mins 1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23 5 Mins 1491314700000[2017-04-04 14:05:00.000+0000] O: 1.0158 C: 1.01572 H: 1.01589 L: 1.01566 V: 280.64
2017-04-04 14:07:23 1 Min 1491314820000[2017-04-04 14:07:00.000+0000] O: 1.01574 C: 1.01572 H: 1.01575 L: 1.01566 V: 54.73
2017-04-04 14:07:23 10 Secs 1491314840000[2017-04-04 14:07:20.000+0000] O: 1.01574 C: 1.01572 H: 1.01574 L: 1.01572 V: 8.75
2017-04-04 14:07:23 Ticks 1491314842669[2017-04-04 14:07:22.669+0000] / 1.01572 / 1.01553
package apiclient2220;
import java.util.HashSet;
import java.util.Set;
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.Unit;
import com.dukascopy.api.feed.IBarFeedListener;
public class PeriodStategy implements IStrategy, IBarFeedListener{
private Instrument instrument = Instrument.AUDCAD;
@Override
public void onStart(IContext context) throws JFException {
Set<Instrument> instruments = new HashSet<>();
instruments.add(instrument);
context.setSubscribedInstruments(instruments, true);
Period[] periods = new Period[]{
Period.TEN_SECS,
Period.ONE_MIN,
Period.FIVE_MINS,
Period.TEN_MINS,
Period.FIFTEEN_MINS,
Period.THIRTY_MINS,
Period.ONE_HOUR,
Period.FOUR_HOURS,
Period.DAILY,
Period.WEEKLY,
Period.MONTHLY
};
//these will be available by default:
context.getConsole().getInfo().println(String.format("%1$-8s %2$s", Period.TICK, context.getHistory().getTick(instrument, 0)));
for (Period p : periods){
context.getConsole().getInfo().println(String.format("%1$-8s %2$s", p, context.getHistory().getBar(instrument, p, OfferSide.ASK, 0)));
}
//try to load load non-subscribed custom period:
Period customPeriod = Period.createCustomPeriod(Unit.Second, 120);
context.getConsole().getInfo().println(String.format("(Not subscribed by default) %1$-8s %2$s", customPeriod, context.getHistory().getBar(instrument, customPeriod, OfferSide.ASK, 0)));
//subscribe custom period:
context.subscribeToBarsFeed(instrument, customPeriod, OfferSide.ASK, this);
//and print custom in-progress candle:
context.getConsole().getInfo().println(String.format("(Subscribed) %1$-8s %2$s", customPeriod, context.getHistory().getBar(instrument, customPeriod, OfferSide.ASK, 0)));
}
@Override public void onTick(Instrument instrument, ITick tick) throws JFException { }
@Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { }
@Override public void onMessage(IMessage message) throws JFException { }
@Override public void onAccount(IAccount account) throws JFException { }
@Override public void onStop() throws JFException { }
@Override public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bar) { }
}