Tutorial Source Code |
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. If accessing content from:
The majority of the examples are built to work within Jupyter Notebook. Ensure this package is installed. |
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.
Note: The 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.
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.
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:
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.
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:
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.