target price of S&P500 listed firm

How to get all target price of S&P500 listed firm in from 2000 to 2022?

Best Answer

  • raksina.samasiri
    Answer ✓

    Hi @pan.yiming ,

    First create a list of date you want to retrieve the data

    import pandas as pd
    import time
    import refinitiv.data as rd
    rd.open_session()

    m = 'Dec'
    start_y = 2000
    end_y = 2022

    start = 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

    1715588715499.png

    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)

    1715588728608.png

    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


Answers