Python: getting dates for all tickers/symbols, even for dates in which data is not available.

I have the following function in Python (snippet 1):

´Snippet 1:

import eikon as ek #import Eikon module
import numpy as np

def get_data(identifiers,tr_fields,settings,eikon_id):
ek.set_app_id(eikon_id)
df = ek.get_data(identifiers, tr_fields,settings)[0]
df.columns = df.columns.str.replace(r"[ ]", "_")
df.columns = df.columns.str.replace(r"[-]", "_")
df = df.fillna(np.nan)
return df

If I use it with these parameters (snippet 2), I get the following outcome (image 1):

Snippet 2:

tr_fields=['TR.TotalReturn.calcDate', 'TR.TotalReturn']
sets={'SDate': '03/27/2017', 'EDate': '03/31/2017', 'Frq': 'D'}
id='XXXX'
instruments=['BP8Y3X2','6071475']
a=get_data(instruments,tr_fields,sets,id)

Image 1:

image

My guess is that Eikon has some sort of default setting that avoids populating a Dataframe with missing data. But, what if I wanted to have that missing data? For this particular case, I'd like to have all five dates for BP8Y3X2 in the Dataframe, obviously with NaN values in their respective Calc_Date and Total_Return columns.

Is something like this possible?

Thanks in advance for your help.

Best Answer

  • The reason why it does not return is that BP8Y3X2 does not exist as an instrument. If you check the error part of the get_date() tuple, you will see something like:

    [{'code': 416,
    'col': 1,
    'message': "Unable to collect data for the field 'TR.TOTALRETURN.calcDate' and some specific identifier(s).",
    'row': 0},
    {'code': 416,
    'col': 2,
    'message': "Unable to collect data for the field 'TR.TOTALRETURN' and some specific identifier(s).",
    'row': 0}]

    On to the second part of the question. Requests like this will indeed yield the datapoints that exist in the database, rather than mapping them to some time range. With pandas you can actually do it yourself, which is pretty straight forward.

    Do the request:

    response, error = tr.get_data(instruments=['GAZP.MM','6071475'], fields=['TR.TotalReturn.calcDate', 'TR.TotalReturn'], parameters={'SDate': '0', 'EDate': '-20', 'Frq': 'D'}) 


    Rebuild dataframe, so that the date is the index:

    df = response.pivot_table(values='Total Return', index=['Calc Date', 'Instrument']).unstack('Instrument') 

    Convert index format to date:

    df.index = pd.to_datetime(df.index) 

    Reindex with the new date range:

    df.reindex(pd.date_range(start='2017-12-01', end='2018-01-18', freq=pd.offsets.BDay()))

    The result will look like this:

    image

Answers

  • Thanks a lot for your solution :)

    Is there any way to index an instrument that ceased to exist or is no longer listed? Because for this particular case, BP8Y3X2 was an instrument for a certain company (can't remember now which one), but I guess the company used another SEDOL for it. Is there a way I can index this into the Dataframe as well?

  • I'm afraid not. You can only find active equity issues using CUSIP/SEDOL/ISIN in Eikon.