Getting the initial raw data about the offers
Overview
The article teaches how to get the initial raw data about the offers.
Get offers snapshot
If you need to get initial data without subscribing to the instrument (subscription may take a time) you should use the method IOffersManager.getRawOffersSnapshot. Snapshot will be not actual at the moment, it can be used as initial values.
You can also read the article: How to subscribe to a new instrument.
To get an offers shapshot, you should follow these steps:
-
Login (see the tutorial here)
-
Configure which instruments you want to get a snapshot for.
-
Login (see the tutorial here)
-
Configure which instruments you want to get a snapshot for.
-
Login (see the tutorial here)
-
Configure which instruments you want to get a snapshot for in a worker.
-
Printing the results in a readable format.
-
Updating the html to include our new JavaScript files.
-
The main JavaScript file.
Step 1: Login
Follow the steps in the login tutorial here
Step 2: Configure which instruments you want to get a snapshot for
private requestOffersSnapshot= async (session: IFXConnectLiteSession) => {
return new Promise<void>((resolve, reject) => {
let offerIds : string[] = session.getInstrumentsManager().getAllInstrumentDescriptors().
map((item) =>item.getOfferId());
const shuffled = offerIds.sort(() => 0.5 - Math.random());
offerIds = shuffled.slice(0, parseInt(options.numberOfInstruments));
console.log('Requested Offers Snapshot:');
console.log('Number of instruments randomly selected:', offerIds.length);
const startTime = (new Date()).getTime();
const rawOffersSnapshotCallback: IRawOffersSnapshotCallback = {
onSuccess: function (rawOffers: RawOffer[]): void {
const now = (new Date()).getTime();
console.table(rawOffers)
console.log(`Snapshot executed in ${now - startTime} ms`);
resolve();
},
onError: function (error: IFXConnectLiteError): void {
console.log(`Error`, {error})
resolve();
}
}
try {
session.getOffersManager().getRawOffersSnapshot(offerIds, rawOffersSnapshotCallback);
} catch(ex) {
reject(ex);
}
})
}
requestOffersSnapshot= async (session) => {
return new Promise((resolve, reject) => {
let offerIds = session.getInstrumentsManager().getAllInstrumentDescriptors().
/*filter((item) =>item.getPriceStreamId() != '103').*/map((item) =>item.getOfferId());
const shuffled = offerIds.sort(() => 0.5 - Math.random());
offerIds = shuffled.slice(0, parseInt(options.numberOfInstruments));
console.log('Requested Offers Snapshot:');
console.log('Number of instruments randomly selected:', offerIds.length);
const startTime = (new Date()).getTime();
const rawOffersSnapshotCallback = {
onSuccess: function (rawOffers) {
const now = (new Date()).getTime();
console.table(rawOffers)
console.log(`Snapshot executed in ${now - startTime} ms`);
resolve();
},
onError: function (error) {
console.log(`Error`, {error})
resolve();
}
}
try {
session.getOffersManager().getRawOffersSnapshot(offerIds, rawOffersSnapshotCallback);
} catch(ex) {
reject(ex);
}
})
}
In our offers worker offers\offer-worker.js we can specify which instruments to get the snapshots for.
In our example, we show all instruments and then get the snapshot for the one you click on.
class OfferWorker {
constructor(instrumentsManager, offersManager) {
this.instrumentsManager = instrumentsManager;
this.offersManager = offersManager;
this.tableBuilder = new OfferTableBuilder();
}
#getOfferContainers(instruments) {
let results = [];
for (let instrument of instruments) {
let offerId = instrument.getOfferId();
let offer = this.offersManager.getOfferById(offerId);
if (offer === null)
continue;
let symbol = instrument.getSymbol();
results.push({
offer: offer,
symbol: symbol
});
}
return results;
}
#getRawOffers(offerId) {
return new Promise((resolve, reject) => {
let callback = new RawOffersSnapshotCallback(resolve, reject);
this.offersManager.getRawOffersSnapshot(offerId, callback);
});
}
async showOfferDetails(offer) {
let rawOffers = await this.#getRawOffers(offer.offerId);
let rawOffer = rawOffers[0];
this.tableBuilder.renderOfferDetailsSubtable(rawOffer);
}
async showOffers() {
let instruments = this.instrumentsManager.getAllInstruments();
// In this sample we use all instruments,
// but you can request only desired ones, like:
// let instrument = this.instrumentsManager.getInstrumentBySymbol('USD/JPY');
let offerContainers = this.#getOfferContainers(instruments);
this.tableBuilder.renderOfferTable(offerContainers, this);
}
}
Step 3: Printing the results in a readable format.
For printing the results of the command, we have first created a file which will contain ourTableBuilder class.
Follow the instruction in the Table Builder tutorial here.
offers/raw-offer-snapshot-callback.js
class RawOffersSnapshotCallback {
constructor(resolve, reject) {
this.reject = reject;
this.resolve = resolve;
}
onSuccess(rawOffers) {
this.resolve(rawOffers);
}
onError(rawOffers) {
this.reject();
}
}
common/common.js
class DataManagerStateChangeListener {
constructor(manager, resolve, reject) {
this.manager = manager;
this.reject = reject;
this.resolve = resolve;
}
onStateChange(state) {
if (state.isLoaded()) {
this.manager.unsubscribeStateChange(this);
this.resolve(this.manager);
}
if (state.hasError()) {
this.manager.unsubscribeStateChange(this);
this.reject(state.getError());
}
}
}
function loadManager(manager) {
return new Promise((resolve, reject) => {
let listener = new DataManagerStateChangeListener(manager, resolve, reject);
manager.subscribeStateChange(listener);
manager.refresh();
});
}
async function wait(timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
}
Step 4: Updating the html to include our new JavaScript files.
<script src="offers/offer-table-builder.js"></script>
<script src="offers/offer-worker.js"></script>
<script src="offers/raw-offer-snapshot-callback.js"></script>
<script src="offers/offer-table-builder.js"></script>
<script src="common/table-builder.js"></script>
<script src="common/common.js"></script>
<script src="index.js"></script>
Step 5: The main JavaScript file.
index.js
async function showOffers(session) {
let instrumentsManager = session.getInstrumentsManager();
await loadManager(instrumentsManager);
let offersManager = session.getOffersManager();
await loadManager(offersManager);
let offerWorker = new OfferWorker(instrumentsManager, offersManager);
await offerWorker.showOffers();
}
document.addEventListener('DOMContentLoaded', () => {
let logLevel = FXConnectLite.LoggerFactory.LEVEL_DEBUG;
let logger = FXConnectLite.LoggerFactory.createLogger(logLevel);
let session = FXConnectLite.FXConnectLiteSessionFactory.create('GetOffersSnapshotSample');
session.setLogger(logger);
let callback = showOffers;
let connectionStatusChangeListener = new ConnectionStatusChangeListener(session, callback);
session.subscribeConnectionStatusChange(connectionStatusChangeListener);
bindConnectionRelatedEvents(session, () => callback(session));
});
Download the sample Node TypeScript, Node JavaScript, JavaScript.
| Table of Contents | |
|---|---|
| Advanced API Command used to get offers shapshots |
|
| Advanced API Command used to get connection status |
|
| Advanced These articles describes best practices when working with the API |
|
| Advanced API Command used to make changes to system properties |
|
| Advanced API Command used to make changes to trading properties |