| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004, 2005, 2006, 2007 StatPro Italia srl |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file termstructure.hpp |
| 21 | \brief base class for term structures |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_term_structure_hpp |
| 25 | #define quantlib_term_structure_hpp |
| 26 | |
| 27 | #include <ql/time/calendar.hpp> |
| 28 | #include <ql/time/daycounter.hpp> |
| 29 | #include <ql/settings.hpp> |
| 30 | #include <ql/handle.hpp> |
| 31 | #include <ql/math/interpolations/extrapolation.hpp> |
| 32 | #include <ql/utilities/null.hpp> |
| 33 | |
| 34 | namespace QuantLib { |
| 35 | |
| 36 | //! Basic term-structure functionality |
| 37 | class TermStructure : public virtual Observer, |
| 38 | public virtual Observable, |
| 39 | public Extrapolator { |
| 40 | public: |
| 41 | /*! \name Constructors |
| 42 | |
| 43 | There are three ways in which a term structure can keep |
| 44 | track of its reference date. The first is that such date |
| 45 | is fixed; the second is that it is determined by advancing |
| 46 | the current date of a given number of business days; and |
| 47 | the third is that it is based on the reference date of |
| 48 | some other structure. |
| 49 | |
| 50 | In the first case, the constructor taking a date is to be |
| 51 | used; the default implementation of referenceDate() will |
| 52 | then return such date. In the second case, the constructor |
| 53 | taking a number of days and a calendar is to be used; |
| 54 | referenceDate() will return a date calculated based on the |
| 55 | current evaluation date, and the term structure and its |
| 56 | observers will be notified when the evaluation date |
| 57 | changes. In the last case, the referenceDate() method must |
| 58 | be overridden in derived classes so that it fetches and |
| 59 | return the appropriate date. |
| 60 | */ |
| 61 | //@{ |
| 62 | //! default constructor |
| 63 | /*! \warning term structures initialized by means of this |
| 64 | constructor must manage their own reference date |
| 65 | by overriding the referenceDate() method. |
| 66 | */ |
| 67 | explicit TermStructure(DayCounter dc = DayCounter()); |
| 68 | //! initialize with a fixed reference date |
| 69 | explicit TermStructure(const Date& referenceDate, |
| 70 | Calendar calendar = Calendar(), |
| 71 | DayCounter dc = DayCounter()); |
| 72 | //! calculate the reference date based on the global evaluation date |
| 73 | TermStructure(Natural settlementDays, Calendar, DayCounter dc = DayCounter()); |
| 74 | //@} |
| 75 | ~TermStructure() override = default; |
| 76 | //! \name Dates and Time |
| 77 | //@{ |
| 78 | //! the day counter used for date/time conversion |
| 79 | virtual DayCounter dayCounter() const; |
| 80 | //! date/time conversion |
| 81 | Time timeFromReference(const Date& date) const; |
| 82 | //! the latest date for which the curve can return values |
| 83 | virtual Date maxDate() const = 0; |
| 84 | //! the latest time for which the curve can return values |
| 85 | virtual Time maxTime() const; |
| 86 | //! the date at which discount = 1.0 and/or variance = 0.0 |
| 87 | virtual const Date& referenceDate() const; |
| 88 | //! the calendar used for reference and/or option date calculation |
| 89 | virtual Calendar calendar() const; |
| 90 | //! the settlementDays used for reference date calculation |
| 91 | virtual Natural settlementDays() const; |
| 92 | //@} |
| 93 | //! \name Observer interface |
| 94 | //@{ |
| 95 | void update() override; |
| 96 | //@} |
| 97 | protected: |
| 98 | //! date-range check |
| 99 | void checkRange(const Date& d, |
| 100 | bool ) const; |
| 101 | //! time-range check |
| 102 | void checkRange(Time t, |
| 103 | bool ) const; |
| 104 | bool moving_ = false; |
| 105 | mutable bool updated_ = true; |
| 106 | Calendar calendar_; |
| 107 | private: |
| 108 | mutable Date referenceDate_; |
| 109 | Natural settlementDays_; |
| 110 | DayCounter dayCounter_; |
| 111 | }; |
| 112 | |
| 113 | // inline definitions |
| 114 | |
| 115 | inline DayCounter TermStructure::dayCounter() const { |
| 116 | return dayCounter_; |
| 117 | } |
| 118 | |
| 119 | inline Time TermStructure::maxTime() const { |
| 120 | return timeFromReference(date: maxDate()); |
| 121 | } |
| 122 | |
| 123 | inline Calendar TermStructure::calendar() const { |
| 124 | return calendar_; |
| 125 | } |
| 126 | |
| 127 | inline Natural TermStructure::settlementDays() const { |
| 128 | QL_REQUIRE(settlementDays_!=Null<Natural>(), |
| 129 | "settlement days not provided for this instance" ); |
| 130 | return settlementDays_; |
| 131 | } |
| 132 | |
| 133 | inline Time TermStructure::timeFromReference(const Date& d) const { |
| 134 | return dayCounter().yearFraction(d1: referenceDate(), d2: d); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | #endif |
| 140 | |