Can I use Datastream API (DSWS) in Python to collect constituent data such as weights in the past?
Can I use Datastream API (DSWS) in Python to collect constituent data such as weights in the past?
Hi,
Thank you for your participation in the forum.
Is the reply below satisfactory in answering your question?
If yes please click the 'Accept' text next to the reply. This will guide all community members who have a similar question.
Otherwise please post again offering further insight into your question.
Thanks,
AHS
Please be informed that a reply has been verified as correct in answering the question, and has been marked as such.
Thanks,
AHS
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 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.