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 0x7fd210911990 {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-08-03 175.11 177.58 174.51 326944 176.77 174.8 175.79 175.5584 <NA> <NA> 2426 1 72000 57397762.48 <NA> 175.11
2023-08-04 176.07 177.69 175.15 505158 175.15 175.51 176.38 176.3369 1 131500 2740 1 72000 89077970.99 <NA> 176.07
2023-08-08 174.87 176.87 173.1 384439 175.07 174.46 175.21 174.9201 1 17000 2743 1 72000 67246091.86 <NA> 174.87
2023-08-09 175.97 176.48 174.39 220834 174.99 175.54 176.49 175.8187 1 28400 1436 1 72000 38826738.84 <NA> 175.97
2023-08-10 176.65 179.5 175.88 278183 179.49 176.2 176.99 176.8403 <NA> <NA> 2148 1 72000 49193959.06 <NA> 176.65
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2024-07-25 224.44 227.32 224.38 249547 225.99 222.68 226.95 225.52931 2 23100 1933 1 72000 56280163.41 <NA> 224.44
2024-07-26 223.65 225.89 222.98 289195 225.28 223.0 227.0 223.81105 1 69900 1800 1 72000 64725036.64 <NA> 223.65
2024-07-29 224.45 224.9 222.78 213306 223.97 223.0 224.7 223.94578 <NA> <NA> 1818 1 72000 47768978.23 <NA> 224.45
2024-07-30 222.56 226.41 221.66 420740 224.55 221.19 223.9 222.95476 2 99300 2500 1 72000 93805986.18 <NA> 222.56
2024-07-31 223.75 224.49 221.34 355450 222.88 222.7 242.0 223.66221 1 15400 2567 1 72000 79500731.57 <NA> 223.75

250 rows × 16 columns

Hide code cell content
rd.close_session()