Can I use Datastream API (DSWS) in Python to collect constituent data such as weights in the past?
Hi @danieluphromes ,
Yes, to get the constituent list in the past, you can put
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 passwordds = DSWS.Datastream(username = '# DSWS USERNAME #', password = '# DSWS PASSWORD #') # call Datastream to get a list of RICsdat = 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 monthmmyy_list = ['0100','0200','0300','0521'] # call Datastream to get RICs listrics_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.