Can I use Datastream API (DSWS) to collect historical constituent data?

Can I use Datastream API (DSWS) in Python to collect constituent data such as weights in the past?

Best Answer

  • raksina.samasiri
    Answer ✓

    Hi @danieluphromes ,

    Yes, to get the constituent list in the past, you can put

    • the date in format 'mmyy' after the symbol
    • then follow them with '|L' to get the list of constituents

    For example, to get the list of S&P 500 constituents on Jan 2021, tickers = 'LS&PCOMP0121|L' can be used.

    here's the Python code

    import DatastreamDSWS as DSWS
     
    # set datastream username and password
    ds = DSWS.Datastream(username = '# DSWS USERNAME #', password = '# DSWS PASSWORD #')
     
    # call Datastream to get a list of RICs
    dat = ds.get_data(tickers='LS&PCOMP0121|L', fields=['RIC'], kind=0)
    print(dat)

    and here's an example, in case you would like to get the historical data of multiple months

    # crate list of months and years to fetch a list of RICs on each month
    mmyy_list = ['0100','0200','0300','0521']
     
    # call Datastream to get RICs list
    rics_list = {}
    for mmyy in mmyy_list:
    dat = ds.get_data(tickers=f'LS&PCOMP{mmyy}|L', fields=['RIC'], kind=0)
    # save rics list in python dict
        rics_list[mmyy] = dat['Value'].tolist()

    Hope this could help.