|
| 1 | +/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright (C) 2026 Zain Mughal |
| 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 | + <https://www.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 | +#include <ql/indexes/iborindex.hpp> |
| 21 | +#include <ql/instruments/makemultipleresetsswap.hpp> |
| 22 | +#include <ql/pricingengines/swap/discountingswapengine.hpp> |
| 23 | +#include <ql/settings.hpp> |
| 24 | +#include <ql/time/schedule.hpp> |
| 25 | + |
| 26 | +namespace QuantLib { |
| 27 | + |
| 28 | + MakeMultipleResetsSwap::MakeMultipleResetsSwap( |
| 29 | + const Period& tenor, |
| 30 | + const ext::shared_ptr<IborIndex>& iborIndex, |
| 31 | + Size resetsPerCoupon, |
| 32 | + Rate fixedRate, |
| 33 | + const Period& fwdStart) |
| 34 | + : tenor_(tenor), iborIndex_(iborIndex), resetsPerCoupon_(resetsPerCoupon), |
| 35 | + fixedRate_(fixedRate), forwardStart_(fwdStart), |
| 36 | + fixedDayCount_(iborIndex->dayCounter()) {} |
| 37 | + |
| 38 | + MakeMultipleResetsSwap::operator MultipleResetsSwap() const { |
| 39 | + ext::shared_ptr<MultipleResetsSwap> swap = *this; |
| 40 | + return *swap; |
| 41 | + } |
| 42 | + |
| 43 | + MakeMultipleResetsSwap::operator ext::shared_ptr<MultipleResetsSwap>() const { |
| 44 | + Calendar cal = iborIndex_->fixingCalendar(); |
| 45 | + BusinessDayConvention bdc = iborIndex_->businessDayConvention(); |
| 46 | + |
| 47 | + Date startDate; |
| 48 | + if (effectiveDate_ != Date()) { |
| 49 | + startDate = effectiveDate_; |
| 50 | + } else { |
| 51 | + Natural settlDays = settlementDays_ != Null<Natural>() ? |
| 52 | + settlementDays_ : iborIndex_->fixingDays(); |
| 53 | + Date refDate = Settings::instance().evaluationDate(); |
| 54 | + startDate = cal.advance(cal.adjust(refDate), settlDays * Days); |
| 55 | + startDate = cal.advance(startDate, forwardStart_, |
| 56 | + forwardStart_.length() < 0 ? Preceding : Following); |
| 57 | + } |
| 58 | + |
| 59 | + Date endDate = terminationDate_ != Date() ? |
| 60 | + terminationDate_ : |
| 61 | + cal.advance(startDate, tenor_, bdc); |
| 62 | + |
| 63 | + Period resetTenor = iborIndex_->tenor(); |
| 64 | + // Fixed coupon period: resetsPerCoupon consecutive reset periods. |
| 65 | + // If not overridden, derive the fixed frequency from the coupon period. |
| 66 | + Frequency fixedFreq = fixedFrequency_; |
| 67 | + if (fixedFreq == NoFrequency) { |
| 68 | + Period couponTenor(resetsPerCoupon_ * resetTenor.length(), resetTenor.units()); |
| 69 | + fixedFreq = couponTenor.frequency(); |
| 70 | + } |
| 71 | + |
| 72 | + Schedule fixedSchedule(startDate, endDate, Period(fixedFreq), |
| 73 | + cal, fixedConvention_, fixedConvention_, |
| 74 | + DateGeneration::Backward, false); |
| 75 | + |
| 76 | + Schedule fullResetSchedule(startDate, endDate, resetTenor, |
| 77 | + cal, bdc, bdc, |
| 78 | + DateGeneration::Backward, false); |
| 79 | + |
| 80 | + Rate usedFixedRate = fixedRate_; |
| 81 | + if (fixedRate_ == Null<Rate>()) { |
| 82 | + MultipleResetsSwap temp(type_, nominal_, |
| 83 | + fixedSchedule, 0.0, fixedDayCount_, |
| 84 | + fullResetSchedule, iborIndex_, resetsPerCoupon_, |
| 85 | + spread_, averagingMethod_); |
| 86 | + if (engine_ == nullptr) { |
| 87 | + Handle<YieldTermStructure> disc = |
| 88 | + iborIndex_->forwardingTermStructure(); |
| 89 | + QL_REQUIRE(!disc.empty(), |
| 90 | + "null term structure set to this instance of " |
| 91 | + << iborIndex_->name()); |
| 92 | + temp.setPricingEngine(ext::make_shared<DiscountingSwapEngine>(disc, false)); |
| 93 | + } else { |
| 94 | + temp.setPricingEngine(engine_); |
| 95 | + } |
| 96 | + usedFixedRate = temp.fairRate(); |
| 97 | + } |
| 98 | + |
| 99 | + auto swap = ext::make_shared<MultipleResetsSwap>( |
| 100 | + type_, nominal_, |
| 101 | + fixedSchedule, usedFixedRate, fixedDayCount_, |
| 102 | + fullResetSchedule, iborIndex_, resetsPerCoupon_, |
| 103 | + spread_, averagingMethod_); |
| 104 | + |
| 105 | + if (engine_ == nullptr) { |
| 106 | + Handle<YieldTermStructure> disc = iborIndex_->forwardingTermStructure(); |
| 107 | + if (!disc.empty()) |
| 108 | + swap->setPricingEngine(ext::make_shared<DiscountingSwapEngine>(disc, false)); |
| 109 | + } else { |
| 110 | + swap->setPricingEngine(engine_); |
| 111 | + } |
| 112 | + |
| 113 | + return swap; |
| 114 | + } |
| 115 | + |
| 116 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::receiveFixed(bool flag) { |
| 117 | + type_ = flag ? Swap::Receiver : Swap::Payer; |
| 118 | + return *this; |
| 119 | + } |
| 120 | + |
| 121 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withType(Swap::Type type) { |
| 122 | + type_ = type; |
| 123 | + return *this; |
| 124 | + } |
| 125 | + |
| 126 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withNominal(Real n) { |
| 127 | + nominal_ = n; |
| 128 | + return *this; |
| 129 | + } |
| 130 | + |
| 131 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withSettlementDays(Natural settlementDays) { |
| 132 | + settlementDays_ = settlementDays; |
| 133 | + effectiveDate_ = Date(); |
| 134 | + return *this; |
| 135 | + } |
| 136 | + |
| 137 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withEffectiveDate(const Date& d) { |
| 138 | + effectiveDate_ = d; |
| 139 | + return *this; |
| 140 | + } |
| 141 | + |
| 142 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withTerminationDate(const Date& d) { |
| 143 | + terminationDate_ = d; |
| 144 | + if (d != Date()) |
| 145 | + tenor_ = Period(); |
| 146 | + return *this; |
| 147 | + } |
| 148 | + |
| 149 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withFixedLegFrequency(Frequency f) { |
| 150 | + fixedFrequency_ = f; |
| 151 | + return *this; |
| 152 | + } |
| 153 | + |
| 154 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withFixedLegDayCount(const DayCounter& dc) { |
| 155 | + fixedDayCount_ = dc; |
| 156 | + return *this; |
| 157 | + } |
| 158 | + |
| 159 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withFixedLegConvention(BusinessDayConvention bdc) { |
| 160 | + fixedConvention_ = bdc; |
| 161 | + return *this; |
| 162 | + } |
| 163 | + |
| 164 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withFloatingLegSpread(Spread sp) { |
| 165 | + spread_ = sp; |
| 166 | + return *this; |
| 167 | + } |
| 168 | + |
| 169 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withAveragingMethod(RateAveraging::Type m) { |
| 170 | + averagingMethod_ = m; |
| 171 | + return *this; |
| 172 | + } |
| 173 | + |
| 174 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withDiscountingTermStructure( |
| 175 | + const Handle<YieldTermStructure>& d) { |
| 176 | + engine_ = ext::make_shared<DiscountingSwapEngine>(d, false); |
| 177 | + return *this; |
| 178 | + } |
| 179 | + |
| 180 | + MakeMultipleResetsSwap& MakeMultipleResetsSwap::withPricingEngine( |
| 181 | + const ext::shared_ptr<PricingEngine>& engine) { |
| 182 | + engine_ = engine; |
| 183 | + return *this; |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments