Fetching stock prices¶
Show 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 0x7f93c4304fd0 {name='rdp'}>
You can use the Refinitiv Data Library for Python to retrieve the latest stock prices for a single company by passing its Refinitiv Instrument Code to the get_data
function.
rd.get_data("TRI.TO")
The get_data
query requires that you account have access to real-time trading data, which is not available to all users. If you don’t, you can request the latest "1min"
intervals from the get_history
method.
rd.get_history(
"TRI.TO",
interval="1min",
).tail(1)
/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 | HIGH_1 | LOW_1 | OPEN_PRC | TRDPRC_1 | NUM_MOVES | ACVOL_UNS | HIGH_YLD | LOW_YLD | OPEN_YLD | YIELD | ... | BID_NUMMOV | ASK_HIGH_1 | ASK_LOW_1 | OPEN_ASK | ASK | ASK_NUMMOV | MID_HIGH | MID_LOW | MID_OPEN | MID_PRICE |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Timestamp | |||||||||||||||||||||
2024-04-26 14:47:00 | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | ... | 73 | 210.81 | 210.8 | 210.81 | 210.8 | 73 | <NA> | <NA> | <NA> | <NA> |
1 rows × 24 columns
Historical data¶
You can retrieve historical stock prices by passing a Refinitiv Instrument Code to the get_history
function. By default it returns the closing price for the last 30 days.
rd.get_history('TRI.N')
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-03-28 | 155.83 | 157.6 | 155.03 | 136750 | 156.17 | 155.69 | 155.77 | 21297952 | 155.7437 | 1 | 38347 | 1451 | 1 | 72600 |
2024-04-01 | 155.3 | 155.84 | 154.07 | 49537 | 155.84 | 155.24 | 155.25 | 7684388 | 155.1242 | 1 | 18664 | 783 | 1 | 72600 |
2024-04-02 | 152.73 | 154.26 | 152.12 | 74566 | 153.79 | 152.67 | 152.73 | 11394046 | 152.8048 | 1 | 23812 | 992 | 1 | 72600 |
2024-04-03 | 152.44 | 153.49 | 151.98 | 59196 | 152.08 | 152.36 | 152.44 | 9032983 | 152.5945 | 1 | 22021 | 839 | 1 | 72600 |
2024-04-04 | 152.15 | 153.71 | 151.31 | 144144 | 153.12 | 152.05 | 152.15 | 21929318 | 152.1348 | 1 | 89214 | 878 | 1 | 72600 |
2024-04-05 | 151.94 | 152.91 | 151.72 | 73655 | 151.76 | 151.93 | 152.0 | 11198767 | 152.0435 | 1 | 18916 | 901 | 1 | 72600 |
2024-04-08 | 153.97 | 154.2 | 152.25 | 58333 | 152.25 | 153.93 | 153.94 | 8971913 | 153.8051 | 1 | 16132 | 810 | 1 | 72600 |
2024-04-09 | 154.54 | 154.59 | 152.28 | 63643 | 154.18 | 154.54 | 154.59 | 9813995 | 154.2038 | 1 | 13254 | 725 | 1 | 72600 |
2024-04-10 | 153.17 | 153.69 | 152.14 | 75560 | 153.35 | 153.17 | 153.21 | 11577309 | 153.2201 | 1 | 25115 | 807 | 1 | 72600 |
2024-04-11 | 154.55 | 154.66 | 151.89 | 80012 | 153.93 | 154.54 | 154.55 | 12323739 | 154.0236 | 1 | 16442 | 979 | 1 | 72600 |
2024-04-12 | 152.38 | 154.02 | 152.18 | 114429 | 153.27 | 152.3 | 152.31 | 17457709 | 152.5637 | 1 | 14924 | 1395 | 1 | 72600 |
2024-04-15 | 152.55 | 154.71 | 152.41 | 72123 | 153.47 | 152.62 | 152.63 | 11035400 | 153.0081 | 1 | 23057 | 1018 | 1 | 72600 |
2024-04-16 | 152.29 | 153.15 | 151.88 | 70968 | 152.74 | 152.29 | 152.41 | 10818902 | 152.4476 | 1 | 19346 | 882 | 1 | 72600 |
2024-04-17 | 152.85 | 153.72 | 151.56 | 67852 | 153.19 | 152.85 | 152.9 | 10371667 | 152.8572 | 1 | 17920 | 844 | 1 | 72600 |
2024-04-18 | 150.79 | 153.05 | 150.42 | 91115 | 153.05 | 150.76 | 150.77 | 13784004 | 151.2814 | 1 | 17998 | 1203 | 1 | 72600 |
2024-04-19 | 150.22 | 151.63 | 149.55 | 89600 | 150.79 | 150.22 | 150.33 | 13481424 | 150.4623 | 1 | 16898 | 1359 | 1 | 72600 |
2024-04-22 | 152.99 | 153.99 | 151.14 | 74409 | 151.2 | 152.99 | 153.0 | 11372959 | 152.8439 | 1 | 19596 | 927 | 1 | 72600 |
2024-04-23 | 153.59 | 154.61 | 153.28 | 72989 | 153.9 | 153.54 | 153.64 | 11219982 | 153.7216 | 1 | 19619 | 846 | 1 | 72600 |
2024-04-24 | 152.63 | 155.12 | 152.16 | 70534 | 153.56 | 152.63 | 152.71 | 10792980 | 153.0181 | 1 | 22383 | 801 | 1 | 72600 |
2024-04-25 | 152.57 | 153.11 | 150.07 | 77710 | 151.05 | 152.57 | 152.58 | 11843493 | 152.4063 | 1 | 17395 | 918 | 1 | 72600 |
Multiple instruments¶
You can retrieve data for multiple instruments by passing a list of Refinitiv Instrument Codes to the get_data
and get_history
functions.
rd.get_history(['TRI.N', 'LSEG.L'])
/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 | ... | LSEG.L | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TRDPRC_1 | HIGH_1 | LOW_1 | ACVOL_UNS | OPEN_PRC | BID | ASK | TRNOVR_UNS | VWAP | BLKCOUNT | ... | OPN_AUCVOL | OPN_AUC | CLS_AUC | TRD_STATUS | INT_AUC | INT_AUCVOL | EX_VOL_UNS | ALL_C_MOVE | ELG_NUMMOV | NAVALUE | |
Date | |||||||||||||||||||||
2024-03-27 | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | ... | 12488 | 9676 | 9592 | 1 | <NA> | <NA> | 1849871 | 4268 | 3778 | <NA> |
2024-03-28 | 155.83 | 157.6 | 155.03 | 136750 | 156.17 | 155.69 | 155.77 | 21297952 | 155.7437 | 1 | ... | 11699 | 9588 | 9490 | 1 | <NA> | <NA> | 1134822 | 6522 | 6001 | <NA> |
2024-04-01 | 155.3 | 155.84 | 154.07 | 49537 | 155.84 | 155.24 | 155.25 | 7684388 | 155.1242 | 1 | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> |
2024-04-02 | 152.73 | 154.26 | 152.12 | 74566 | 153.79 | 152.67 | 152.73 | 11394046 | 152.8048 | 1 | ... | 36885 | 9486 | 9424 | 1 | <NA> | <NA> | 1745849 | 6553 | 5861 | <NA> |
2024-04-03 | 152.44 | 153.49 | 151.98 | 59196 | 152.08 | 152.36 | 152.44 | 9032983 | 152.5945 | 1 | ... | 8704 | 9398 | 9360 | 1 | <NA> | <NA> | 943311 | 4708 | 4356 | <NA> |
2024-04-04 | 152.15 | 153.71 | 151.31 | 144144 | 153.12 | 152.05 | 152.15 | 21929318 | 152.1348 | 1 | ... | 9662 | 9374 | 9414 | 1 | <NA> | <NA> | 846512 | 3795 | 3463 | <NA> |
2024-04-05 | 151.94 | 152.91 | 151.72 | 73655 | 151.76 | 151.93 | 152.0 | 11198767 | 152.0435 | 1 | ... | 12199 | 9340 | 9378 | 1 | <NA> | <NA> | 2933896 | 5928 | 5596 | <NA> |
2024-04-08 | 153.97 | 154.2 | 152.25 | 58333 | 152.25 | 153.93 | 153.94 | 8971913 | 153.8051 | 1 | ... | 8308 | 9362 | 9236 | 1 | <NA> | <NA> | 1058179 | 7139 | 6741 | <NA> |
2024-04-09 | 154.54 | 154.59 | 152.28 | 63643 | 154.18 | 154.54 | 154.59 | 9813995 | 154.2038 | 1 | ... | 7198 | 9238 | 9190 | 1 | <NA> | <NA> | 1943308 | 5520 | 5135 | <NA> |
2024-04-10 | 153.17 | 153.69 | 152.14 | 75560 | 153.35 | 153.17 | 153.21 | 11577309 | 153.2201 | 1 | ... | 8348 | 9160 | 9270 | 1 | <NA> | <NA> | 1388239 | 6304 | 5942 | <NA> |
2024-04-11 | 154.55 | 154.66 | 151.89 | 80012 | 153.93 | 154.54 | 154.55 | 12323739 | 154.0236 | 1 | ... | 2523 | 9280 | 9306 | 1 | <NA> | <NA> | 746959 | 6424 | 6037 | <NA> |
2024-04-12 | 152.38 | 154.02 | 152.18 | 114429 | 153.27 | 152.3 | 152.31 | 17457709 | 152.5637 | 1 | ... | 7760 | 9328 | 9314 | 1 | <NA> | <NA> | 678956 | 4919 | 4500 | <NA> |
2024-04-15 | 152.55 | 154.71 | 152.41 | 72123 | 153.47 | 152.62 | 152.63 | 11035400 | 153.0081 | 1 | ... | 7767 | 9300 | 9270 | 1 | <NA> | <NA> | 737058 | 3323 | 3032 | <NA> |
2024-04-16 | 152.29 | 153.15 | 151.88 | 70968 | 152.74 | 152.29 | 152.41 | 10818902 | 152.4476 | 1 | ... | 7834 | 9222 | 9152 | 1 | <NA> | <NA> | 3220580 | 6611 | 6264 | <NA> |
2024-04-17 | 152.85 | 153.72 | 151.56 | 67852 | 153.19 | 152.85 | 152.9 | 10371667 | 152.8572 | 1 | ... | 5355 | 9114 | 9130 | 1 | <NA> | <NA> | 586739 | 2598 | 2216 | <NA> |
2024-04-18 | 150.79 | 153.05 | 150.42 | 91115 | 153.05 | 150.76 | 150.77 | 13784004 | 151.2814 | 1 | ... | 4695 | 9100 | 9036 | 1 | <NA> | <NA> | 1407523 | 3310 | 2866 | <NA> |
2024-04-19 | 150.22 | 151.63 | 149.55 | 89600 | 150.79 | 150.22 | 150.33 | 13481424 | 150.4623 | 1 | ... | 5941 | 9000 | 8984 | 1 | 8960 | 28266 | 3657199 | 4284 | 3954 | <NA> |
2024-04-22 | 152.99 | 153.99 | 151.14 | 74409 | 151.2 | 152.99 | 153.0 | 11372959 | 152.8439 | 1 | ... | 9259 | 9084 | 9100 | 1 | <NA> | <NA> | 1009649 | 5697 | 5229 | <NA> |
2024-04-23 | 153.59 | 154.61 | 153.28 | 72989 | 153.9 | 153.54 | 153.64 | 11219982 | 153.7216 | 1 | ... | 14524 | 9200 | 8998 | 1 | <NA> | <NA> | 2413325 | 6888 | 6425 | <NA> |
2024-04-24 | 152.63 | 155.12 | 152.16 | 70534 | 153.56 | 152.63 | 152.71 | 10792980 | 153.0181 | 1 | ... | 6090 | 8998 | 8808 | 1 | <NA> | <NA> | 1515403 | 7681 | 7091 | <NA> |
2024-04-25 | 152.57 | 153.11 | 150.07 | 77710 | 151.05 | 152.57 | 152.58 | 11843493 | 152.4063 | 1 | ... | 19524 | 8676 | 8890 | 1 | <NA> | <NA> | 2030184 | 9269 | 8659 | <NA> |
21 rows × 46 columns
Show code cell content
rd.close_session()