Fetching custom time ranges

Hide code cell content
import refinitiv.data as rd

rd.open_session()
/home/runner/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_access_layer/session.py:71:FutureWarning: 
You open a platform session using the default value of the signon_control parameter (signon_control=True).
In future library version v2.0, this default will be changed to False.
If you want to keep the same behavior as today, you will need to set the signon_control parameter to True either in the library configuration file
({'sessions':{'platform':{'your_session_name':{'signon_control':true}}}}) or in your code where you create the Platform Session.
These alternative options are already supported in the current version of the library.
<refinitiv.data.session.Definition object at 0x7f3bfcbe8fd0 {name='rdp'}>

You can use the Refinitiv Data Library for Python to retrieve economic indicators for custom time ranges by passing a start and end date to the get_history function.

The inputs should be datetime.timedelta objects. The start argument is how many days before today to start the range, and the end argument is how many days before today to end the range.

This example retrieves the US unemployment rate for the last 20 years:

from datetime import timedelta

rd.get_history(
    "USUNR=ECI",
    # Note that this number is negative because it's in the past
    start=timedelta(days=-365 * 20),
    # `end` is set to zero to draw the latest numbers
    end=timedelta(days=0),
)
USUNR=ECI VALUE
Date
2004-05-31 5.6
2004-06-30 5.6
2004-07-31 5.5
2004-08-31 5.4
2004-09-30 5.4
... ...
2023-11-30 3.7
2023-12-31 3.7
2024-01-31 3.7
2024-02-29 3.9
2024-03-31 3.8

239 rows × 1 columns

Hide code cell content
rd.close_session()