Fetching a company’s peers

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.
[Error 400] - {'error': 'access_denied', 'error_description': 'Account locked out due to provide incorrect password.'}
<refinitiv.data.session.Definition object at 0x7ff4fc1bc850 {name='rdp'}>

You can use the Refinitiv Data Library for Python to retrieve a list of the companies judged to be peers of a given company.

To do so, pass a company’s Refinitiv Instrument Code to the Peer component of the package’s discovery submodule. By default, the method only returns the code of each peer. You’ll need to pass the resulting list to another query to retrieve additional information about each company.

Here’s how to retrieve each of the Thomson Reuters’ peers:

rd.discovery.Peers("TRI.N")
<refinitiv.data.discovery._universe_expanders._peers.Peers at 0x7ff4f455c9a0>

You’ll need to convert it to a list object to see the results.

list(rd.discovery.Peers("TRI.N"))
Session is not opened. Can't send any request
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 list(rd.discovery.Peers("TRI.N"))

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/discovery/_universe_expanders/_universe_expander.py:15, in UniverseExpander.__next__(self)
     14 def __next__(self):
---> 15     if self._n < len(self._universe):
     16         result = self._universe[self._n]
     17         self._n += 1

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_tools/_common.py:216, in cached_property.__get__(self, instance, cls)
    215 def __get__(self, instance, cls=None):
--> 216     result = instance.__dict__[self.func.__name__] = self.func(instance)
    217     return result

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/discovery/_universe_expanders/_discovery_universe.py:51, in DiscoveryUniverse._universe(self)
     49 @cached_property
     50 def _universe(self):
---> 51     universe = get_universe(self._expression)
     52     if not universe:
     53         raise RDError(-1, default_error_message)

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/discovery/_universe_expanders/_discovery_universe.py:27, in get_universe(expression)
     25 session = get_default()
     26 logger = session.logger()
---> 27 adc_data = get_adc_data(
     28     params={
     29         "universe": expression,
     30         "fields": "TR.RIC",
     31     },
     32     logger=logger,
     33 )
     34 adc_raw = adc_data.raw
     35 return update_universe(
     36     adc_raw,
     37     None,
     38 )

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/content/_get_adc_data.py:28, in get_adc_data(params, logger)
     26 universe = params["universe"]
     27 logger.info(f"Requesting {fields} for {universe}")
---> 28 response = fundamental_and_reference.Definition(**params).get_data()
     29 DEBUG and logger.debug(f"ADC --->\n{response.data.df.to_string()}\n")
     31 request_messages = response.raw.request

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/content/fundamental_and_reference/_definition.py:269, in Definition.get_data(self, session, on_response)
    266         ticket = raw.get("ticket")
    268 else:
--> 269     response = super().get_data(session, on_response)
    271 return response

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/content/_content_provider_layer.py:58, in ContentUsageLoggerMixin.get_data(self, session, on_response)
     51 # Library usage logging
     52 get_usage_logger().log_func(
     53     name=f"{ModuleName.CONTENT}.{self._USAGE_CLS_NAME}.get_data",
     54     func_path=f"{self.__class__.__module__}.{self.__class__.__qualname__}.get_data",
     55     kwargs={**self._kwargs},
     56     desc={FilterType.SYNC, FilterType.LAYER_CONTENT},
     57 )
---> 58 return super().get_data(session, on_response)

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/delivery/_data/_data_provider_layer.py:149, in DataProviderLayer.get_data(self, session, on_response)
    133 """
    134 Sends a request to the client file store to retrieve the previously defined data.
    135 
   (...)
    146 
    147 """
    148 session = get_valid_session(session)
--> 149 response = get_data(self._data_type, self._provider, session, **self._kwargs)
    150 if on_response is not None:
    151     warnings.warn(
    152         "Avoid using on_response for get_data method, it will be removed in future library version v2.0.",
    153         category=FutureWarning,
    154     )

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/delivery/_data/_data_provider_layer.py:51, in get_data(data_type, provider, session, **kwargs)
     44 # Library usage logging
     45 get_usage_logger().log_func(
     46     name=f"{ModuleName.DELIVERY}.{DataProviderLayer._USAGE_CLS_NAME}.get_data",
     47     func_path=f"{DataProviderLayer.__module__}.{DataProviderLayer.__qualname__}.get_data",
     48     kwargs={"url": url, "auto_retry": auto_retry, **kwargs},
     49     desc={FilterType.SYNC, FilterType.LAYER_DELIVERY},
     50 )
---> 51 response = provider.get_data(session, url, auto_retry=auto_retry, **kwargs)
     52 return response

File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/delivery/_data/_data_provider.py:78, in DataProvider.get_data(self, session, *args, **kwargs)
     77 def get_data(self, session: "Session", *args, **kwargs) -> Response:
---> 78     raise_if_closed(session)
     79     request = self.request.create(session, *args, **kwargs)
     80     raw_response = self.connection.send(request, session, *args, **kwargs)

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
Hide code cell content
rd.close_session()