Refinitiv Data Library for Python

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 content from both standard web-based APIs and real-time streaming services.  Built on top of standard request/reply interfaces as well as HTML 5 WebSocket streaming protocols, the library includes a Pricing interface that allows developers to define instruments and properties to retrieve real-time, level 1 (MarketPrice) prices.

The following tutorial requests for a small watchlist of instruments, retrieving real-time prices available within the RDP's Pricing snapshot endpoint or within streaming services.  In addition, the tutorial will include ways to access the data elements from the response.

NoteThe intention of this tutorial is to demonstrate the ability to retrieve a snapshot of real-time prices.  The Pricing interface supports 2 ways to retrieve real-time prices - through RDP's pricing snapshot interface or through Refinitiv's streaming services.  Depending on your permissions and what you entitled to access, this tutorial will demonstrate both ways to request real-time snapshots.

Executing the Application

In our examples, we're requesting to retrieve a snapshot of 3 instruments and demonstrate ways to extract the details from the response.   A snapshot response contains the entire real-time image of the data based on the fields of interest.  The tutorial demonstrates common ways to extract the data elements from the response.

Depending on what you are entitled to access, choose the appropriate example within the source code package.  To execute either example, refer to the pre-requisites section at the top of this tutorial.

The expected output should look similar to this:

  Instruments BID ASK
0 EUR= 1.2129 1.213
1 GBP= 1.4087 1.4088
2 JPY= 110.07 110.08
3 CAD= 1.2178 1.2182

Iterate through the cache and display some fields:

EUR=
    BID : 1.213
    ASK : 1.2131
GBP=
    BID : 1.4087
    ASK : 1.4088
JPY=
    BID : 110.07
    ASK : 110.08
CAD=
    BID : 1.2179
    ASK : 1.218

The output is a result of extracting a field element within a specific instrument, displaying the entire contents of an instrument, and walking through the entire response, display values for each.

Requesting and extracting real-time prices from the snapshot service

The following code segment requests for a small watchlist of items from the real-time snapshot service defined within the platform:

    	
            

# Define our RDP Snapshot Price object - note the get_data() call 

response = rd.content.pricing.Definition(

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

    fields=['BID', 'ASK']

).get_data()

response.data.df

The above code segment has 2 distinct key steps in the first statement:

  • 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.
  • Request the data
    Using the definition, request to retrieve a snapshot of data from the pricing service - by calling the get_data() function.  The request will carry the attributes of the definition that are relevant to the service and respond with a complete collection of fields for each instrument specified.

The response should contain a dataframe if the request was valid

The details specific to the snapshot service are captured within a container defined within the data element.  The container represents the entire collection of instruments and the corresponding values for each specified field.  If there is a need to pull out a specific field for one of the instruments requested, you can easily do that by referencing the item using standard subscript operators.  In addition, you should be able to pull out the entire data record for a specified instrument from the response.  And finally, the ability to iterate through the details of the response and pull out individual elements.

Requesting and extracting real-time prices from the streaming service

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

    	
            

# Define our Streaming Price object - note the get_stream() call

non_streaming = rd.content.pricing.Definition(

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

    fields=['BID', 'ASK']

).get_stream()

 

# If you want to snap just the current prices,
# open the instrument in non-streaming mode - i.e. no updates

non_streaming.open(with_updates=False)

# Snapshot the prices at the time of the open() call

non_streaming.get_snapshot()

The above code segment was intentionally broken out into 3 distinct steps for the purpose of explanation, but also provides flexibility when using the interfaces.  In general, the above code performs the following:

  • 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 - in snapshot mode
    Using the stream interface, open the stream.  By default, the streaming interface will stream updates when changes occur in the market.  In the above code segment, we are instructing the stream to turn off streaming so we only receive the refresh or initial image from the streaming services.  Doing this effectively mirrors the actions of requesting a snapshot from the RDP snapshot pricing service. Because we are not interested in streaming updates, the service will deliver the initial refresh for each item requested.

Once completed, we begin to extract and display details populated within the data. 

    	
            

# Print whole dataframe  - all instruments and fields 
print(non_streaming.get_snapshot())

 

# Direct access to a field for EURO
non_streaming['EUR=']['BID']

 

# Direct access to 1 instrument and field
eur = non_streaming['EUR=']
eur['BID']

 

# Iterate on instruments and fields

for instrument in non_streaming:

    print(instrument.name)

    for field_name, field_value in instrument:

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

The Pricing interface defined within the Refinitiv Data Library for Python includes other useful features, such as the ability to stream live quotes and trades within user-defined callbacks and to manage a real-time, in-memory, cache.  Refer to the tutorial package for additional examples.   In addition, we encourage the use of auto-complete within your Jupyter Notebook or Python editor to discover other useful capabilities.