| 1 | /* |
| 2 | Copyright (C) 2006, 2009 Ferdinando Ametrano |
| 3 | Copyright (C) 2006, 2007, 2009 StatPro Italia srl |
| 4 | |
| 5 | This file is part of QuantLib, a free-software/open-source library |
| 6 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 7 | |
| 8 | QuantLib is free software: you can redistribute it and/or modify it |
| 9 | under the terms of the QuantLib license. You should have received a |
| 10 | copy of the license along with this program; if not, please email |
| 11 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 12 | <http://quantlib.org/license.shtml>. |
| 13 | |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but |
| 16 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 | or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. */ |
| 18 | |
| 19 | /*! \file swapindex.hpp |
| 20 | \brief swap-rate indexes |
| 21 | */ |
| 22 | |
| 23 | #ifndef quantlib_swapindex_hpp |
| 24 | #define quantlib_swapindex_hpp |
| 25 | |
| 26 | #include <ql/indexes/interestrateindex.hpp> |
| 27 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 28 | #include <ql/cashflows/rateaveraging.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | class Schedule; |
| 33 | |
| 34 | class IborIndex; |
| 35 | class VanillaSwap; |
| 36 | |
| 37 | class OvernightIndex; |
| 38 | class OvernightIndexedSwap; |
| 39 | |
| 40 | //! base class for swap-rate indexes |
| 41 | class SwapIndex : public InterestRateIndex { |
| 42 | public: |
| 43 | SwapIndex(const std::string& familyName, |
| 44 | const Period& tenor, |
| 45 | Natural settlementDays, |
| 46 | const Currency& currency, |
| 47 | const Calendar& fixingCalendar, |
| 48 | const Period& fixedLegTenor, |
| 49 | BusinessDayConvention fixedLegConvention, |
| 50 | const DayCounter& fixedLegDayCounter, |
| 51 | ext::shared_ptr<IborIndex> iborIndex); |
| 52 | SwapIndex(const std::string& familyName, |
| 53 | const Period& tenor, |
| 54 | Natural settlementDays, |
| 55 | const Currency& currency, |
| 56 | const Calendar& fixingCalendar, |
| 57 | const Period& fixedLegTenor, |
| 58 | BusinessDayConvention fixedLegConvention, |
| 59 | const DayCounter& fixedLegDayCounter, |
| 60 | ext::shared_ptr<IborIndex> iborIndex, |
| 61 | Handle<YieldTermStructure> discountingTermStructure); |
| 62 | //! \name InterestRateIndex interface |
| 63 | //@{ |
| 64 | Date maturityDate(const Date& valueDate) const override; |
| 65 | //@} |
| 66 | //! \name Inspectors |
| 67 | //@{ |
| 68 | Period fixedLegTenor() const { return fixedLegTenor_; } |
| 69 | BusinessDayConvention fixedLegConvention() const; |
| 70 | ext::shared_ptr<IborIndex> iborIndex() const { return iborIndex_; } |
| 71 | Handle<YieldTermStructure> forwardingTermStructure() const; |
| 72 | Handle<YieldTermStructure> discountingTermStructure() const; |
| 73 | bool exogenousDiscount() const; |
| 74 | /*! \warning Relinking the term structure underlying the index will |
| 75 | not have effect on the returned swap. |
| 76 | */ |
| 77 | ext::shared_ptr<VanillaSwap> underlyingSwap( |
| 78 | const Date& fixingDate) const; |
| 79 | //@} |
| 80 | //! \name Other methods |
| 81 | //@{ |
| 82 | //! returns a copy of itself linked to a different forwarding curve |
| 83 | virtual ext::shared_ptr<SwapIndex> clone( |
| 84 | const Handle<YieldTermStructure>& forwarding) const; |
| 85 | //! returns a copy of itself linked to different curves |
| 86 | virtual ext::shared_ptr<SwapIndex> clone( |
| 87 | const Handle<YieldTermStructure>& forwarding, |
| 88 | const Handle<YieldTermStructure>& discounting) const; |
| 89 | //! returns a copy of itself with different tenor |
| 90 | virtual ext::shared_ptr<SwapIndex> clone( |
| 91 | const Period& tenor) const; |
| 92 | // @} |
| 93 | protected: |
| 94 | Rate forecastFixing(const Date& fixingDate) const override; |
| 95 | Period tenor_; |
| 96 | ext::shared_ptr<IborIndex> iborIndex_; |
| 97 | Period fixedLegTenor_; |
| 98 | BusinessDayConvention fixedLegConvention_; |
| 99 | bool exogenousDiscount_; |
| 100 | Handle<YieldTermStructure> discount_; |
| 101 | // cache data to avoid swap recreation when the same fixing date |
| 102 | // is used multiple time to forecast changing fixing |
| 103 | mutable ext::shared_ptr<VanillaSwap> lastSwap_; |
| 104 | mutable Date lastFixingDate_; |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | //! base class for overnight indexed swap indexes |
| 109 | class OvernightIndexedSwapIndex : public SwapIndex { |
| 110 | public: |
| 111 | OvernightIndexedSwapIndex( |
| 112 | const std::string& familyName, |
| 113 | const Period& tenor, |
| 114 | Natural settlementDays, |
| 115 | const Currency& currency, |
| 116 | const ext::shared_ptr<OvernightIndex>& overnightIndex, |
| 117 | bool telescopicValueDates = false, |
| 118 | RateAveraging::Type averagingMethod = RateAveraging::Compound); |
| 119 | //! \name Inspectors |
| 120 | //@{ |
| 121 | ext::shared_ptr<OvernightIndex> overnightIndex() const; |
| 122 | /*! \warning Relinking the term structure underlying the index will |
| 123 | not have effect on the returned swap. |
| 124 | */ |
| 125 | ext::shared_ptr<OvernightIndexedSwap> underlyingSwap( |
| 126 | const Date& fixingDate) const; |
| 127 | //@} |
| 128 | protected: |
| 129 | ext::shared_ptr<OvernightIndex> overnightIndex_; |
| 130 | bool telescopicValueDates_; |
| 131 | RateAveraging::Type averagingMethod_; |
| 132 | // cache data to avoid swap recreation when the same fixing date |
| 133 | // is used multiple time to forecast changing fixing |
| 134 | mutable ext::shared_ptr<OvernightIndexedSwap> lastSwap_; |
| 135 | mutable Date lastFixingDate_; |
| 136 | }; |
| 137 | |
| 138 | // inline definitions |
| 139 | |
| 140 | inline BusinessDayConvention SwapIndex::fixedLegConvention() const { |
| 141 | return fixedLegConvention_; |
| 142 | } |
| 143 | |
| 144 | inline bool SwapIndex::exogenousDiscount() const { |
| 145 | return exogenousDiscount_; |
| 146 | } |
| 147 | |
| 148 | inline ext::shared_ptr<OvernightIndex> |
| 149 | OvernightIndexedSwapIndex::overnightIndex() const { |
| 150 | return overnightIndex_; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | #endif |
| 156 | |