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:
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:
0
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?
0 -
I'm afraid not. You can only find active equity issues using CUSIP/SEDOL/ISIN in Eikon.
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 651 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