| 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 | #include <ql/indexes/iborindex.hpp> |
| 20 | #include <ql/indexes/swapindex.hpp> |
| 21 | #include <ql/instruments/makeois.hpp> |
| 22 | #include <ql/instruments/makevanillaswap.hpp> |
| 23 | #include <ql/time/schedule.hpp> |
| 24 | #include <sstream> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace QuantLib { |
| 28 | |
| 29 | SwapIndex::SwapIndex(const std::string& familyName, |
| 30 | const Period& tenor, |
| 31 | Natural settlementDays, |
| 32 | const Currency& currency, |
| 33 | const Calendar& fixingCalendar, |
| 34 | const Period& fixedLegTenor, |
| 35 | BusinessDayConvention fixedLegConvention, |
| 36 | const DayCounter& fixedLegDayCounter, |
| 37 | ext::shared_ptr<IborIndex> iborIndex) |
| 38 | : InterestRateIndex( |
| 39 | familyName, tenor, settlementDays, currency, fixingCalendar, fixedLegDayCounter), |
| 40 | tenor_(tenor), iborIndex_(std::move(iborIndex)), fixedLegTenor_(fixedLegTenor), |
| 41 | fixedLegConvention_(fixedLegConvention), exogenousDiscount_(false) { |
| 42 | registerWith(h: iborIndex_); |
| 43 | } |
| 44 | |
| 45 | SwapIndex::SwapIndex(const std::string& familyName, |
| 46 | const Period& tenor, |
| 47 | Natural settlementDays, |
| 48 | const Currency& currency, |
| 49 | const Calendar& fixingCalendar, |
| 50 | const Period& fixedLegTenor, |
| 51 | BusinessDayConvention fixedLegConvention, |
| 52 | const DayCounter& fixedLegDayCounter, |
| 53 | ext::shared_ptr<IborIndex> iborIndex, |
| 54 | Handle<YieldTermStructure> discount) |
| 55 | : InterestRateIndex( |
| 56 | familyName, tenor, settlementDays, currency, fixingCalendar, fixedLegDayCounter), |
| 57 | tenor_(tenor), iborIndex_(std::move(iborIndex)), fixedLegTenor_(fixedLegTenor), |
| 58 | fixedLegConvention_(fixedLegConvention), exogenousDiscount_(true), |
| 59 | discount_(std::move(discount)) { |
| 60 | registerWith(h: iborIndex_); |
| 61 | registerWith(h: discount_); |
| 62 | } |
| 63 | |
| 64 | Handle<YieldTermStructure> SwapIndex::forwardingTermStructure() const { |
| 65 | return iborIndex_->forwardingTermStructure(); |
| 66 | } |
| 67 | |
| 68 | Handle<YieldTermStructure> SwapIndex::discountingTermStructure() const { |
| 69 | return discount_; // empty if not exogenous |
| 70 | } |
| 71 | |
| 72 | Rate SwapIndex::forecastFixing(const Date& fixingDate) const { |
| 73 | return underlyingSwap(fixingDate)->fairRate(); |
| 74 | } |
| 75 | |
| 76 | ext::shared_ptr<VanillaSwap> |
| 77 | SwapIndex::underlyingSwap(const Date& fixingDate) const { |
| 78 | |
| 79 | QL_REQUIRE(fixingDate!=Date(), "null fixing date" ); |
| 80 | |
| 81 | // caching mechanism |
| 82 | if (lastFixingDate_!=fixingDate) { |
| 83 | Rate fixedRate = 0.0; |
| 84 | if (exogenousDiscount_) |
| 85 | lastSwap_ = MakeVanillaSwap(tenor_, iborIndex_, fixedRate) |
| 86 | .withEffectiveDate(valueDate(fixingDate)) |
| 87 | .withFixedLegCalendar(cal: fixingCalendar()) |
| 88 | .withFixedLegDayCount(dc: dayCounter_) |
| 89 | .withFixedLegTenor(t: fixedLegTenor_) |
| 90 | .withFixedLegConvention(bdc: fixedLegConvention_) |
| 91 | .withFixedLegTerminationDateConvention(bdc: fixedLegConvention_) |
| 92 | .withDiscountingTermStructure(discountCurve: discount_); |
| 93 | else |
| 94 | lastSwap_ = MakeVanillaSwap(tenor_, iborIndex_, fixedRate) |
| 95 | .withEffectiveDate(valueDate(fixingDate)) |
| 96 | .withFixedLegCalendar(cal: fixingCalendar()) |
| 97 | .withFixedLegDayCount(dc: dayCounter_) |
| 98 | .withFixedLegTenor(t: fixedLegTenor_) |
| 99 | .withFixedLegConvention(bdc: fixedLegConvention_) |
| 100 | .withFixedLegTerminationDateConvention(bdc: fixedLegConvention_); |
| 101 | lastFixingDate_ = fixingDate; |
| 102 | } |
| 103 | return lastSwap_; |
| 104 | } |
| 105 | |
| 106 | Date SwapIndex::maturityDate(const Date& valueDate) const { |
| 107 | Date fixDate = fixingDate(valueDate); |
| 108 | return underlyingSwap(fixingDate: fixDate)->maturityDate(); |
| 109 | } |
| 110 | |
| 111 | ext::shared_ptr<SwapIndex> |
| 112 | SwapIndex::clone(const Handle<YieldTermStructure>& forwarding) const { |
| 113 | |
| 114 | if (exogenousDiscount_) |
| 115 | return ext::make_shared<SwapIndex>(args: familyName(), |
| 116 | args: tenor(), |
| 117 | args: fixingDays(), |
| 118 | args: currency(), |
| 119 | args: fixingCalendar(), |
| 120 | args: fixedLegTenor(), |
| 121 | args: fixedLegConvention(), |
| 122 | args: dayCounter(), |
| 123 | args: iborIndex_->clone(forwarding), |
| 124 | args: discount_); |
| 125 | else |
| 126 | return ext::make_shared<SwapIndex>(args: familyName(), |
| 127 | args: tenor(), |
| 128 | args: fixingDays(), |
| 129 | args: currency(), |
| 130 | args: fixingCalendar(), |
| 131 | args: fixedLegTenor(), |
| 132 | args: fixedLegConvention(), |
| 133 | args: dayCounter(), |
| 134 | args: iborIndex_->clone(forwarding)); |
| 135 | } |
| 136 | |
| 137 | ext::shared_ptr<SwapIndex> |
| 138 | SwapIndex::clone(const Handle<YieldTermStructure>& forwarding, |
| 139 | const Handle<YieldTermStructure>& discounting) const { |
| 140 | return ext::make_shared<SwapIndex>(args: familyName(), |
| 141 | args: tenor(), |
| 142 | args: fixingDays(), |
| 143 | args: currency(), |
| 144 | args: fixingCalendar(), |
| 145 | args: fixedLegTenor(), |
| 146 | args: fixedLegConvention(), |
| 147 | args: dayCounter(), |
| 148 | args: iborIndex_->clone(forwarding), |
| 149 | args: discounting); |
| 150 | } |
| 151 | |
| 152 | ext::shared_ptr<SwapIndex> |
| 153 | SwapIndex::clone(const Period& tenor) const { |
| 154 | |
| 155 | if (exogenousDiscount_) |
| 156 | return ext::make_shared<SwapIndex>(args: familyName(), |
| 157 | args: tenor, |
| 158 | args: fixingDays(), |
| 159 | args: currency(), |
| 160 | args: fixingCalendar(), |
| 161 | args: fixedLegTenor(), |
| 162 | args: fixedLegConvention(), |
| 163 | args: dayCounter(), |
| 164 | args: iborIndex(), |
| 165 | args: discountingTermStructure()); |
| 166 | else |
| 167 | return ext::make_shared<SwapIndex>(args: familyName(), |
| 168 | args: tenor, |
| 169 | args: fixingDays(), |
| 170 | args: currency(), |
| 171 | args: fixingCalendar(), |
| 172 | args: fixedLegTenor(), |
| 173 | args: fixedLegConvention(), |
| 174 | args: dayCounter(), |
| 175 | args: iborIndex()); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | OvernightIndexedSwapIndex::OvernightIndexedSwapIndex( |
| 180 | const std::string& familyName, |
| 181 | const Period& tenor, |
| 182 | Natural settlementDays, |
| 183 | const Currency& currency, |
| 184 | const ext::shared_ptr<OvernightIndex>& overnightIndex, |
| 185 | bool telescopicValueDates, |
| 186 | RateAveraging::Type averagingMethod) |
| 187 | : SwapIndex(familyName, |
| 188 | tenor, |
| 189 | settlementDays, |
| 190 | currency, |
| 191 | overnightIndex->fixingCalendar(), |
| 192 | 1 * Years, |
| 193 | ModifiedFollowing, |
| 194 | overnightIndex->dayCounter(), |
| 195 | overnightIndex), |
| 196 | overnightIndex_(overnightIndex), |
| 197 | telescopicValueDates_(telescopicValueDates), |
| 198 | averagingMethod_(averagingMethod) {} |
| 199 | |
| 200 | |
| 201 | ext::shared_ptr<OvernightIndexedSwap> |
| 202 | OvernightIndexedSwapIndex::underlyingSwap(const Date& fixingDate) const { |
| 203 | |
| 204 | QL_REQUIRE(fixingDate!=Date(), "null fixing date" ); |
| 205 | |
| 206 | // caching mechanism |
| 207 | if (lastFixingDate_!=fixingDate) { |
| 208 | Rate fixedRate = 0.0; |
| 209 | lastSwap_ = MakeOIS(tenor_, overnightIndex_, fixedRate) |
| 210 | .withEffectiveDate(valueDate(fixingDate)) |
| 211 | .withFixedLegDayCount(dc: dayCounter_) |
| 212 | .withTelescopicValueDates(telescopicValueDates: telescopicValueDates_) |
| 213 | .withAveragingMethod(averagingMethod: averagingMethod_); |
| 214 | lastFixingDate_ = fixingDate; |
| 215 | } |
| 216 | return lastSwap_; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |