The article teaches how to receive price updates for an instrument.
How to receive price updates for an instrument
In this article you will learn how to use the API to receive price updates for an instrument.
Before you can receive price updates for an instrument, you need to:
Subscribe to the offers manager change event.
Create an offer's state change listener.
Receive price updates for an instrument.
Step 1: Subscribe to the offers manager change event.
The first step is to subscribe to the offers manager change event.
This event is triggered when the offer data has changed, such as when a new offer is added, removed, or updated.
You can use the IOffersManager interface to access the methods and properties of the offers manager.
Below you can see some example code for subscribing to the offers manager change event:
Subscribe to the offersManagerChange Hide
typescriptjavascriptlet offersManager = session.getOffersManager(); //subscribe to the offersManager change to get an event when offer data has changed offersManager.subscribeOfferChange(new OfferChangeListener(instrumentsManager, offersManager)) offersManager.subscribeStateChange(new OffersStateChangeListener(offersManager, instrumentsManager));
See also IOffersManager, getOpenPosition, subscribeStateChange
Step 2: Create an offer's state change listener.
The next step is to create an offer's state change listener.
This is a function that will be called whenever an offer's state changes, such as when its price, volume, or spread changes.
You can use the getOfferById method of the offers manager to get an offer by its ID.
Below, you can see some example code to create an offer's state change listener:
The offer's state change listener Hide
typescriptjavascriptclass OfferChangeListener implements FXConnectLite.IOfferChangeListener { private isFirstOfferChange: boolean = false; private instrumentsManager: FXConnectLite.IInstrumentsManager; private offersManager: FXConnectLite.IOffersManager; public constructor(instrumentManager: FXConnectLite.IInstrumentsManager, offersManager: FXConnectLite.IOffersManager) { this.instrumentsManager = instrumentManager; this.offersManager = offersManager; } onChange(offerInfo: FXConnectLite.OfferInfo): void { if (!this.isFirstOfferChange) { Printer.print(``); Printer.print(`Offer changes:`); Printer.print(``); Printer.print(OfferFormatter.TITLE); this.isFirstOfferChange = true; } let offer = this.offersManager.getOfferById(offerInfo.getOfferId()); Printer.print(`${OfferFormatter.format(offer, this.instrumentsManager)}`); } onAdd(offerInfo: FXConnectLite.OfferInfo): void { //do nothing } }
See also IOffersManager, getOfferById
Step 3: Receive price updates for an instrument.
The final step is to receive price updates for an instrument.
You can use the subscribeStateChange method of the offer object to subscribe to its state changes.
This method takes an offer's state change listener as an argument and returns a subscription object.
You can also use the refresh method of the subscription object to manually request a price update.
Below, you can see some example code to receive price updates for an instrument:
Price updates for an instrument Hide
typescriptjavascriptprivate loadOffers = async (session) => { return new Promise<void>((resolve, reject) => { if(session.getOffersManager().getState().isLoaded()) resolve(); else { let completeHandler = new CompleteLoadingHandler(resolve, this.timeout); session.getOffersManager().subscribeStateChange(completeHandler); session.getOffersManager().refresh(); } }) }
See also subscribeStateChange, refresh()
Conclusion:
You have learned how to use the API to receive price updates for an instrument. Before you can receive price updates, you need to subscribe to the offers manager change event, create an offer's state change listener, and receive price updates for an instrument. You have also learned how to subscribe to the offers manager change event using the IOffersManager interface and how to create an offer's state change listener using the getOfferById method of the offers manager. You can now use this knowledge to receive price updates for instruments on the platform.
Download the sample TypeScript, JavaScript.