[ quantlib-Bugs-3504746 ] bmaindex method previousWednesday

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ quantlib-Bugs-3504746 ] bmaindex method previousWednesday

SourceForge.net
Bugs item #3504746, was opened at 2012-03-14 16:23
Message generated for change (Tracker Item Submitted) made by h-freedman
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=112740&aid=3504746&group_id=12740

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Henry Freedman (h-freedman)
Assigned to: Nobody/Anonymous (nobody)
Summary: bmaindex method previousWednesday

Initial Comment:
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;
        }


I am porting the quantlib to Java, here is the method improved in Java, it maybe explains my point better
==================================================================================
    /**
     * 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.
    }


----------------------------------------------------------------------

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=112740&aid=3504746&group_id=12740

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev