Logo Search packages:      
Sourcecode: quantlib version File versions

calendar.hpp

Go to the documentation of this file.
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
 Copyright (C) 2006 Piter Dias

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/reference/license.html>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file calendar.hpp
    \brief %calendar class
*/

#ifndef quantlib_calendar_hpp
#define quantlib_calendar_hpp

#include <ql/date.hpp>
#include <ql/Patterns/bridge.hpp>
#include <set>
#include <vector>

namespace QuantLib {

    //! Business Day conventions
    /*! These conventions specify the algorithm used to adjust a date in case
        it is not a valid business day.

        \ingroup datetime
    */
    enum BusinessDayConvention {
        // ISDA
        Following,          /*!< Choose the first business day after
                                 the given holiday. */
        ModifiedFollowing,  /*!< Choose the first business day after
                                 the given holiday unless it belongs
                                 to a different month, in which case
                                 choose the first business day before
                                 the holiday. */
        Preceding,          /*!< Choose the first business day before
                                 the given holiday. */
        // NON ISDA
        ModifiedPreceding,  /*!< Choose the first business day before
                                 the given holiday unless it belongs
                                 to a different month, in which case
                                 choose the first business day after
                                 the holiday. */
        Unadjusted,         /*!< Do not adjust. */
        MonthEndReference,  /*!< Choose the first business day after
                                 the given holiday. If the original
                                 date falls on last business day of
                                 the month, choose the last business
                                 day of the month */
        UnadjustedMonthEnd  /*!< Do not adjust, unless the original
                                 date falls on the last business day
                                 of month. In this case, choose the
                                 last day of the month regardless of
                                 whether it is a business day. */
    };

    /*! \relates BusinessDayConvention */
    std::ostream& operator<<(std::ostream&, BusinessDayConvention);


    //! abstract base class for calendar implementations
00077     class CalendarImpl {
      public:
        virtual ~CalendarImpl() {}
        virtual std::string name() const = 0;
        virtual bool isBusinessDay(const Date&) const = 0;
        virtual bool isWeekend(Weekday) const = 0;
        std::set<Date> addedHolidays, removedHolidays;
    };

