I use Python's DSWS a lot, and often need to collect few data points for many instruments (more than 50). How can I use Python to loop through instruments and get results in one Pandas data-frame?
Hi @danieluphromes,
For - as an e.g. - a string 'tickers_of_interest_str' (see comment) from 2020-01-01 to 2020-03-01 and the default field ('X'), you can use the below:
# We need pandas in addition to DSWS as dsimport pandas# We need to split our string of instruments as a listtickers_of_interest_lst = tickers_of_interest_str.split(",")# Get 1st instrument's datadf = ds.get_data(tickers = tickers_of_interest_lst[0], fields = ['X'], start = '2020-01-01', end = '2020-03-01').T# Define a function to get chunks of 50 instrument lotsdef chunks(lst, n): """Yield successive n-sized chunks from lst.""" _lst = [] for i in range(0, len(lst), n): _lst.append(lst[i:i + n]) return _lst# Use the pre-defined function up to the limit of DSWS - i.e.: 50tickers_of_interest_chunks = chunks(lst = tickers_of_interest_lst[1:], n = 50)# Now collect all data and append to dffor i in tickers_of_interest_chunks: _df = ds.get_data(tickers = ','.join(i), fields = ['X'], start = '2020-01-01', end = '2020-03-01').T df = df.append(_df)df = df.Tdisplay(df)
tickers_of_interest_str = "US01Y00,US02Y00,US03Y00,US04Y00,US05Y00,US06Y00,US07Y00,US08Y00,US09Y00,US10Y00,US11Y00,US12Y00,US13Y00,US14Y00,US15Y00,US16Y00,US17Y00,US18Y00,US19Y00,US20Y00,US21Y00,US22Y00,US23Y00,US24Y00,US25Y00,US26Y00,US27Y00,US28Y00,US29Y00,US30Y00,US31Y00,US32Y00,US33Y00,US34Y00,US35Y00,US36Y00,US37Y00,US38Y00,US39Y00,US40Y00,US41Y00,US42Y00,US43Y00,US44Y00,US45Y00,US46Y00,US47Y00,US48Y00,US49Y00,US50Y00,UK01Y00,UK02Y00,UK03Y00,UK04Y00,UK05Y00,UK06Y00,UK07Y00,UK08Y00,UK09Y00,UK10Y00,UK11Y00,UK12Y00"