I am trying to get the price for the next expiring future contract on a given date for Dow Jones and NASDAQ through python API? I am having a hard time figuring it out. Any help is appreciated. Thanks
@tmohann In Eikon we have instruments called continuation futures which are the front month future and takes into account futures rolls. Please look at the RICs : YMc1 (Mini Dow future) & NQc1 (NASDAQ Mini future). Following expiry futures are also available by replacing the 1 (ie 1st expiring future) with a 2 (ie 2nd expiring future) or a 3 (3rd expiring future). The great thing is you can get a series of such values:
ek.get_timeseries(['YMc1','NQc1'], start_date='01-01-2017', end_date='01-01-2019', interval='daily')
Jason - Thank you. This works great. Any chance you have how to get the number of days left to the expiry? For ex, number of trading days to YMc1? Appreciate your help! Thank you
There is a field EXPIR_DATE you can use to calc the number of days to expiry
future, err = ek.get_data('NQc1','EXPIR_DATE')
Thanks James. I am trying to get EXPIR_DATE on historical basis i.e. for each date, what the next expiry date for YMc1 over the course of the last 2 years. I am not able to get the historical data with get_data for EXPIR_DATE. Is that even possible? Thanks
We do store expired Futures data contracts .. by using a ^ after the RIC we do display the data and you can pull time series. That being said the historical Expiry Date field does not seem to be available yet in Eikon DAPI (although it is in the desktop GUI).
@tmohann
I'm not a python expert but was able to work through the calculation of days until expiry as follows (the simplification is at the end).
I used datetime to do the subtraction of dates between today and the expiration.
You won't see the output of the code here, except for the last one, but it works for me in my jupyter notebook, and would encourage you to try it out.
#Do the imports...import eikon as ekimport configparser as cpimport pandas as pdimport datetime as dtfrom datetime import datetime, date, time
#Get the app key setupcfg = cp.ConfigParser()cfg.read('eikon.cfg')#Eikon.cfg must be in the same folder as script here, and contains the keykey = cfg['eikon']['key']ek.set_app_key(key)
#Get the Eikon data - in this case a future expiry datefuture, err = ek.get_data('NQc1','EXPIR_DATE')future
#The above puts out a pandas dataframe but we want a string so we can parse it into a datetime formatstringExpDate = future["EXPIR_DATE"][0]stringExpDate
dateTimeExpDate = datetime.strptime(stringExpDate, "%Y-%m-%d")dateTimeExpDate
dateExpDate = dateTimeExpDate.date() # convert exDate to date from datetimedateExpDate
todayDate = dt.date.today() #Get today's datetodayDate
#Now a simple subtraction is supported with datetime
difference = dateExpDate - todayDatedifference
difference.days # use the days attribute to get the days from this timedetla datatype...
#Here is the simplification
future, err = ek.get_data('NQc1','EXPIR_DATE') # get the Eikon data, in this case an expiration date for an optiondays = (datetime.strptime(future["EXPIR_DATE"][0], "%Y-%m-%d").date() - dt.date.today()).days #get the number of days until it expirationdays
10
Thanks Jon. That looks great. I needed the historical pricing and the date to expiry over time. Looks like data API doesn't provide EXPIR_DATE for prior contracts. For now, I ended up building a hack to project expiry date based on the contract pricing date and the next 3rd Friday of the quarter. That seems to work. Thanks for all the help!
That is a good workaround. Just keep in mind that each exchange and contract have different expiry rules (am assuming already factored into your analysis). If you ever need to check you can reference contract trading details by exchange/instrument.