# How to get the IMM dates in Python for a given symbol?

How can I get an IMM date from its 2 character symbol (eg. "U6", "Z8") in python?

I have not been able to install the quantlib addin (it's hard if you aren't tech savy), so please don't point me in that direction..

This does the trick. Given "U8", function will get the IMM date for Sep 2018. Given "U4", function gets the IMM date for Sep 2024 (or it will until we've passed that date). Could be easily modified to also pull historical IMMs from a given longer symbol eg. "U2014".

def getIMMDate(IMMcode):

'''Takes 2 digit IMM code and returns effective date as datetime object'''
from datetime import datetime, timedelta

monthcodes = [[1, 'F'], [2, 'G'], [3, 'H'], [4, 'J'], [5, 'K'], [6, 'M'], [7, 'N'], [8, 'Q'],
[9, 'U'], [10, 'V'], [11, 'X'], [12, 'Z']]
month = [pair for pair in monthcodes if pair == IMMcode.upper()]

year_prefix = str(datetime.now().year)[:3]
year = int(year_prefix + IMMcode)

the_date = datetime(year, month, 1)
temp = the_date.replace(day=1)
nth_week = 3
week_day = 2
adj = (week_day - temp.weekday()) % 7
temp += timedelta(weeks=nth_week - 1)

if temp < datetime.now():
year_prefix = str(int(str(datetime.now().year)[:3]) + 1)
year = int(year_prefix + IMMcode)

the_date = datetime(year, month, 1)
temp = the_date.replace(day=1)
nth_week = 3
week_day = 2
adj = (week_day - temp.weekday()) % 7