/src/quantlib/ql/termstructures/yield/multipleresetsswaphelper.cpp
Line | Count | Source |
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/instruments/makemultipleresetsswap.hpp> |
21 | | #include <ql/instruments/simplifynotificationgraph.hpp> |
22 | | #include <ql/termstructures/yield/multipleresetsswaphelper.hpp> |
23 | | #include <ql/utilities/null_deleter.hpp> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | MultipleResetsSwapRateHelper::MultipleResetsSwapRateHelper( |
28 | | Natural settlementDays, |
29 | | const Period& tenor, |
30 | | const std::variant<Rate, Handle<Quote>>& fixedRate, |
31 | | const ext::shared_ptr<IborIndex>& iborIndex, |
32 | | Size resetsPerCoupon, |
33 | | Handle<YieldTermStructure> discountingCurve, |
34 | | RateAveraging::Type averagingMethod, |
35 | | Spread spread, |
36 | | Frequency fixedFrequency, |
37 | | const DayCounter& fixedDayCount, |
38 | | BusinessDayConvention fixedConvention) |
39 | 0 | : RelativeDateRateHelper(fixedRate), settlementDays_(settlementDays), tenor_(tenor), |
40 | 0 | resetsPerCoupon_(resetsPerCoupon), averagingMethod_(averagingMethod), spread_(spread), |
41 | 0 | fixedFrequency_(fixedFrequency), |
42 | 0 | fixedDayCount_(!fixedDayCount.empty() ? fixedDayCount : iborIndex->dayCounter()), |
43 | 0 | fixedConvention_(fixedConvention), discountHandle_(std::move(discountingCurve)) { |
44 | | |
45 | | // Clone the index so it forwards rates from termStructureHandle_, |
46 | | // but don't register the clone as an observer of termStructureHandle_ |
47 | | // (that would interfere with bootstrapping). |
48 | 0 | iborIndex_ = ext::dynamic_pointer_cast<IborIndex>(iborIndex->clone(termStructureHandle_)); |
49 | 0 | iborIndex_->unregisterWith(termStructureHandle_); |
50 | |
|
51 | 0 | registerWith(iborIndex_); |
52 | 0 | registerWith(discountHandle_); |
53 | 0 | initializeDates(); |
54 | 0 | } |
55 | | |
56 | 0 | void MultipleResetsSwapRateHelper::initializeDates() { |
57 | 0 | swap_ = MakeMultipleResetsSwap(tenor_, iborIndex_, resetsPerCoupon_) |
58 | 0 | .withFixedRate(0.0) |
59 | 0 | .withSettlementDays(settlementDays_) |
60 | 0 | .withFixedLegFrequency(fixedFrequency_) |
61 | 0 | .withFixedLegDayCount(fixedDayCount_) |
62 | 0 | .withFixedLegConvention(fixedConvention_) |
63 | 0 | .withFloatingLegSpread(spread_) |
64 | 0 | .withAveragingMethod(averagingMethod_) |
65 | 0 | .withDiscountingTermStructure(discountRelinkableHandle_); |
66 | |
|
67 | 0 | simplifyNotificationGraph(*swap_, true); |
68 | |
|
69 | 0 | earliestDate_ = swap_->startDate(); |
70 | 0 | latestRelevantDate_ = latestDate_ = |
71 | 0 | std::max(swap_->fixedLeg().back()->date(), swap_->floatingLeg().back()->date()); |
72 | 0 | } |
73 | | |
74 | 0 | void MultipleResetsSwapRateHelper::setTermStructure(YieldTermStructure* t) { |
75 | 0 | bool observer = false; |
76 | 0 | ext::shared_ptr<YieldTermStructure> temp(t, null_deleter()); |
77 | 0 | termStructureHandle_.linkTo(temp, observer); |
78 | 0 | if (discountHandle_.empty()) |
79 | 0 | discountRelinkableHandle_.linkTo(temp, observer); |
80 | 0 | else |
81 | 0 | discountRelinkableHandle_.linkTo(*discountHandle_, observer); |
82 | 0 | RelativeDateRateHelper::setTermStructure(t); |
83 | 0 | } |
84 | | |
85 | 0 | Real MultipleResetsSwapRateHelper::impliedQuote() const { |
86 | 0 | QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); |
87 | 0 | swap_->deepUpdate(); |
88 | 0 | return swap_->fairRate(); |
89 | 0 | } |
90 | | |
91 | 0 | void MultipleResetsSwapRateHelper::accept(AcyclicVisitor& v) { |
92 | 0 | auto* v1 = dynamic_cast<Visitor<MultipleResetsSwapRateHelper>*>(&v); |
93 | 0 | if (v1 != nullptr) |
94 | 0 | v1->visit(*this); |
95 | 0 | else |
96 | 0 | RateHelper::accept(v); |
97 | 0 | } |
98 | | |
99 | | } |