Eikon Data API

API Family: Eikon APIs

Streaming API - How to retrieve real-time streaming prices

Tutorial source code

Last update June 2020
Prerequisite Eikon Data API Quick Start guide

This tutorial explains how to install and use the latest version of the Eikon Data API with Streaming. Streaming data retrieval is a new feature and is available to any Eikon 4 or Refinitiv Workspace user who already has access to the Eikon Data API.

How to install the Eikon Data API with Streaming

Streaming data retrieval is included the Eikon Data API starting from version (1.1.2). In order upgrade your Python environment to the latest Eikon Data API version, please refer to the  How to update your Eikon Data API tutorial.

How retrieve streaming data in Python

The main purpose of receiving streaming data in your application is generally to keep it up-to-date with the latest values received from the market. This is generally done by registering event handlers that are called whenever new value updates come in.

Another use case scenario for streaming data, is to use it to update an in-memory data cache kept at the disposal of your application. Then, whenever it needs the latest real-time values, the application retrieves values from this cache instead of sending a request to Eikon / Refinitiv Workspace. The immediate benefit of this cache is to decrease the number of requests sent by the application for real-time data.

The following sections explain how to use the Eikon Data API for these use case scenarios.

Using the Eikon Data API as a real-time cache

This is the simplest way to leverage streaming data. This use case is appropriate for applications that need frequent snapshots of real-time data but not need to be notified on data updates.

If that case, you can advantageously replace the usual ek.get_data() calls you would make for retrieving real-time snapshots by streaming_prices.get_snapshot() calls to get snapshots from a real-time cache. Because the later call does not send requests but retrieves data from cache instead, this change can dramatically reduce the number of requests your application sends to Eikon / Refinitiv Workspace.

The following code snippet opens a StreamingPrices object for a list of instruments and fields.

streaming_prices = ek.StreamingPrices(
    instruments = ['GBP=','JPY=', 'CAD='], 
    fields   = ['CF_BID','CF_ASK','OPEN_PRC', 'CF_HIGH','CF_LOW', 'CF_CLOSE']

Then, by calling get_snapshot() on the streaming object, you can get the latest values from the cache, as a DataFrame, without sending any request.

df = streaming_prices.get_snapshot()

Here is the expected output:

  Instrument    CF_BID    CF_ASK  OPEN_PRC   CF_HIGH    CF_LOW  CF_CLOSE
0       GBP=    1.2907    1.2911    1.2922    1.2931    1.2889    1.2922
1       JPY=  108.4000  108.4300  108.5300  108.5700  108.3600  108.5300
2       CAD=    1.3308    1.3312    1.3266    1.3315    1.3262    1.3266

The get_snapshot() method can be called any number of times, it always returns a snapshot of the latest real-time values, as long as the StreamingPrices object is kept opened. You can even retrieve a subset of the instruments and fields that you initially requested. Like this:

df = streaming_prices.get_snapshot(
    instruments = ['JPY=', 'CAD='], 
    fields   = ['CF_BID','CF_ASK']

Here is the expected output:

  Instrument    CF_BID    CF_ASK
0       JPY=  108.4000  108.4300
1       CAD=    1.3308    1.3312

You can also directly access to values and instruments in the cache. Please refer to the notebook example for more details:


Here is the expected output:

{'status': <StreamState.Open: 3>, 'code': 'Open', 'message': 'All is well'}

Once you’re done with the StreamingPrices object, you can just close it. This will close the underlying streaming subscriptions.


Using the Eikon Data API to receive update events

Using StreamingPrices objects to retrieve events requires a bit more code than the example above but not that much. The main difference with events is that your application needs to register one or several handlers to receive the events.

This is how to use a StreamingPrices object to receive data events for “refresh” events (a.k.a. images) and “update” events.

First we define an event handler designed to display the incoming data:

def display_fields(streaming_prices, instrument_name, fields):
    print("Fields received for", instrument_name, ":", fields)

Then, we create and open a StreamingPrices object for a list of instruments and fields, referencing the display function defined above:

streaming_prices = ek.StreamingPrices(
    instruments = ['EUR=','GBP=','JPY=', 'CAD='], 
    fields   = ['SALTIM', 'CF_BID','CF_ASK','OPEN_PRC', 'CF_HIGH','CF_LOW', 'CF_CLOSE'],
    on_refresh = lambda streaming_prices, instrument_name, fields : 
        display_fields(streaming_prices, instrument_name, fields),
    on_update = lambda streaming_prices, instrument_name, fields : 
        display_fields(streaming_prices, instrument_name, fields

Here is the expected output:

Fields received for CAD= : {'CF_BID': 1.3207, 'CF_ASK': 1.3208, 'OPEN_PRC': 1.3203, 'CF_HIGH': 1.3216, 'CF_LOW': 1.3195, 'CF_CLOSE': 1.3206}
Fields received for GBP= : {'CF_BID': 1.2939, 'CF_ASK': 1.2941, 'OPEN_PRC': 1.2952, 'CF_HIGH': 1.2969, 'CF_LOW': 1.2927, 'CF_CLOSE': 1.2952}
Fields received for JPY= : {'CF_BID': 108.68, 'CF_ASK': 108.71, 'OPEN_PRC': 108.66, 'CF_HIGH': 108.84, 'CF_LOW': 108.47, 'CF_CLOSE': 108.66}
Fields received for EUR= : {'CF_BID': 1.1078, 'CF_ASK': 1.108, 'OPEN_PRC': 1.107, 'CF_HIGH': 1.1082, 'CF_LOW': 1.1063, 'CF_CLOSE': 1.107}
Fields received for CAD= : {'CF_BID': 1.3207, 'CF_ASK': 1.3208}
Fields received for GBP= : {'CF_BID': 1.2939, 'CF_ASK': 1.2941}
Fields received for JPY= : {'CF_BID': 108.69, 'CF_ASK': 108.7}
Fields received for EUR= : {'CF_BID': 1.1077, 'CF_ASK': 1.1081}

Once we are done with events and want to stop receiving them we just close the StreamingPrices object like this:


More detailed examples

More detailed examples are available as Jupyter Notebooks in the Refinitiv API Samples Github group: 

For questions and feedback, please reach out to the Refinitiv Developer Community on the Eikon Data API Q&A forum.