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 0x7fa514128c40 {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-08-01 14:03:00 2461.89 2458.75 2459.625 2460.75 250 2462.3 2459.26 2460.125 2461.16 250 2462.095 2459.005 2459.875 2460.955

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-07-04 2362.44 2350.59 2356.37 2356.12 2353.32 2356.62 2354.07 2350.59 2362.44 2350.94 2362.83
2024-07-05 2392.84 2353.57 2391.7683 2391.4563 2357.26 2392.0803 2358.76 2353.57 2392.84 2354.5 2393.24
2024-07-08 2391.4226 2350.95 2359.15 2358.7983 2391.27 2359.5017 2392.27 2350.95 2391.4226 2351.37 2392.27
2024-07-09 2371.24 2349.39 2363.9401 2363.5845 2358.23 2364.2957 2358.98 2349.39 2371.24 2349.8 2371.8
2024-07-10 2386.53 2362.35 2371.4249 2371.071 2363.43 2371.7788 2364.18 2362.35 2386.53 2363.1 2386.96
2024-07-11 2424.44 2370.59 2415.13 2414.7786 2371.36 2415.4814 2371.81 2370.59 2424.44 2371.08 2424.8
2024-07-12 2418.19 2391.35 2411.6549 2411.2744 2416.09 2412.0354 2416.84 2391.35 2418.19 2391.7 2418.55
2024-07-15 2439.59 2401.17 2422.4151 2422.074 2409.25 2422.7561 2409.76 2401.17 2439.59 2401.53 2439.89
2024-07-16 2469.59 2423.39 2468.945 2468.573 2424.55 2469.3169 2425.06 2423.39 2469.59 2423.7 2470.23
2024-07-17 2483.6 2451.39 2458.7602 2458.3838 2475.9426 2459.1365 2476.4893 2451.39 2483.6 2451.81 2483.9658
2024-07-18 2474.82 2440.21 2445.34 2444.9688 2459.875 2445.7112 2460.375 2440.21 2474.82 2440.6 2475.23
2024-07-19 2445.56 2393.75 2401.195 2400.79 2444.9924 2401.6 2445.6975 2393.69 2445.59 2394.14 2447.0
2024-07-22 2412.09 2383.79 2398.0101 2397.6475 2399.9099 2398.3726 2400.9099 2383.79 2412.09 2384.2 2412.5493
2024-07-23 2411.97 2388.05 2409.565 2409.2073 2396.58 2409.9226 2397.03 2388.05 2411.97 2388.48 2412.3293
2024-07-24 2431.89 2395.99 2397.945 2397.5859 2408.49 2398.304 2409.39 2395.99 2431.89 2397.0 2432.2146
2024-07-25 2401.09 2352.99 2364.87 2364.4995 2398.11 2365.2405 2398.86 2352.99 2401.09 2353.39 2401.5598
2024-07-26 2390.61 2355.69 2386.825 2385.575 2363.85 2388.075 2364.75 2355.69 2390.61 2356.08 2391.04
2024-07-29 2402.89 2369.59 2383.9149 2383.5371 2386.07 2384.2927 2388.32 2369.59 2402.89 2369.95 2403.56
2024-07-30 2412.7 2376.35 2408.655 2408.43 2383.43 2408.88 2383.88 2376.35 2412.7 2376.68 2413.3
2024-07-31 2450.79 2403.7468 2448.455 2448.0989 2409.87 2448.811 2410.32 2403.7468 2450.79 2403.98 2451.2112

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-07-04 2362.44 2350.59 2356.37 2356.12 2353.32 2356.62 2354.07 2350.59 2362.44 2350.94 ... 30.19 30.41 30.4 30.4723 30.42 30.4973 30.19 30.5833 30.2103 30.6035
2024-07-05 2392.84 2353.57 2391.7683 2391.4563 2357.26 2392.0803 2358.76 2353.57 2392.84 2354.5 ... 30.1342 31.2254 31.2129 30.37 31.2379 30.42 30.1342 31.4843 30.2 31.51
2024-07-08 2391.4226 2350.95 2359.15 2358.7983 2391.27 2359.5017 2392.27 2350.95 2391.4226 2351.37 ... 30.42 30.815 30.79 31.15 30.84 31.2 30.42 31.3678 30.4377 31.3831
2024-07-09 2371.24 2349.39 2363.9401 2363.5845 2358.23 2364.2957 2358.98 2349.39 2371.24 2349.8 ... 30.5 30.8025 30.79 30.73 30.815 30.755 30.5 31.1479 30.52 31.1746
2024-07-10 2386.53 2362.35 2371.4249 2371.071 2363.43 2371.7788 2364.18 2362.35 2386.53 2363.1 ... 30.63 30.8155 30.808 30.7765 30.823 30.8015 30.63 31.1621 30.6574 31.19
2024-07-11 2424.44 2370.59 2415.13 2414.7786 2371.36 2415.4814 2371.81 2370.59 2424.44 2371.08 ... 30.7855 31.46 31.435 30.7855 31.485 30.8005 30.7855 31.751 30.8005 31.7625
2024-07-12 2418.19 2391.35 2411.6549 2411.2744 2416.09 2412.0354 2416.84 2391.35 2418.19 2391.7 ... 30.36 30.795 30.77 31.4545 30.82 31.4795 30.36 31.4545 30.3928 31.48
2024-07-15 2439.59 2401.17 2422.4151 2422.074 2409.25 2422.7561 2409.76 2401.17 2439.59 2401.53 ... 30.5063 31.035 31.01 30.8035 31.06 30.8285 30.5063 31.0875 30.5326 31.13
2024-07-16 2469.59 2423.39 2468.945 2468.573 2424.55 2469.3169 2425.06 2423.39 2469.59 2423.7 ... 30.4663 31.405 31.38 30.6695 31.43 30.6945 30.4663 31.4 30.4942 31.44
2024-07-17 2483.6 2451.39 2458.7602 2458.3838 2475.9426 2459.1365 2476.4893 2451.39 2483.6 2451.81 ... 30.03 30.301 30.2935 31.3838 30.3085 31.4238 30.03 31.42 30.0575 31.47
2024-07-18 2474.82 2440.21 2445.34 2444.9688 2459.875 2445.7112 2460.375 2440.21 2474.82 2440.6 ... 29.72 30.095 30.7 30.2935 30.12 30.3085 29.72 30.61 29.7497 30.66
2024-07-19 2445.56 2393.75 2401.195 2400.79 2444.9924 2401.6 2445.6975 2393.69 2445.59 2394.14 ... 28.85 29.285 29.26 29.69 29.31 29.82 28.85 29.87 28.8799 29.92
2024-07-22 2412.09 2383.79 2398.0101 2397.6475 2399.9099 2398.3726 2400.9099 2383.79 2412.09 2384.2 ... 28.72 29.225 29.2 29.18 29.25 29.26 28.72 29.428 28.7542 29.47
2024-07-23 2411.97 2388.05 2409.565 2409.2073 2396.58 2409.9226 2397.03 2388.05 2411.97 2388.48 ... 28.6487 29.245 29.22 29.19 29.27 29.26 28.6487 29.32 28.6701 29.37
2024-07-24 2431.89 2395.99 2397.945 2397.5859 2408.49 2398.304 2409.39 2395.99 2431.89 2397.0 ... 28.87 29.015 28.99 29.2122 29.04 29.2622 28.87 29.4482 28.8992 29.5
2024-07-25 2401.09 2352.99 2364.87 2364.4995 2398.11 2365.2405 2398.86 2352.99 2401.09 2353.39 ... 27.3809 27.9888 27.9688 28.8897 28.0087 28.9147 27.3809 28.94 27.4329 28.99
2024-07-26 2390.61 2355.69 2386.825 2385.575 2363.85 2388.075 2364.75 2355.69 2390.61 2356.08 ... 27.565 27.915 27.89 27.83 27.94 27.86 27.565 28.0682 27.5943 28.11
2024-07-29 2402.89 2369.59 2383.9149 2383.5371 2386.07 2384.2927 2388.32 2369.59 2402.89 2369.95 ... 27.3 27.885 27.86 27.8835 27.91 27.9585 27.3 28.1909 27.32 28.24
2024-07-30 2412.7 2376.35 2408.655 2408.43 2383.43 2408.88 2383.88 2376.35 2412.7 2376.68 ... 27.61 28.415 28.39 27.8843 28.44 27.8993 27.61 28.4104 27.6317 28.46
2024-07-31 2450.79 2403.7468 2448.455 2448.0989 2409.87 2448.811 2410.32 2403.7468 2450.79 2403.98 ... 28.22 29.065 29.04 28.3813 29.09 28.3963 28.22 29.04 28.2495 29.09

20 rows × 22 columns

Hide code cell content
rd.close_session()