Fetching commodity 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 0x7fca34808fd0 {name='rdp'}>

You can use the Refinitiv Data Library for Python to retrieve the latest price of a commodity by passing its Refinitiv Instrument Code to the get_data function.

Adding an equals sign as a suffix will return the value in US dollars. Here’s the spot price of gold:

rd.get_data("XAU=")

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(
    "XAU=",
    interval="1min",
).tail(1)
XAU= BID_HIGH_1 BID_LOW_1 OPEN_BID BID 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 15:03:00 2333.79 2333.15 2333.1819 2333.395 54 2334.2 2333.7 2333.8157 2333.895 54 2333.995 2333.45 2333.4988 2333.645

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('XAU=')
XAU= HIGH_1 LOW_1 MID_PRICE BID OPEN_BID ASK OPEN_ASK BID_LOW_1 BID_HIGH_1 ASK_LOW_1 ASK_HIGH_1
Date
2024-03-28 2234.99 2187.0627 2232.9025 2232.3757 2193.7251 2233.4292 2194.655 2187.0627 2234.99 2187.6 2236.8
2024-04-01 2265.49 2228.29 2250.8825 2250.3557 2238.59 2251.4092 2240.6 2228.29 2265.49 2228.8 2266.2209
2024-04-02 2280.89 2246.4722 2280.5601 2280.0957 2250.0801 2281.0244 2251.6799 2246.4722 2280.89 2247.1 2281.4475
2024-04-03 2300.89 2265.1 2299.6226 2299.1702 2279.1106 2300.075 2280.0393 2265.1 2300.89 2265.9 2301.5515
2024-04-04 2305.04 2279.89 2289.8799 2289.4287 2298.8201 2290.3311 2300.4199 2279.89 2305.04 2280.4 2305.5779
2024-04-05 2330.06 2267.6033 2329.755 2329.5 2289.0801 2330.01 2290.6799 2267.6033 2330.06 2268.1 2330.6201
2024-04-08 2353.79 2302.5149 2339.2426 2338.8909 2321.19 2339.5942 2322.8 2302.5149 2353.79 2303.2 2354.4
2024-04-09 2365.09 2336.5793 2352.9125 2352.5767 2338.74 2353.2483 2339.74 2336.5793 2365.09 2337.3 2365.6
2024-04-10 2359.89 2319.29 2333.1725 2332.7944 2352.4099 2333.5505 2353.4099 2319.29 2359.89 2319.8 2360.4084
2024-04-11 2377.59 2325.59 2373.5924 2373.2378 2332.6699 2373.947 2333.6699 2325.59 2377.59 2326.1 2378.0046
2024-04-12 2431.29 2333.69 2343.855 2343.4255 2373.0901 2344.2844 2374.0901 2333.69 2431.29 2334.1 2431.7771
2024-04-15 2387.39 2324.29 2382.8624 2382.5078 2343.3601 2383.217 2344.3601 2324.29 2387.39 2324.9 2387.8
2024-04-16 2398.09 2362.6 2383.2025 2382.8318 2382.3601 2383.5732 2383.3601 2362.6 2398.09 2363.3 2398.6
2024-04-17 2395.39 2354.29 2361.1326 2360.8057 2382.7 2361.4595 2383.7 2354.29 2395.39 2355.4 2395.86
2024-04-18 2392.69 2360.3674 2378.595 2378.2507 2360.4097 2378.9392 2361.0703 2360.3674 2392.69 2361.0251 2393.0
2024-04-19 2417.59 2372.8125 2391.7017 2390.4517 2378.1001 2392.9517 2379.1001 2372.8125 2417.59 2373.1 2418.0
2024-04-22 2388.1348 2325.09 2326.6225 2326.2859 2387.59 2326.959 2388.8 2325.09 2388.1348 2325.5 2389.3
2024-04-23 2334.29 2291.19 2322.1926 2321.8145 2326.1201 2322.5706 2327.1201 2291.19 2334.29 2291.6 2334.6748
2024-04-24 2336.9 2311.69 2316.2 2315.822 2321.6899 2316.5779 2322.6899 2311.69 2336.9 2312.1 2337.41
2024-04-25 2344.59 2304.9685 2332.1601 2331.782 2315.7 2332.5381 2316.7 2304.9685 2344.59 2305.6 2345.136

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(['XAU=', 'XAG='])
XAU= ... XAG=
HIGH_1 LOW_1 MID_PRICE BID OPEN_BID ASK OPEN_ASK BID_LOW_1 BID_HIGH_1 ASK_LOW_1 ... LOW_1 MID_PRICE BID OPEN_BID ASK OPEN_ASK BID_LOW_1 BID_HIGH_1 ASK_LOW_1 ASK_HIGH_1
Date
2024-03-28 2234.99 2187.0627 2232.9025 2232.3757 2193.7251 2233.4292 2194.655 2187.0627 2234.99 2187.6 ... 24.3691 24.9769 24.9738 24.6244 24.98 24.6544 24.3691 25.0081 24.3939 25.02
2024-04-01 2265.49 2228.29 2250.8825 2250.3557 2238.59 2251.4092 2240.6 2228.29 2265.49 2228.8 ... 24.73 25.11 25.085 24.98 25.135 25.02 24.73 25.3821 24.7492 25.4007
2024-04-02 2280.89 2246.4722 2280.5601 2280.0957 2250.0801 2281.0244 2251.6799 2246.4722 2280.89 2247.1 ... 25.0551 26.135 26.11 25.11 26.16 25.14 25.0551 26.1693 25.0821 26.1924
2024-04-03 2300.89 2265.1 2299.6226 2299.1702 2279.1106 2300.075 2280.0393 2265.1 2300.89 2265.9 ... 26.09 27.24 27.215 26.1351 27.265 26.1601 26.09 27.215 26.1173 27.265
2024-04-04 2305.04 2279.89 2289.8799 2289.4287 2298.8201 2290.3311 2300.4199 2279.89 2305.04 2280.4 ... 26.67 26.965 26.94 27.1625 26.99 27.1875 26.67 27.3283 26.6918 27.3407
2024-04-05 2330.06 2267.6033 2329.755 2329.5 2289.0801 2330.01 2290.6799 2267.6033 2330.06 2268.1 ... 26.26 27.505 27.48 26.9389 27.53 26.9639 26.26 27.493 26.2885 27.53
2024-04-08 2353.79 2302.5149 2339.2426 2338.8909 2321.19 2339.5942 2322.8 2302.5149 2353.79 2303.2 ... 26.84 27.8522 27.8372 27.465 27.8672 27.49 26.84 28.0834 26.8693 28.1
2024-04-09 2365.09 2336.5793 2352.9125 2352.5767 2338.74 2353.2483 2339.74 2336.5793 2365.09 2337.3 ... 27.6 28.1621 28.1593 27.8372 28.1649 27.8672 27.6 28.3203 27.6282 28.34
2024-04-10 2359.89 2319.29 2333.1725 2332.7944 2352.4099 2333.5505 2353.4099 2319.29 2359.89 2319.8 ... 27.52 27.995 27.97 28.1185 28.02 28.1485 27.52 28.52 27.54 28.54
2024-04-11 2377.59 2325.59 2373.5924 2373.2378 2332.6699 2373.947 2333.6699 2325.59 2377.59 2326.1 ... 27.74 28.495 28.47 27.925 28.52 27.955 27.74 28.4963 27.76 28.5233
2024-04-12 2431.29 2333.69 2343.855 2343.4255 2373.0901 2344.2844 2374.0901 2333.69 2431.29 2334.1 ... 27.8119 27.9 27.875 28.4022 27.925 28.4322 27.8119 29.7908 27.8919 29.82
2024-04-15 2387.39 2324.29 2382.8624 2382.5078 2343.3601 2383.217 2344.3601 2324.29 2387.39 2324.9 ... 27.56 28.91 28.885 28.0983 28.935 28.1233 27.56 28.8904 27.5963 28.935
2024-04-16 2398.09 2362.6 2383.2025 2382.8318 2382.3601 2383.5732 2383.3601 2362.6 2398.09 2363.3 ... 27.94 28.0975 28.085 28.8515 28.11 28.8765 27.94 29.0162 27.9581 29.04
2024-04-17 2395.39 2354.29 2361.1326 2360.8057 2382.7 2361.4595 2383.7 2354.29 2395.39 2355.4 ... 28.05 28.24 28.215 28.098 28.265 28.123 28.05 28.8 28.0758 28.821
2024-04-18 2392.69 2360.3674 2378.595 2378.2507 2360.4097 2378.9392 2361.0703 2360.3674 2392.69 2361.0251 ... 28.1149 28.2355 28.2205 28.1825 28.2505 28.2075 28.1149 28.6533 28.1426 28.664
2024-04-19 2417.59 2372.8125 2391.7017 2390.4517 2378.1001 2392.9517 2379.1001 2372.8125 2417.59 2373.1 ... 28.05 28.6783 28.6533 28.2205 28.7033 28.2505 28.05 28.94 28.0728 28.96
2024-04-22 2388.1348 2325.09 2326.6225 2326.2859 2387.59 2326.959 2388.8 2325.09 2388.1348 2325.5 ... 27.09 27.215 27.19 28.68 27.24 28.78 27.09 28.7 27.1184 28.78
2024-04-23 2334.29 2291.19 2322.1926 2321.8145 2326.1201 2322.5706 2327.1201 2291.19 2334.29 2291.6 ... 26.65 27.305 27.28 27.2147 27.33 27.2447 26.65 27.38 26.6724 27.4
2024-04-24 2336.9 2311.69 2316.2 2315.822 2321.6899 2316.5779 2322.6899 2311.69 2336.9 2312.1 ... 27.02 27.2 27.175 27.2835 27.225 27.3135 27.02 27.4834 27.04 27.509
2024-04-25 2344.59 2304.9685 2332.1601 2331.782 2315.7 2332.5381 2316.7 2304.9685 2344.59 2305.6 ... 26.9598 27.4369 27.4346 27.148 27.4392 27.178 26.9598 27.5736 26.9883 27.5822

20 rows × 22 columns

Hide code cell content
rd.close_session()