In the file bmaindex.cpp, the method previousWednesday does not work correctly if the provided date is this week's Wednesday, then it return the same date, while it should return previous weeks's Wednesday. I might be wrong though.
Current Code
----------------------
Date previousWednesday(const Date& date) {
Weekday w = date.weekday();
if (w >= 4) // roll back w-4 days
return date - (w - 4) * Days;
else // roll forward 4-w days and back one week
return date + (4 - w - 7) * Days;
}
Improved Code
---------------------------
Date previousWednesday(const Date& date) {
Weekday w = date.weekday();
if (w > 4) // roll back w-4 days ////////// HERE IS THE FIX: > instead of >=
return date - (w - 4) * Days;
else // roll forward 4-w days and back one week
// I would suggest to take the absolute value of 4 - w - 7 and use the date - operator reflecting the rolling back.
return date + (4 - w - 7) * Days;
}
/**
* This method is useful for instruments or indexes fixed on Wednesdays, e.g. BMA index.
* @return Date corresponding to the current or previous week's Wed.
*/
public static Date previousWednesday(final Date date) {
final int weekDayNumber = date.weekday().value();
if (weekDayNumber > 4) // if Thur, Fri, Sat
return date.subAssign((weekDayNumber - 4)); // go back to this week Wed.
else // if Sun, Mon, Tue, Wed
return date.subAssign(Math.abs(4 - weekDayNumber - 7)); // go back to previous week's Wed.
}
I forgot to mention, that I moved in my Java code the methods previousWednesday and nextWednesday to the class Date and made them static, as the belong better there. to be used by other instruments or to be copied/generalised for other weekdays shall other instruments require that.
The behavior of the function is correct; if today is wednesday, we want today's date. The name of the function is wrong. It might be "lastWednesday" or something like this.