Subscribe to Instruments

In order to trade an instrument, it has to be subscribed. One can retrieve already subscribed instruments by using the IContext.getSubscribedInstruments() method. One can subscribe to instruments by using the IContext.setSubscribedInstruments() method. When using the asynchronous method, the subscription might not be immediate, so in order to make sure that the instrument is available for trade it is advised to wait for the subscription to complete before submitting an order. See also Instrument tradability.

Synchronous subscription

Consider subscribing to two instruments EURSEK and AUDUSD:
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURSEK);
instruments.add(Instrument.AUDUSD);

context.setSubscribedInstruments(instruments, true);

Asynchronous subscription

Consider subscribing to two instruments EURSEK and AUDUSD with maximum waiting time of 1 second.

Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURSEK);
instruments.add(Instrument.AUDUSD);

context.setSubscribedInstruments(instruments);

// wait max 1 second for the instruments to get subscribed
int i = 10;
while (!context.getSubscribedInstruments().containsAll(instruments) && i>=0) {
    try {
        console.getOut().println("Instruments not subscribed yet " + i);
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        console.getOut().println(e.getMessage());
    }
    i--;
}
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.