How to get all target price of S&P500 listed firm in from 2000 to 2022?
Hi @pan.yiming ,
First create a list of date you want to retrieve the data
import pandas as pdimport timeimport refinitiv.data as rdrd.open_session()m = 'Dec'start_y = 2000end_y = 2022start = pd.to_datetime(f'{m}{start_y}', format='%b%Y')end = pd.to_datetime(f'{m}{end_y+1}', format='%b%Y')date_list = pd.date_range(start, end, freq='Y')date_list
Then from each date, use get_data function to retrieve the list of constituents on that date
for date in date_list: print(date) index_data = rd.get_data(f'0#.SPX({date.strftime("%Y%m%d")})',['TR.RIC']) display(index_data) time.sleep(1)
After you retrieve the constituents, depending on which format of the data you want, you can retrieve the target price data with get_data function.
Please let me know in case you have any further question
Is this what you're looking for?
here's to get the S&P500 constituents list as of 31 December 2022, you could loop through the dates that you want the data and put the date in the bracket right after the RIC name (replace the date 20221231 in the code below)
index_data = rd.get_data('0#.SPX(20221231)',['TR.RIC', 'TR.PriceTargetMedian','TR.PriceTargetMean','TR.PriceTargetNumIncEstimates'])index_data
However, there's an article Building historical index constituents, which explain the process of building historical constituents of a market index, underscoring its importance for investors, researchers, and various participants in the financial markets.
Or you could create a Python dictionary with the list of Constituents of each date, then use this dict for further data retrieval
for example,
constituents_dict = {}for date in date_list: print(date) index_data = rd.get_data(f'0#.SPX({date.strftime("%Y%m%d")})',['TR.RIC']) constituents_dict[date] = index_data['RIC'].to_list() time.sleep(1)
The Python dict will be in format of {Date: [list of Constituents], ...}
Thank you so much. Do you know how to get the Eikon data API key?
You're welcome.
The code I suggested is using Refinitiv Data Library for Python, which is the newestand strategic python library for retrieving data from the Data Platform. More detail can be found in article Summary of Common LSEG Refinitiv APIs
You may follow the quick start guide to start using the Data Library and the example code can be found here