Fetching index 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 0x7f06040d1990 {name='rdp'}>
You can use the Refinitiv Data Library for Python to retrieve the latest value of a stock index by passing its Refinitiv Instrument Code to the get_data
function. They are typically prefixed with a period. Here’s the Dow Jones Industrial Average:
rd.get_data(".DJI")
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(
".DJI",
interval="1min",
).tail(1)
.DJI | HIGH_1 | LOW_1 | OPEN_PRC | TRDPRC_1 | NUM_MOVES | ACVOL_UNS | BID_HIGH_1 | BID_LOW_1 | OPEN_BID | BID | BID_NUMMOV | ASK_HIGH_1 | ASK_LOW_1 | OPEN_ASK | ASK | ASK_NUMMOV |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Timestamp | ||||||||||||||||
2024-08-01 14:03:00 | 40845.7 | 40830.61 | 40845.65 | 40843.1 | 35 | 459402 | 40829.0 | 40813.82 | 40825.32 | 40822.75 | 36 | 40864.14 | 40850.15 | 40857.9 | 40860.46 | 36 |
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('.DJI')
.DJI | TRDPRC_1 | OPEN_PRC | HIGH_1 | LOW_1 | ACVOL_UNS | ASK | THEO_OPEN | BID | NUM_MOVES |
---|---|---|---|---|---|---|---|---|---|
Date | |||||||||
2024-07-03 | 39308.0 | 39358.95 | 39411.17 | 39230.86 | 202071389 | 39349.26 | 39361.28 | 39217.8 | 13802 |
2024-07-05 | 39375.87 | 39313.4 | 39399.62 | 39168.7 | 331490015 | 39433.75 | 39300.05 | 39306.16 | 24602 |
2024-07-08 | 39344.79 | 39391.98 | 39654.96 | 39278.43 | 364051095 | 39441.04 | 39456.87 | 39274.1 | 24602 |
2024-07-09 | 39291.97 | 39357.37 | 39492.28 | 39146.6 | 365564644 | 39401.23 | 39325.61 | 39209.13 | 24602 |
2024-07-10 | 39721.36 | 39272.45 | 39736.2 | 39256.72 | 336924793 | 39778.19 | 39263.72 | 39560.74 | 24602 |
2024-07-11 | 39753.75 | 39695.18 | 39875.6 | 39623.12 | 363850306 | 39943.48 | 39723.73 | 39647.98 | 24602 |
2024-07-12 | 40000.9 | 39783.28 | 40257.24 | 39783.28 | 329069849 | 40125.13 | 39868.46 | 39905.84 | 24602 |
2024-07-15 | 40211.72 | 40138.4 | 40351.1 | 40136.1 | 322332993 | 40390.15 | 40215.07 | 40206.0 | 24602 |
2024-07-16 | 40954.48 | 40263.78 | 40988.81 | 40263.78 | 313520180 | 41016.95 | 40382.0 | 40865.85 | 24602 |
2024-07-17 | 41198.08 | 40862.57 | 41221.98 | 40849.71 | 438858895 | 41302.27 | 40851.99 | 41056.57 | 24602 |
2024-07-18 | 40665.02 | 41156.56 | 41376.0 | 40597.5 | 398285177 | 40921.3 | 41052.43 | 40623.83 | 24602 |
2024-07-19 | 40287.53 | 40592.35 | 40626.16 | 40203.27 | 354442967 | 40365.25 | 40628.36 | 40152.46 | 24602 |
2024-07-22 | 40415.44 | 40414.49 | 40472.53 | 40222.77 | 358564742 | 40474.5 | 40381.74 | 40330.89 | 24602 |
2024-07-23 | 40358.09 | 40443.73 | 40527.89 | 40319.7 | 296803005 | 40441.26 | 40423.32 | 40193.85 | 24602 |
2024-07-24 | 39853.87 | 40210.63 | 40258.44 | 39807.45 | 400901853 | 40068.04 | 40167.37 | 39876.4 | 24602 |
2024-07-25 | 39935.07 | 39828.63 | 40438.82 | 39817.5 | 382491163 | 40140.44 | 39866.81 | 39869.64 | 24602 |
2024-07-26 | 40589.34 | 40140.86 | 40753.83 | 40140.86 | 334454548 | 40699.84 | 40169.67 | 40489.68 | 24602 |
2024-07-29 | 40539.93 | 40665.71 | 40682.49 | 40388.83 | 277552406 | 40664.16 | 40651.68 | 40473.98 | 24602 |
2024-07-30 | 40743.33 | 40622.13 | 40866.25 | 40529.1 | 358447974 | 40715.47 | 40581.65 | 40332.79 | 24602 |
2024-07-31 | 40842.79 | 40768.88 | 41198.63 | 40655.89 | 420099927 | 41047.37 | 40720.6 | 40666.73 | 24602 |
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(['.DJI', '.FTSE'])
/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)`
.DJI | .FTSE | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TRDPRC_1 | OPEN_PRC | HIGH_1 | LOW_1 | ACVOL_UNS | ASK | THEO_OPEN | BID | NUM_MOVES | TRDPRC_1 | OPEN_PRC | HIGH_1 | LOW_1 | ACVOL_UNS | TRNOVR_UNS | TRDVOL_1 | |
Date | ||||||||||||||||
2024-07-03 | 39308.0 | 39358.95 | 39411.17 | 39230.86 | 202071389 | 39349.26 | 39361.28 | 39217.8 | 13802 | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> |
2024-07-04 | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | 8241.26 | 8171.12 | 8257.95 | 8170.97 | 558635082 | 278564.33 | 26167798 |
2024-07-05 | 39375.87 | 39313.4 | 39399.62 | 39168.7 | 331490015 | 39433.75 | 39300.05 | 39306.16 | 24602 | 8203.93 | 8241.26 | 8279.75 | 8182.7 | 999386717 | 578812.71 | 38044661 |
2024-07-08 | 39344.79 | 39391.98 | 39654.96 | 39278.43 | 364051095 | 39441.04 | 39456.87 | 39274.1 | 24602 | 8193.49 | 8203.93 | 8232.81 | 8175.26 | 703743592 | 374160.94 | 3428408 |
2024-07-09 | 39291.97 | 39357.37 | 39492.28 | 39146.6 | 365564644 | 39401.23 | 39325.61 | 39209.13 | 24602 | 8139.81 | 8193.49 | 8217.1 | 8132.49 | 1036430134 | 568998.98 | 79363221 |
2024-07-10 | 39721.36 | 39272.45 | 39736.2 | 39256.72 | 336924793 | 39778.19 | 39263.72 | 39560.74 | 24602 | 8193.51 | 8139.81 | 8194.85 | 8138.18 | 677919948 | 454063.19 | 38202526 |
2024-07-11 | 39753.75 | 39695.18 | 39875.6 | 39623.12 | 363850306 | 39943.48 | 39723.73 | 39647.98 | 24602 | 8223.34 | 8193.51 | 8231.78 | 8190.81 | 581046858 | 325892.96 | 3302821 |
2024-07-12 | 40000.9 | 39783.28 | 40257.24 | 39783.28 | 329069849 | 40125.13 | 39868.46 | 39905.84 | 24602 | 8252.91 | 8223.34 | 8276.31 | 8223.34 | 515470753 | 289804.25 | 18772341 |
2024-07-15 | 40211.72 | 40138.4 | 40351.1 | 40136.1 | 322332993 | 40390.15 | 40215.07 | 40206.0 | 24602 | 8182.96 | 8252.91 | 8252.91 | 8174.62 | 547591903 | 325615.34 | 11344080 |
2024-07-16 | 40954.48 | 40263.78 | 40988.81 | 40263.78 | 313520180 | 41016.95 | 40382.0 | 40865.85 | 24602 | 8164.9 | 8182.96 | 8182.96 | 8126.07 | 565225672 | 320990.07 | 8612453 |
2024-07-17 | 41198.08 | 40862.57 | 41221.98 | 40849.71 | 438858895 | 41302.27 | 40851.99 | 41056.57 | 24602 | 8187.46 | 8164.9 | 8204.29 | 8132.02 | 746441568 | 369381.84 | 66961673 |
2024-07-18 | 40665.02 | 41156.56 | 41376.0 | 40597.5 | 398285177 | 40921.3 | 41052.43 | 40623.83 | 24602 | 8204.89 | 8187.46 | 8267.78 | 8187.46 | 541475871 | 301032.36 | 3288391 |
2024-07-19 | 40287.53 | 40592.35 | 40626.16 | 40203.27 | 354442967 | 40365.25 | 40628.36 | 40152.46 | 24602 | 8155.72 | 8150.09 | 8184.26 | 8135.08 | 664048898 | 416374.83 | 49615027 |
2024-07-22 | 40415.44 | 40414.49 | 40472.53 | 40222.77 | 358564742 | 40474.5 | 40381.74 | 40330.89 | 24602 | 8198.78 | 8155.72 | 8240.4 | 8155.72 | 772112000 | 399265.81 | 96832599 |
2024-07-23 | 40358.09 | 40443.73 | 40527.89 | 40319.7 | 296803005 | 40441.26 | 40423.32 | 40193.85 | 24602 | 8167.37 | 8198.78 | 8228.1 | 8151.46 | 871901942 | 540206.27 | 40133761 |
2024-07-24 | 39853.87 | 40210.63 | 40258.44 | 39807.45 | 400901853 | 40068.04 | 40167.37 | 39876.4 | 24602 | 8153.69 | 8167.37 | 8170.68 | 8112.03 | 596371593 | 348091.42 | 27879427 |
2024-07-25 | 39935.07 | 39828.63 | 40438.82 | 39817.5 | 382491163 | 40140.44 | 39866.81 | 39869.64 | 24602 | 8186.35 | 8153.69 | 8187.51 | 8056.01 | 975429859 | 503397.05 | 6069931 |
2024-07-26 | 40589.34 | 40140.86 | 40753.83 | 40140.86 | 334454548 | 40699.84 | 40169.67 | 40489.68 | 24602 | 8285.71 | 8186.35 | 8290.33 | 8181.23 | 658568207 | 356037.55 | 12924958 |
2024-07-29 | 40539.93 | 40665.71 | 40682.49 | 40388.83 | 277552406 | 40664.16 | 40651.68 | 40473.98 | 24602 | 8292.35 | 8285.71 | 8368.38 | 8285.71 | 779761321 | 414630.01 | 55128986 |
2024-07-30 | 40743.33 | 40622.13 | 40866.25 | 40529.1 | 358447974 | 40715.47 | 40581.65 | 40332.79 | 24602 | 8274.41 | 8292.35 | 8297.92 | 8235.55 | 683082740 | 373406.65 | 52305749 |
2024-07-31 | 40842.79 | 40768.88 | 41198.63 | 40655.89 | 420099927 | 41047.37 | 40720.6 | 40666.73 | 24602 | 8367.98 | 8274.41 | 8399.88 | 8274.41 | 838706258 | 509959.68 | 35380509 |
Show code cell content
rd.close_session()