    //! %calendar class
    /*! This class provides methods for determining whether a date is a
        business day or a holiday for a given market, and for
        incrementing/decrementing a date of a given number of business days.

        The Bridge pattern is used to provide the base behavior of the
        calendar, namely, to determine whether a date is a business day.

        A calendar should be defined for specific exchange holiday schedule
        or for general country holiday schedule. Legacy city holiday schedule
        calendars will be moved to the exchange/country convention.

        \ingroup datetime

        \test the methods for adding and removing holidays are tested
              by inspecting the calendar before and after their
              invocation.
    */
00104     class Calendar : public Bridge<Calendar,CalendarImpl> {
      public:
        //! \name Calendar interface
        //@{
        //! Returns the name of the calendar.
        /*! \warning This method is used for output and comparison between
                calendars. It is <b>not</b> meant to be used for writing
                switch-on-type code.
        */
        std::string name() const;
        /*! Returns <tt>true</tt> iff the date is a business day for the
            given market.
        */
        bool isBusinessDay(const Date& d) const;
        /*! Returns <tt>true</tt> iff the date is a holiday for the given
            market.
        */
        bool isHoliday(const Date& d) const;
        /*! Returns <tt>true</tt> iff the weekday is part of the
            weekend for the given market.
        */
        bool isWeekend(Weekday w) const;
        /*! Returns <tt>true</tt> iff the date is last business day for the
            month in given market.
        */
        bool isEndOfMonth(const Date& d) const;
        //! last business day of the month to which the given date belongs
        Date endOfMonth(const Date& d) const;

        /*! Adds a date to the set of holidays for the given calendar. */
        void addHoliday(const Date&);
        /*! Removes a date from the set of holidays for the given calendar. */
        void removeHoliday(const Date&);

        //! Returns the holidays between two dates
        static std::vector<Date> holidayList(const Calendar& calendar,
                                             const Date& from,
                                             const Date& to,
                                             bool includeWeekEnds = false);

        /*! Adjusts a non-business day to the appropriate near business day
            with respect to the given convention.
        */
        Date adjust(const Date&,
                    BusinessDayConvention convention = Following,
                    const Date& origin = Date()) const;
        /*! Advances the given date of the given number of business days and
            returns the result.
            \note The input date is not modified.
        */
        Date advance(const Date&,
                     Integer n,
                     TimeUnit unit,
                     BusinessDayConvention convention = Following,
                     bool endOfMonth = false) const;
        /*! Advances the given date as specified by the given period and
            returns the result.
            \note The input date is not modified.
        */
        Date advance(const Date& date,
                     const Period& period,
                     BusinessDayConvention convention = Following,
                     bool endOfMonth = false) const;
        /*! Calculates the number of business days between two given
            dates and returns the result.
        */
        BigInteger businessDaysBetween(const Date& from,
                                       const Date& to,
                                       bool includeFirst = true,
                                       bool includeLast = false) const;
        //@}

        //! partial calendar implementation
        /*! This class provides the means of determining the Easter
            Monday for a given year, as well as specifying Saturdays
            and Sundays as weekend days.
        */
00181         class WesternImpl : public CalendarImpl {
          protected:
            bool isWeekend(Weekday) const;
            //! expressed relative to first day of year
            static Day easterMonday(Year);
        };
        //! partial calendar implementation
        /*! This class provides the means of determining the Orthodox
            Easter Monday for a given year, as well as specifying
            Saturdays and Sundays as weekend days.
        */
00192         class OrthodoxImpl : public CalendarImpl {
          protected:
            bool isWeekend(Weekday) const;
            //! expressed relative to first day of year
            static Day easterMonday(Year);
        };
        /*! This default constructor returns a calendar with a null
            implementation, which is therefore unusable except as a
            placeholder.
        */
00202         Calendar() {}
      protected:
        /*! This protected constructor will only be invoked by derived
            classes which define a given Calendar implementation */
00206         Calendar(const boost::shared_ptr<CalendarImpl>& impl)
        : Bridge<Calendar,CalendarImpl>(impl) {}
    };

    /*! Returns <tt>true</tt> iff the two calendars belong to the same
        derived class.
        \relates Calendar
    */
    bool operator==(const Calendar&, const Calendar&);

    /*! \relates Calendar */
    bool operator!=(const Calendar&, const Calendar&);

    /*! \relates Calendar */
    std::ostream& operator<<(std::ostream&, const Calendar&);


    // inline definitions

00225     inline std::string Calendar::name() const {
        return impl_->name();
    }

00229     inline bool Calendar::isBusinessDay(const Date& d) const {
        if (impl_->addedHolidays.find(d) != impl_->addedHolidays.end())
            return false;
        if (impl_->removedHolidays.find(d) != impl_->removedHolidays.end())
            return true;
        return impl_->isBusinessDay(d);
    }

00237     inline bool Calendar::isEndOfMonth(const Date& d) const {
        return (d.month() != adjust(d+1).month());
    }

00241     inline Date Calendar::endOfMonth(const Date& d) const {
        return adjust(Date::endOfMonth(d), Preceding);
    }

00245     inline bool Calendar::isHoliday(const Date& d) const {
        return !isBusinessDay(d);
    }

00249     inline bool Calendar::isWeekend(Weekday w) const {
        return impl_->isWeekend(w);
    }

00253     inline bool operator==(const Calendar& c1, const Calendar& c2) {
        return (c1.empty() && c2.empty())
            || (!c1.empty() && !c2.empty() && c1.name() == c2.name());
    }

00258     inline bool operator!=(const Calendar& c1, const Calendar& c2) {
        return !(c1 == c2);
    }

00262     inline std::ostream& operator<<(std::ostream& out, const Calendar &c) {
        return out << c.name();
    }

}


#endif

Generated by  Doxygen 1.6.0   Back to index