| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 5 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009 StatPro Italia srl |
| 6 | Copyright (C) 2006, 2011 Ferdinando Ametrano |
| 7 | |
| 8 | This file is part of QuantLib, a free-software/open-source library |
| 9 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 10 | |
| 11 | QuantLib is free software: you can redistribute it and/or modify it |
| 12 | under the terms of the QuantLib license. You should have received a |
| 13 | copy of the license along with this program; if not, please email |
| 14 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 15 | <http://quantlib.org/license.shtml>. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 20 | */ |
| 21 | |
| 22 | /*! \file interestrateindex.hpp |
| 23 | \brief base class for interest rate indexes |
| 24 | */ |
| 25 | |
| 26 | #ifndef quantlib_interestrateindex_hpp |
| 27 | #define quantlib_interestrateindex_hpp |
| 28 | |
| 29 | #include <ql/index.hpp> |
| 30 | #include <ql/time/calendar.hpp> |
| 31 | #include <ql/currency.hpp> |
| 32 | #include <ql/time/daycounter.hpp> |
| 33 | #include <ql/time/period.hpp> |
| 34 | |
| 35 | namespace QuantLib { |
| 36 | |
| 37 | //! base class for interest rate indexes |
| 38 | /*! \todo add methods returning InterestRate */ |
| 39 | class InterestRateIndex : public Index, |
| 40 | public Observer { |
| 41 | public: |
| 42 | InterestRateIndex(std::string familyName, |
| 43 | const Period& tenor, |
| 44 | Natural settlementDays, |
| 45 | Currency currency, |
| 46 | Calendar fixingCalendar, |
| 47 | DayCounter dayCounter); |
| 48 | //! \name Index interface |
| 49 | //@{ |
| 50 | std::string name() const override; |
| 51 | Calendar fixingCalendar() const override; |
| 52 | bool isValidFixingDate(const Date& fixingDate) const override; |
| 53 | Rate fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override; |
| 54 | //@} |
| 55 | //! \name Observer interface |
| 56 | //@{ |
| 57 | void update() override; |
| 58 | //@} |
| 59 | //! \name Inspectors |
| 60 | //@{ |
| 61 | std::string familyName() const { return familyName_; } |
| 62 | Period tenor() const { return tenor_; } |
| 63 | Natural fixingDays() const { return fixingDays_; } |
| 64 | Date fixingDate(const Date& valueDate) const; |
| 65 | const Currency& currency() const { return currency_; } |
| 66 | const DayCounter& dayCounter() const { return dayCounter_; } |
| 67 | //@} |
| 68 | /*! \name Date calculations |
| 69 | |
| 70 | These method can be overridden to implement particular |
| 71 | conventions (e.g. EurLibor) |
| 72 | |
| 73 | @{ |
| 74 | */ |
| 75 | virtual Date valueDate(const Date& fixingDate) const; |
| 76 | virtual Date maturityDate(const Date& valueDate) const = 0; |
| 77 | //@} |
| 78 | //! \name Fixing calculations |
| 79 | //@{ |
| 80 | //! It can be overridden to implement particular conventions |
| 81 | virtual Rate forecastFixing(const Date& fixingDate) const = 0; |
| 82 | virtual Rate pastFixing(const Date& fixingDate) const; |
| 83 | // @} |
| 84 | protected: |
| 85 | std::string familyName_; |
| 86 | Period tenor_; |
| 87 | Natural fixingDays_; |
| 88 | Currency currency_; |
| 89 | DayCounter dayCounter_; |
| 90 | std::string name_; |
| 91 | private: |
| 92 | Calendar fixingCalendar_; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | // inline definitions |
| 97 | |
| 98 | inline std::string InterestRateIndex::name() const { |
| 99 | return name_; |
| 100 | } |
| 101 | |
| 102 | inline Calendar InterestRateIndex::fixingCalendar() const { |
| 103 | return fixingCalendar_; |
| 104 | } |
| 105 | |
| 106 | inline bool InterestRateIndex::isValidFixingDate(const Date& d) const { |
| 107 | return fixingCalendar().isBusinessDay(d); |
| 108 | } |
| 109 | |
| 110 | inline void InterestRateIndex::update() { |
| 111 | notifyObservers(); |
| 112 | } |
| 113 | |
| 114 | inline Date InterestRateIndex::fixingDate(const Date& valueDate) const { |
| 115 | Date fixingDate = fixingCalendar().advance(valueDate, |
| 116 | n: -static_cast<Integer>(fixingDays_), unit: Days); |
| 117 | return fixingDate; |
| 118 | } |
| 119 | |
| 120 | inline Date InterestRateIndex::valueDate(const Date& fixingDate) const { |
| 121 | QL_REQUIRE(isValidFixingDate(fixingDate), |
| 122 | fixingDate << " is not a valid fixing date" ); |
| 123 | return fixingCalendar().advance(fixingDate, n: fixingDays_, unit: Days); |
| 124 | } |
| 125 | |
| 126 | inline Rate InterestRateIndex::pastFixing(const Date& fixingDate) const { |
| 127 | QL_REQUIRE(isValidFixingDate(fixingDate), |
| 128 | fixingDate << " is not a valid fixing date" ); |
| 129 | return timeSeries()[fixingDate]; |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | #endif |
| 135 | |