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 0x7fd3747d4fd0 {name='rdp'}>

You can use the Refinitiv Data Library for Python to retrieve the stock prices 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 closing price for the Thomson Reuters stock for the last 365 calendar days:

from datetime import timedelta

rd.get_history(
    "TRI.TO",
    # Note that this number is negative because it's in the past
    start=timedelta(days=-365),
    # `end` is set to zero to draw the latest numbers
    end=timedelta(days=0),
)
/home/runner/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_tools/_dataframe.py:177:FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
TRI.TO TRDPRC_1 HIGH_1 LOW_1 ACVOL_UNS OPEN_PRC BID ASK VWAP BLKCOUNT BLKVOLUM NUM_MOVES TRD_STATUS SALTIM TRNOVR_UNS NAVALUE ALT_CLOSE
Date
2023-04-28 178.16 180.0 177.69 277310 179.12 177.81 178.58 178.2732 3 41200 1543 1 72929 49436947.31 <NA> 178.16
2023-05-01 179.07 179.57 177.6 404868 178.19 178.71 179.49 179.0182 6 274800 1079 1 72918 72478735.28 <NA> 179.07
2023-05-02 178.12 179.72 169.77 562216 173.89 178.0 178.75 177.1083 <NA> <NA> 4539 1 72000 99573102.02 <NA> 178.12
2023-05-03 170.09 175.82 169.78 502402 175.27 169.53 170.78 171.4574 2 67000 3701 1 72906 86140538.03 <NA> 170.09
2023-05-04 161.2 169.3 160.93 641898 169.04 160.8 163.68 163.3714 <NA> <NA> 5616 1 72000 104867796.36 <NA> 161.2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2024-04-19 206.67 208.5 205.69 204696 207.67 206.19 206.68 206.82967 <NA> <NA> 1606 1 72000 42337205.6 <NA> 206.67
2024-04-22 209.58 210.86 206.96 179357 206.96 209.22 210.12 209.52944 <NA> <NA> 1505 1 72000 37580571.24 <NA> 209.58
2024-04-23 209.92 211.48 209.37 206579 211.08 209.4 210.52 210.26773 <NA> <NA> 1794 1 72937 43436897.09 <NA> 209.92
2024-04-24 209.09 212.61 208.69 160582 210.81 208.51 209.66 209.72646 <NA> <NA> 1202 1 72979 33678295.08 <NA> 209.09
2024-04-25 208.35 209.18 205.67 170282 207.44 208.04 208.94 207.95457 <NA> <NA> 1554 1 72000 35410920.06 <NA> 208.35

250 rows × 16 columns

Hide code cell content
rd.close_session()