Tutorial Source Code

Python

Examples referenced

Last Update July 2021
Interpreter Python 3.7.x or greater
Pre-requisites

Familiarity with Python and a basic understanding of Jupyter Notebook.

Access Credentials

Quick Start

If accessing content from:

  • Desktop (Eikon 4.0.36 or greater / Refinitiv Workspace 1.8 or greater) 
  • Deployed Environment (ADS 3.2 or greater)

The majority of the examples are built to work within Jupyter Notebook.  Ensure this package is installed.




Overview

Whether accessing from the desktop, directly to the cloud, or through your deployed managed streaming services, the Refinitiv Data Libraries provide ease-of-use interfaces to retrieve real-time streaming content from the platform. By utilizing streaming interfaces, developers can easily define properties to request, receive, and process market data in real-time.

The following tutorial requests for a small watchlist of instruments, retrieving level 1 (MarketPrice) streaming content that will automatically update an internally-manage cache in real-time.  The Pricing interface includes a number of powerful features, one of which is the ability to cache real-time events in the background for the purpose of fast and easy retrieval.  The clear advantage is the convenience and simplicity when accessing live prices but also provides fast access by avoiding the need to traverse the network whenever there is a choice to retrieve up-to-date prices. 

Executing the Tutorial

In our example, we're requesting to stream 3 instruments.  The goal of the example is to show the initial values coming in and how they change as market conditions change.  The example does not capture and display the real-time changes but rather updates an internally managed cache behind the scenes.  The example demonstrates the ability to retrieve values from the Pricing cache and shows values changing every few seconds.  For example, the following output highlights the initial values for one of the instruments and what happens a few seconds later when we display the cache.  We will see the new values based on the changes in the market for that specific instrument.

The expected output should look something like this:

  Instrument BID ASK
0 EUR= 1.189 1.1894
1 GBP= 1.3901 1.3905
2 JPY= 110.56 110.57

Call get_snapshot() again after a few seconds:

  Instrument BID ASK
0 EUR= 1.1891 1.1893
1 GBP= 1.39 1.3904
2 JPY= 110.54 110.55

As you will note, most of the values have changed during the intermediate time between the 1st and 2nd get_snapshot() call. 

 

How to request and extract live values from the cache

The following code segment requests for a small watchlist of items from the streaming services defined within the platform:

    	
            

# Define our Streaming Price object

streaming_cache = rd.content.pricing.Definition(

    universe=['EUR=', 'GBP=', 'JPY='],

    fields=['BID', 'ASK']

).get_stream()

# Open the Stream - Once opened, the library starts caching the updates

streaming_cache.open()

# When you want the current price, get a snapshot

streaming_cache.get_snapshot()

The above code segment performs the following key actions:

  • Define a Pricing request
    The Pricing interface provides the ability to specify a list of instruments to stream.  In addition, includes a number of other options, including the ability to specify a list of fields.  It's worth noting that if no fields are specified in the definition, all available fields will be returned.  While not specifying the fields is a convenient way to look at all available fields, in practice, it's best to limit the fieldset based on requirements to optimize performance.

  • Retrieve a stream interface
    Using the pricing definition, retrieve a streaming interface providing the ability to request and manage real-time streaming data.

  • Open the stream
    Using the stream interface, open the stream.  Opening a stream performs 2 distinct actions.  First, the request will instruct the streaming services to deliver a complete image, or refresh, of the latest values for the specified fields.  Second, the streaming services will begin to deliver real-time updates based on changes in the live market.  The stream interface will automatically react to these real-time events in order to properly maintain a live pricing cache.

  • Snap the current values from the Cache
    Once the streams are open and the values are being received and cached, we can call get_snapshot to get the live values

Once in place, we can begin to perform different actions on our streams to extract live data.  For example, the following code segment will extract a snapshot of the live cache and iterate through the cache to display values.

    	
            

# Iterate through all instruments in cache and display latest field values

for instrument in streaming:

    print(instrument.name)

    for field_name, field_value in instrument:

        print(f"\t{field_name} : {field_value}")

The individual instruments available within the cache define simple access methods to retrieve properties for each.  For example:

    	
            

# Directly access the latest 'BID' price for the EURO
streaming['EUR=']['BID']

 

# Access the Pricing Stream object for GBP
gbp = streaming['GBP=']
# and then use object to access individual fields
gbp['ASK']

 

Closing the stream

When you wish to stop the flow of live updates, you will need to issue a close on the stream.  The following code segment will close the stream:

    	
            

# Close an individual instrument's stream
gbp.close();

 

# Close all the instruments opened above
streaming.close()

In our example code, we included a simple way to extract a snapshot of the entire cache.  However, the interface also supports the ability to snapshot a single or subset of items from the cache as opposed to copying the whole collection.  In addition, the interface also allows the specification of individual fields to be extracted when performing a cache snapshot.  In the last example code snippet, we showed a convenient way to access elements directly from the stream as opposed to performing a cache snapshot.  The choice between accessing the stream directly or performing an explicit cache snapshot depends on the specific workflow and use case.

It's worth noting that the Pricing interface inherently creates a live cache as part of its design.  While this is automatic and convenient, users may choose to manage their own cache by utilizing the event capabilities of streaming.  Users can optionally utilize the OMM Stream interface defined within the Delivery layer if they choose to have more control over how items are cached within their applications or if they prefer to forego the need for a cache.

The Pricing interface defined within the Refinitiv Data Library for Python includes other useful features, such as the ability to retrieve real-time prices available within the RDP snapshot pricing service.  This capability can be extremely useful for users that may not have access to or require the need to stream prices through real-time streaming services.  Refer to the tutorial notebook TUT_2.2.02-Pricing-StreamingCache for additional examples. In addition, we encourage the use of auto-complete within your Jupyter Notebook or Python editor to discover other useful capabilities.