question

Upvotes
Accepted
19 4 6 9

How can I use Python's Datastream DSWS to collect data above the limit of 50 instruments?

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?

pythondatastream-apidsws-apiapi-limits
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Upvotes
Accepted
2.3k 16 2 2

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 ds
import pandas

# We need to split our string of instruments as a list
tickers_of_interest_lst = tickers_of_interest_str.split(",")

# Get 1st instrument's data
df = 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 lots
def 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.: 50
tickers_of_interest_chunks = chunks(lst = tickers_of_interest_lst[1:], n = 50)

# Now collect all data and append to df
for 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.T

display(df)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

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"