Terry Reedy wrote: >>Laguna wrote: >> >>>I want to find the expiration date of stock options (3rd Friday of the >>>month) for an any give month and year. > > >>From year and month (and day=1) get the day of the week (n in [0,6]) of the > first of the month using some version of the the standard formula (see > below) and look up the third friday date in a precalculated 7-element list, > or, with n=0 on Saturday, 3rd Friday is 21-n > > Here is a translation of the guts of a 30-year-old Basic program: > > def friday3(m,y): # ints > if m <= 2: > m += 12 > y -= 1 > d = 1 > n = d + 2*m + int(.6*(m+1)) + y + y//4 - y//100 + y//400 + 2
Some simplification is possible: >>> [2*m + int(.6*(m+1)) for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37] >>> [(13*m+3)//5 for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37] >>> > n = int((n/7.0- n//7)*7.0 + .5) n %= 7 > # n=0 is Saturday, making 3rd Friday the 21st. > return 21 - n > > >>>Requirements: >>>d0 = expiration(9, 2005) # d0 would be 16 >>>d1 = expiration(6, 2003) # d1 would be 20 >>>d2 = expiration(2, 2006) # d2 would be 17 > > >>>>for m,y in ((9,2005), (6,2003), (2,2006)): print friday3(m,y) > > ... > 16 > 20 > 17 > > Terry J. Reedy > > > -- http://mail.python.org/mailman/listinfo/python-list