Programming Challenge

Quote from stock777:

lol, nice Wayne. There are complete black box trading systems with fewer lines of code than that.
Heh. :D

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;
 
Clearly works, but it is not elegant imo.

nitro
Quote from MGJ:

Look at a calendar for 2007. June 2007 shows you that the earliest possible date of a 3rd Friday, is the 15th of the month. December 2007 shows you that the latest possible date of a 3rd Friday, is the 21st of the month.

So the test is (DayOfWeek == FRIDAY) AND (15 <= DayOfMonth <= 21). Simple and straightforward.

Some people wish to CALCULATE the date of the 3rd Friday ratehr than merely testing whether a given day is, or is not, a 3rd Friday. TO do this simply, just write seven straightforward IF statements, viz,

IF(the first of the month falls on a Saturday) THEN date_of_3rd_Friday = 21;
IF(the first of the month falls on a Sunday) THEN date_of_3rd_Friday = 20;
IF(the first of the month falls on a Monday) THEN date_of_3rd_Friday = 19;
etc.
 
Quote from nitro:

...14 + (6 - 5) + 1 = 16th

Interestingly enough, you can turn this calculation to get the weekofmonth using a little modular arithmetic once you get it working...

nitro
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){
&nbsp;&nbsp;&nbsp;GregorianCalendar gc1 = new GregorianCalendar();
&nbsp;&nbsp;&nbsp;gc1.setTime(d);
&nbsp;&nbsp;&nbsp;if ( gc1.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.FRIDAY &&
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gc1.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH) == 3 ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;return false;

}

This would work with regular Calendar as well. I just happen to use GregorianCalendar a lot.
 
Quote from nitro:

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
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 :D
 
Quote from H2O:

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 :D

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

:cool: :cool:
 
Back
Top