Fetching commodity 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.
[Error 400] - {'error': 'access_denied', 'error_description': 'Account locked out due to provide incorrect password.'}
<refinitiv.data.session.Definition object at 0x7f8ea04c1570 {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)
Session is not opened. Can't send any request
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 1
----> 1 rd.get_history(
2 "XAU=",
3 interval="1min",
4 ).tail(1)
File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_access_layer/get_history_func.py:96, in get_history(universe, fields, interval, start, end, adjustments, count, use_field_names_in_headers, parameters)
90 warnings.warn(
91 "Parameter 'use_field_names_in_headers' is deprecated and will be removed in future library version v2.0.",
92 FutureWarning,
93 )
95 session = get_default()
---> 96 raise_if_closed(session)
98 logger = session.logger()
100 if interval is not None and interval not in INTERVALS:
File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_core/session/tools.py:37, in raise_if_closed(session)
35 error_message = "Session is not opened. Can't send any request"
36 session.error(error_message)
---> 37 raise ValueError(error_message)
ValueError: Session is not opened. Can't send any request
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=')
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='])
Show code cell content
rd.close_session()