The purpose of this Tutorial is to demonstrate an example of one of the DSWS API delivery methods (namely the Python API) and lead to useful case studies.
DSWS provides a Python API to access Datastream content. This document describes how one may import DatastreamPy and use it - along with its parameters.
1st, you will need Datastream DSWS credentials. Please note that these are most often different from your Workspace credentials and can be found in your 'Welcome to Refinitiv' email.
You may install DatastreamPy simply via pip install. You may do so in the built-in Python IDLE, or any other IDE of choice - be it Jupyter Notebook or CodeBook. (Note that you do not need to install DatastreamPy on CodeBook). To do so, simply run the below:
pip install DatastreamPy
If on Jupyter Notebook or Lab, you may instead need to run the below:
!pip install DatastreamPy
If the above proves insufficient, please do not hesitate to look into how you may pip install Python libraries on your IDE with an online searching engine of your choice before contacting experts in the community on the Q&A page.
import DatastreamPy as dsws
ds = DSWS.DataClient(None, username = 'DSWSusername', password = 'DSWSpassword')
The above creates a Python object already connected to DSWS with which one can request for data.
help(ds)
Help on DataClient in module DatastreamPy.DS_Response object:
class DataClient(builtins.object)
| DataClient(config=None, username=None, password=None, proxies=None, sslVerify=None, sslCert=None)
|
| DataClient helps to retrieve data from DSWS web rest service
|
| Methods defined here:
|
| Check_Token(self)
|
| IsValid(self)
|
| __init__(self, config=None, username=None, password=None, proxies=None, sslVerify=None, sslCert=None)
| Constructor: user details can be supplied from a config file or passed directly as parameters in the constructor of the derived user object type class.
|
| 1) Using ini file (e.g. config.ini) with format
|
| [credentials]
| username=YourID
| password=YourPwd
|
| [proxies]
| # of the form: { 'http' : proxyHttpAddress, 'https' : proxyHttpsAddress } See https://docs.python-requests.org/en/latest/user/advanced/
| proxies=ProxyDetails
|
| [cert]
| # option to supply a specific python requests verify option. See https://docs.python-requests.org/en/latest/user/advanced/
| sslVerify=YourCertPath
|
|
| # first logon with your credentials. Creating a DataClient instance (derived from DSConnect) with your credentials automatically logs on for you
| dataClient = DataClient('config.ini')
|
| 2) Bypassing a config file and using your credentials directly:
|
| dataClient = DataClient(None, 'YourId', 'YourPwd')
|
| get_bundle_data(self, bundleRequest=None, retName=False)
| This Function processes a multiple JSON format data requests to provide
| data response from DSWS web in the form of python Dataframe.
| Use post_user_request to form each JSON data request and append to a List
| to pass the bundleRequset.
|
| Args:
| bundleRequest: List, expects list of Datarequests
| retName: bool, default False, to be set to True if the Instrument
| names and Datatype names are to be returned
|
| Returns:
| DataFrame.
|
| get_data(self, tickers, fields=None, start='', end='', freq='', kind=1)
| This Function processes a single JSON format request to provide
| data response from DSWS web in the form of python Dataframe
|
| Args:
| tickers: string, Dataypes
| fields: List, default None
| start: string, default ''
| end : string, default ''
| freq : string, default '', By deafult DSWS treats as Daily freq
| kind: int, default 1, indicates Timeseries as output
| retName: bool, default False, to be set to True if the Instrument
| names and Datatype names are to be returned
|
| Returns:
| DataFrame.
|
| jsonDateTime_to_datetime(self, jsonDate)
|
| post_user_request(self, tickers, fields=None, start='', end='', freq='', kind=1)
| This function helps to form requests for get_bundle_data.
| Each request is converted to JSON format.
|
| Args:
| tickers: string, Dataypes
| fields: List, default None
| start: string, default ''
| end : string, default ''
| freq : string, default '', By deafult DSWS treats as Daily freq
| kind: int, default 1, indicates Timeseries as output
|
| Returns:
| Dictionary
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
You may now request for data via - for example - get_data. E.g.: you may request for quarterly US GDP for the year 2010 and retrieve a Pandas data-frame:
df = ds.get_data(tickers = "USGDP...D", fields = "X", start = '2010-01-01', end = '2011-01-01', freq = 'Q')
To find the field needed, please visit product.datastream, and navigate to the series, where you can search for your dataset of choice. In the above example, I searched for 'USA GDP' and used the Mnemonic of the 1st result.
In the screenshot below, the Mnemonic is circled in red, the fields of choice in blue, and the highest frequency available in green.
For more examples and explanations, please visit related articles in the Academic, Economic and Econometric or Sustainable Finance use-case landing pages. We also have a multi-part tutorial series looking into how to use DatastreamPy with CodeBook here if you are interested.