Pricing Stream used with events

Overview

This example demonstrates how to retrieve level 1 streaming data (such as trades and quotes) from the Refinitiv Data Platform. The example will show how to define a Pricing object with registered event handlers so that your application is notified when new data is coming in.

Using a Pricing Stream that way allows your application to be updated in real-time when data changes on the market. With this event-driven mode, your application can still benefit from the Pricing Stream data cache and use the getFields()/getFieldValue() methods to pull out the latest received values as real-time snapshots.

The results of those two examples will be identical, the only difference is the style of code (async/ await vs Promise).

Example Code with async/await

    	
            

const { ContainerSession, Pricing } = window['@refinitiv/dataplatform-containersession'];

const sxs = window.sxsWeb;

// For users, who use a private network

// sxs("config", "use-network", "private");

sxs.start(new Date());

sxs.onLoad(async () => {

  console.log('[DEMO] WebSDK Loaded at time: ' + new Date());

  const dataSession = ContainerSession.Definition({

    appKey: 'InputAppKeyHere',

    bus: sxs

  }).getSession();

     

  try {

    await dataSession.open();

   

    const pricingDefinition = Pricing.Definition({

      universe: 'EUR=',

      fields: ['DSPLY_NAME', 'PRCTCK_1', 'TRDPRC_1', 'PCTCHNG', 'HIGH_1', 'LOW_1']

    });

   

    const stream = await pricingDefinition.getStream(dataSession);

    stream

      .onRefresh((refresh, instrument) => console.log(instrument, refresh))

      .onUpdate((update, instrument) => console.log(instrument, update))

      .onStatus((status, instrument) => console.log(instrument, status))

      .onComplete(() => console.log('Complete'))

      .onError(err => console.log(err));

            await stream.open();

    // The StreamingPrice will be closed after 30 seconds

    await new Promise(res => setTimeout(res, 30000));

    await stream.close();

   

  } catch (err) {

    console.log(err);

  } finally {

    await dataSession.close();

  }

});

Example Code with promises

    	
            

const { ContainerSession, Pricing } = window['@refinitiv/dataplatform-containersession'];

const sxs = window.sxsWeb;

// For users, who use a private network

// sxs("config", "use-network", "private");

sxs.start(new Date());

sxs.onLoad(async () => {

  console.log('[DEMO] WebSDK Loaded at time: ' + new Date());

  const dataSession = ContainerSession.Definition({

    appKey: 'InputAppKeyHere',

    bus: sxs

  }).getSession();

 

  const pricingDefinition = Pricing.Definition({

    universe: 'EUR=',

    fields: ['DSPLY_NAME', 'PRCTCK_1', 'TRDPRC_1', 'PCTCHNG', 'HIGH_1', 'LOW_1']

  });

  const stream = pricingDefinition

            .getStream(dataSession)

    .onRefresh((refresh, instrument) => console.log(instrument, refresh))

    .onUpdate((update, instrument) => console.log(instrument, update))

    .onStatus((status, instrument) => console.log(instrument, status))

    .onComplete(() => console.log('Complete'))

    .onError(err => console.log(err));

 

  dataSession

    .open()

    .then(() => stream.open())

    .then(() => new Promise(res => setTimeout(res, 30000)))

      .then(() => stream.close())

    .catch(console.log)

    .then(() => dataSession.close());

});

Example package

All our examples are available here for download