Authenticating in Python¶
You can authenticate with the Refinitiv Data Library for Python directly inline using a function in the package’s session
module.
It requires that provide your application key, as well as the username and password you use to access the Refinitiv web portal. In this example, they will be stored in environment variables to avoid exposing private information in the source code.
import os
import refinitiv.data as rd
session = rd.session.platform.Definition(
app_key=os.getenv('RDP_APP_KEY'),
grant=rd.session.platform.GrantPassword(
username=os.getenv('RDP_USERNAME'),
password=os.getenv('RDP_PASSWORD')
)
).get_session()
/home/runner/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/session/platform.py:90: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.
Once that’s successful, you can open a session:
session.open()
<OpenState.Opened: 'Opened'>
Then it as the default for the library.
rd.session.set_default(session)
Verify the session is open by executing a query for the current price of Thomson Reuters stock. You should back a table of data.
rd.get_history('TRI.N')
/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.N | TRDPRC_1 | HIGH_1 | LOW_1 | ACVOL_UNS | OPEN_PRC | BID | ASK | TRNOVR_UNS | VWAP | BLKCOUNT | BLKVOLUM | NUM_MOVES | TRD_STATUS | SALTIM |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | ||||||||||||||
2024-07-03 | 168.4 | 170.29 | 168.4 | 55852 | 168.4 | 168.4 | 168.5 | 9439595 | 169.0109 | <NA> | <NA> | 955 | 1 | 75600 |
2024-07-05 | 169.22 | 169.7 | 167.58 | 88376 | 169.7 | 169.12 | 169.13 | 14913500 | 168.7506 | 1 | 18545 | 1108 | 1 | 72600 |
2024-07-08 | 167.87 | 169.58 | 166.91 | 83826 | 169.58 | 167.87 | 167.97 | 14066352 | 167.8042 | 1 | 16245 | 1113 | 1 | 72600 |
2024-07-09 | 167.02 | 168.78 | 166.9 | 82874 | 168.01 | 167.07 | 167.12 | 13865832 | 167.3122 | 1 | 18381 | 1033 | 1 | 72600 |
2024-07-10 | 167.69 | 167.99 | 166.94 | 97018 | 167.07 | 167.58 | 167.69 | 16262454 | 167.6231 | 1 | 13290 | 1432 | 1 | 72600 |
2024-07-11 | 165.16 | 169.58 | 164.74 | 147061 | 168.0 | 165.05 | 165.16 | 24358644 | 165.6363 | 1 | 42893 | 1639 | 1 | 72600 |
2024-07-12 | 165.06 | 166.17 | 165.05 | 100635 | 166.09 | 165.06 | 165.13 | 16643040 | 165.3802 | 1 | 21061 | 1444 | 1 | 72600 |
2024-07-15 | 164.02 | 165.9 | 163.56 | 75929 | 165.52 | 164.01 | 164.02 | 12466755 | 164.1896 | 1 | 21484 | 1198 | 1 | 72600 |
2024-07-16 | 165.0 | 165.32 | 163.87 | 133537 | 164.59 | 164.96 | 165.0 | 21987432 | 164.6542 | 1 | 29172 | 1387 | 1 | 72600 |
2024-07-17 | 164.01 | 164.22 | 162.89 | 334264 | 164.13 | 163.98 | 163.99 | 54746815 | 163.7832 | 1 | 47958 | 3205 | 1 | 72600 |
2024-07-18 | 163.1 | 164.15 | 161.49 | 184118 | 164.05 | 163.08 | 163.09 | 30007919 | 162.982 | 1 | 39705 | 1896 | 1 | 72600 |
2024-07-19 | 162.66 | 163.9 | 162.15 | 122164 | 163.31 | 162.63 | 162.73 | 19904438 | 162.9321 | 1 | 37538 | 1351 | 1 | 72600 |
2024-07-22 | 165.66 | 165.78 | 163.59 | 82251 | 163.59 | 165.59 | 165.64 | 13594002 | 165.2746 | 1 | 17538 | 1019 | 1 | 72600 |
2024-07-23 | 165.0 | 166.36 | 165.0 | 65563 | 165.8 | 165.0 | 165.06 | 10846048 | 165.4294 | 1 | 22516 | 925 | 1 | 72600 |
2024-07-24 | 163.5 | 164.56 | 162.57 | 127207 | 164.56 | 163.46 | 163.51 | 20791111 | 163.4431 | 1 | 40941 | 1308 | 1 | 72600 |
2024-07-25 | 162.34 | 164.49 | 162.29 | 100268 | 163.88 | 162.27 | 162.29 | 16343208 | 162.9953 | 1 | 20387 | 1339 | 1 | 72600 |
2024-07-26 | 161.7 | 163.32 | 161.16 | 131102 | 163.32 | 161.7 | 161.71 | 21210983 | 161.7899 | 1 | 36146 | 1612 | 1 | 72600 |
2024-07-29 | 162.09 | 162.39 | 160.785 | 181295 | 161.95 | 162.06 | 162.13 | 29312487 | 161.6839 | 1 | 31594 | 2188 | 1 | 72600 |
2024-07-30 | 160.63 | 162.99 | 160.11 | 117846 | 162.35 | 160.62 | 160.63 | 18962368 | 160.908 | 1 | 27198 | 1548 | 1 | 72600 |
2024-07-31 | 161.92 | 162.71 | 160.42 | 115334 | 161.58 | 161.9 | 162.03 | 18681567 | 161.978 | 1 | 35134 | 1466 | 1 | 72600 |
The library expects you to close your session when you’re finished. You can do so by running the following code:
rd.close_session()