Heh. Maybe so, but a lot of that junk is for Julian Day calcs and to find Good Friday expirations (past and future) for accuracy. You need to convert to Julian to easily count the number of calendar days between dates, so it can be used in Black-Scholes crunching. And besides, WealthScript isn't exactly the most compact language... Here's a fun JD calculator: Code: Function DaysBetweenExp(Month1, Day1, Year1, Month2, Day2, Year2:integer):integer; begin var J1, J2:integer; Function JulianDay(Month, Day, Year:integer):integer; begin var I, J, K: integer; I := Year; J := Month; K := day; Result := K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4; end; J1 := JulianDay(Month1, Day1, Year1); J2 := JulianDay(Month2, Day2, Year2); Result := J2 - J1; end;
Notice that 14 = 0 (Mod 7), 6 = -1 (Mod 7) and -5 = 2 (Mod 7), 1 = 1 (Mod 7), so that 0 -1 + 2 + 1 = 2 (Mod 7), and that 16 = 2 (Mod 7) "third week of month" if you add 1 to the 2. If you get > 4 after adding 1 to the modular arithmetic, you know you are into the next month and must have been on this months fourth week. Now we begin to see why this sort of strategy may work, and a way to a general proof... nitro
Java its easy, use the Gregorian Calendar import java.util.Date; import java.util.GregorianCalendar; public static boolean isOptionsExp(Date d){ GregorianCalendar gc1 = new GregorianCalendar(); gc1.setTime(d); if ( gc1.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.FRIDAY && gc1.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH) == 3 ){ return true; } return false; } This would work with regular Calendar as well. I just happen to use GregorianCalendar a lot.
Notice that if you replace the 14 by a 7 (doesn't affect the calculation Mod 7 since both are 0 (Mod 7) ) you can turn this into a week of month calculation. Now you can say, Code: if(WeekOfMonth(DateTime) == 3 && DayOfWeek(DateTime) == Friday) {...} You know when you have done it right when you get extras for nothing from your solution. This is the best solution imo. nitro
nice challenge/solutions... how about we up the stakes a little bit: most efficient container solution for real-time order book. only if nitro says it's ok though,
Something like this would work in C, it returns the 3rd friday for any given month / year. Let me see about a Java equivalent: #define DEF_FRIDAY 5 #define DEF_DAYINAWEEK 7 int get_3rd_friday (int _mon, int _year) { struct tm cwtm; int yr, tmoffset; yr = _year - 1900; cwtm.tm_sec = 0 ; cwtm.tm_min = 0 ; cwtm.tm_hour = 1 ; cwtm.tm_mday = 1 ; cwtm.tm_mon = _mon ; cwtm.tm_year = yr; mktime (&cwtm); tmoffset = DEF_FRIDAY - cwtm.tm_wday; if (tmoffset < 0) tmoffset += DEF_DAYINAWEEK; tmoffset += 2 * DEF_DAYINAWEEK + 1; return tmoffset; }
I have this very simple formula in excel: =DATE(YEAR($C$1),MONTH($C$1),15)+CHOOSE(WEEKDAY(DATE(YEAR($C$1),MONTH($C$1),15)),3,2,1,0,6,5,4) - 2 Cell C1 contains the current front month (Mar-07 as we speak) It doesn't account for holidays on the specific Fri, but since it runs as a formula in a cell (no VBA needed) I thought this was worth noticing It uses the fact that the 3rd Friday is always at or after the 15th of the month It than adds a number of days to the date depending on what day the 15th is. Lol, after reading my own post I think it can be optimized even further... I see I subtract 2 for some reason and I guess that can be changed. So you see these things only if you look at something you've been using for ages
edit2: I now see why I did this: It can be done without subtracting the 2 days, but this was to make it more clear as this sheet was developed for LIFFE Euribor and the last trading day is: 10.00 - Two business days prior to the third Wednesday of the delivery month