hyperscalper wrote:
IClient.setSubscribedInstruments(Set<Instrument> instruments) talks about subscribing
to the given set of instruments, but does it explicitly UNSUBSCRIBE any other instruments?
Yes it does, unless there is a running strategy which has subscribed to an instrument (by using
IContext.setSubscribedInstruments) which is not in the instrument set which you pass in
IClient.setSubscribedInstruments. Thus, when running a strategy from the SDK, calling
IContext.setSubscribedInstruments effectively "locks" the instrument throughout the strategy execution, such that it can't be removed from the
IClient. Which theoretically means that if each of your strategies explicitly subscribes to instruments it is going to work with you could call
client.setSubscribedInstruments(Collections.EMPTY_SET); on every strategy stop in order to work with the minimal possible set of instruments at any moment. To see how this works consider the following example:
//the thread every 10 seconds sets a different instrument set
new Thread(new Runnable() {
Instrument[] instruments = new Instrument[] { Instrument.EURUSD, Instrument.AUDUSD, Instrument.USDJPY, Instrument.USDCAD,
Instrument.USDCHF, Instrument.USDSGD, Instrument.XAUUSD, };
private int i = 0;
@Override
public void run() {
while (true) {
System.err.println("Subscribe: " + instruments[i]);
client.setSubscribedInstruments(EnumSet.of(instruments[i++]));
if (i >= instruments.length) {
i = 0;
}
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
//the strategy simply prints each tick it receives
client.startStrategy(new IStrategy() {
@Override
public void onStart(IContext context) throws JFException {
//EURUSD will not get unsubscribed as long as the strategy will be running
context.setSubscribedInstruments(EnumSet.of(Instrument.EURUSD));
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
System.out.println(instrument + " " + tick);
}
@Override
public void onMessage(IMessage message) throws JFException {
}
@Override
public void onAccount(IAccount account) throws JFException {
}
@Override
public void onStop() throws JFException {
}
});