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 DatastreamDSWS and use it - along with its parameters.
1st, you will need Datastream DSWS credentials. Please note that these are most often different from your Eikon/Workspace credentials and can be found in your 'Welcome to Refinitiv' email.
You may install DatastreamDSWS 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 DatastreamDSWS on CodeBook). To do so, simply run the below:
pip install DatastreamDSWS as dsws
If on Jupyter Notebook or Lab, you may instead need to run the below:
!pip install DatastreamDSWS
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 DatastreamDSWS as dsws
ds = dsws.Datastream(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 Datastream in module DatastreamDSWS.DS_Response object:
class Datastream(builtins.object)
| Datastream(username, password, config=None, dataSource=None, proxy=None, sslCer=None)
|
| Datastream helps to retrieve data from DSWS web rest service
|
| Methods defined here:
|
| __init__(self, username, password, config=None, dataSource=None, proxy=None, sslCer=None)
| Initialize self. See help(type(self)) for accurate signature.
|
| 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, retName=False)
| 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.
|
| post_user_request(self, tickers, fields=None, start='', end='', freq='', kind=1, retName=False)
| 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
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 DatastreamDSWS with CodeBook here if you are interested.