Instrument Subscriptions

Brief

The article teaches how to subscribe to or unsubscribe from instruments.

Details

Subscribe To Or Unsubscribe From An Instrument

In this article you will learn how to subscribe and unsubscribe to instruments, which are the financial assets that you can trade on the platform, such as currency pairs, stocks, indices, etc.
You can use the IInstrumentsManager interface to manage your subscriptions and get information about the instruments.

To subscribe to or unsubscribe from instruments, you need to follow these steps:

Step 1: Create a SubscribeCallback object, which is a callback class that handles the subscription results.

You can use the following code to create a SubscribeCallback object:

SubscribeCallback Hide

typescriptjavascript
class SubscribeCallback implements FXConnectLite.ISubscribeInstrumentsCallback { resolve; constructor(resolve) { this.resolve = resolve; } onSuccess() { Printer.print("Subscribe success"); this.resolve(); } onError(error: string) { Printer.print("Subscribe error: "+ error); this.resolve(); } }

The onError method is called if at least one subscription to an instrument fails.

Step 2: Get a list of your subscribed instruments.

You can check the list of subscribed instruments by calling the instrument manager's getSubscribedInstruments method.

Get a list of subscribed instruments Hide

typescriptjavascript
session.getInstrumentsManager().refresh(); // refresh it before using it to make sure it contains the latest info. let instrumentsManager = session.getInstrumentsManager(); let subscribedInstruments = instrumentsManager.getSubscribedInstruments();

Step 3: Get an instrument symbol.

Get an instrument symbol using the InstrumentDescriptor class, which is a helper class that helps you identify an instrument by its name, type, and exchange.

You can use the following code to get an instrument symbol:

Get an instrument symbol Hide

typescriptjavascript
let instrumentSymbol = instrumentsManager.getSubscribedInstruments()[0].getSymbol(); instrumentsManager.unsubscribeInstruments([instrumentSymbol], new UnsubscribeCallback(resolve));

Step 4: Subscribing To An Instrument.
To subscribe to an instrument, you use the instrument manager's subscribe method and pass it the symbol of the instrument and the callback function.

You can use the following code to create a SubscribeCallback object:

Subscribe to an instrument Hide

typescriptjavascript
instrumentsManager.subscribeInstruments(["EUR/GBP", "EUR/USD"], new SubscribeCallback(resolve));

This instrument will now appear on your list of subscribed instruments when performing the getSubscribedInstruments action.

See also IInstrumentsManager, ISubscribeInstrumentsCallback

Step 5: Unsubscribing To An Instrument
Create an UnsubscribeCallback object, which is a callback class that handles the unsubscription results.

You can use the following code to create an UnsubscribeCallback object:

Unsubscribe to an instrument Hide

typescriptjavascript
class UnsubscribeCallback implements FXConnectLite.ISubscribeInstrumentsCallback { resolve; constructor(resolve) { this.resolve = resolve; } onSuccess() { Printer.print("Unsubscribe success"); this.resolve(); } onError(error: string) { Printer.print("Unsubscribe error: "+ error); this.resolve(); } }

The onError method is called if at least one unsubscription to an instrument fails.

This instrument will no longer appear on your list of subscribed instruments when performing the getSubscribedInstruments action.

See also IInstrumentsManager, ISubscribeInstrumentsCallback

Conclusion:

You have learned how to subscribe and unsubscribe to instruments using the IInstrumentsManager interface. You have created a SubscribeCallback object, which is a callback class that handles the subscription results. You have also learned how to check the list of subscribed instruments by calling the instrument manager's getSubscribedInstruments method. Additionally, you have learned how to get an instrument symbol using the InstrumentDescriptor class. You can now use this knowledge to manage your subscriptions and get information about the instruments on the platform.

Download the sample TypeScript, JavaScript.

Questions and answers

What are indices?

Indices are a measurement of the performance of a group of stocks. Trading indices enables you to gain exposure to a specific sector of the economy while only needing to open one position.

back