How to recreate DS.get_epit_vintage_matrix('USCONPRCE', date_from='1950-01-01') in DSWS library?

Hi,

I wish to create a vintage matrix as shown below for Economic Point in Time data. I created this using the 3rd party "pydatastream" library, with the following function:

DS.get_epit_vintage_matrix('USCONPRCE', date_from='1950-01-01').

I was advised to use the supported DSWS library instead to achieve this goal, however I am unable to do so with the limited documentation available

image

This vintage matrix contains all releases for this instrument, as well as all dates that this data was revised.

Could somebody please show me how to create this using the DSWS library?

Thanks!

Best Answer

  • Jirapongse
    Jirapongse ✭✭✭✭✭
    Answer ✓

    @pvilleneuve

    Refer to the source code of pydatastream on GitHub, DS.get_epit_vintage_matrix uses a lot of requests to generate the data. You can translate the code that calls self.fetch to the get_data method of DatastreamDSWS.

    For example, the code for get_epit_vintage_matrix looks like:

    def get_epit_vintage_matrix(mnemonic, date_from='1951-01-01', date_to=None):
        rel1 = ds.get_data (tickers=mnemonic,fields= ['REL1'], start=date_from, end=date_to, kind=1)
        date_0 = rel1.dropna().index[0]
        reld123 = ds.get_data(tickers=mnemonic, fields= ['RELD1', 'RELD2', 'RELD3'],
                              start=date_0, end=date_to, kind=1).dropna(how='all')
        res = {}
        for date in reld123.index:
            try:
                _tmp = ds.get_data(tickers=mnemonic, fields= ['RELV'], 
                                   start=date_0, end=date, kind=1).dropna()    
            except DatastreamException:
                continue
            res[date] = _tmp[mnemonic]
        return pd.concat(res).RELV.unstack()

    ds represents the DSWS.Datastream.

Answers

  • Thanks, this worked. Just had to remove "DatastreamException" and fix one "mnuemonic" typo and it ran.