Fetching stock prices

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 0x7feeec510c40 {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-08-01 13:47:00 219.67 218.9 219.18 218.9 70 7500 <NA> <NA> <NA> <NA> ... 987 220.37 218.95 219.24 218.95 987 <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')
/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

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-07-03 168.4 170.29 168.4 55852 168.4 168.4 168.5 9439595 169.0109 <NA> ... <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
2024-07-04 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> ... 7980 9246 9274 1 <NA> <NA> 832278 2738 2411 <NA>
2024-07-05 169.22 169.7 167.58 88376 169.7 169.12 169.13 14913500 168.7506 1 ... 3074 9288 9312 1 <NA> <NA> 1658137 4351 3636 <NA>
2024-07-08 167.87 169.58 166.91 83826 169.58 167.87 167.97 14066352 167.8042 1 ... 2012 9324 9274 1 <NA> <NA> 753500 3191 2525 <NA>
2024-07-09 167.02 168.78 166.9 82874 168.01 167.07 167.12 13865832 167.3122 1 ... 5191 9272 9234 1 <NA> <NA> 695325 4094 3517 <NA>
2024-07-10 167.69 167.99 166.94 97018 167.07 167.58 167.69 16262454 167.6231 1 ... 5179 9270 9216 1 <NA> <NA> 518315 3167 2714 <NA>
2024-07-11 165.16 169.58 164.74 147061 168.0 165.05 165.16 24358644 165.6363 1 ... 9565 9236 9370 1 <NA> <NA> 882786 6806 5964 <NA>
2024-07-12 165.06 166.17 165.05 100635 166.09 165.06 165.13 16643040 165.3802 1 ... 5916 9414 9428 1 <NA> <NA> 727996 3968 3364 <NA>
2024-07-15 164.02 165.9 163.56 75929 165.52 164.01 164.02 12466755 164.1896 1 ... 9442 9392 9482 1 <NA> <NA> 1287545 3099 2686 <NA>
2024-07-16 165.0 165.32 163.87 133537 164.59 164.96 165.0 21987432 164.6542 1 ... 4530 9476 9448 1 <NA> <NA> 776402 3350 3061 <NA>
2024-07-17 164.01 164.22 162.89 334264 164.13 163.98 163.99 54746815 163.7832 1 ... 10205 9450 9404 1 <NA> <NA> 674293 4045 3544 <NA>
2024-07-18 163.1 164.15 161.49 184118 164.05 163.08 163.09 30007919 162.982 1 ... 5186 9442 9470 1 <NA> <NA> 706721 4458 4074 <NA>
2024-07-19 162.66 163.9 162.15 122164 163.31 162.63 162.73 19904438 162.9321 1 ... 5387 9432 9422 1 9420 60664 1418642 3780 3422 <NA>
2024-07-22 165.66 165.78 163.59 82251 163.59 165.59 165.64 13594002 165.2746 1 ... 1882 9438 9436 1 <NA> <NA> 501699 2733 2255 <NA>
2024-07-23 165.0 166.36 165.0 65563 165.8 165.0 165.06 10846048 165.4294 1 ... 3169 9412 9454 1 <NA> <NA> 602844 3238 2794 <NA>
2024-07-24 163.5 164.56 162.57 127207 164.56 163.46 163.51 20791111 163.4431 1 ... 5190 9400 9436 1 <NA> <NA> 539979 2668 2415 <NA>
2024-07-25 162.34 164.49 162.29 100268 163.88 162.27 162.29 16343208 162.9953 1 ... 8891 9366 9374 1 <NA> <NA> 2296441 4693 4416 <NA>
2024-07-26 161.7 163.32 161.16 131102 163.32 161.7 161.71 21210983 161.7899 1 ... 7480 9372 9478 1 <NA> <NA> 659160 4743 4287 <NA>
2024-07-29 162.09 162.39 160.785 181295 161.95 162.06 162.13 29312487 161.6839 1 ... 12573 9518 9468 1 <NA> <NA> 428074 3622 3183 <NA>
2024-07-30 160.63 162.99 160.11 117846 162.35 160.62 160.63 18962368 160.908 1 ... 9770 9470 9468 1 <NA> <NA> 978573 3093 2815 <NA>
2024-07-31 161.92 162.71 160.42 115334 161.58 161.9 162.03 18681567 161.978 1 ... 5447 9516 9470 1 <NA> <NA> 2185382 5463 4934 <NA>

21 rows × 46 columns

Hide code cell content
rd.close_session()