Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/cashflows/multipleresetscoupon.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Toyin Akin
5
 Copyright (C) 2021 Marcin Rybacki
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <http://quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
/*! \file multipleresetscoupon.hpp
22
    \brief Coupon compounding or averaging multiple fixings
23
*/
24
25
#ifndef quantlib_multiple_resets_coupon_hpp
26
#define quantlib_multiple_resets_coupon_hpp
27
28
#include <ql/cashflows/couponpricer.hpp>
29
#include <ql/cashflows/floatingratecoupon.hpp>
30
#include <ql/cashflows/rateaveraging.hpp>
31
#include <ql/time/schedule.hpp>
32
#include <vector>
33
34
namespace QuantLib {
35
36
    class IborIndex;
37
38
    //! multiple-reset coupon
39
    /*! %Coupon paying a rate calculated by compounding or averaging
40
        multiple fixings during its accrual period.
41
    */
42
    class MultipleResetsCoupon : public FloatingRateCoupon {
43
      public:
44
        /*! \param resetSchedule the schedule for the multiple resets. The first and last
45
                                 dates are also the start and end dates of the coupon.
46
                                 Each period specified by the schedule is the underlying
47
                                 period for one fixing; the corresponding fixing date is
48
                                 the passed number of fixing days before the start of
49
                                 the period.
50
            \param couponSpread  an optional spread added to the final coupon rate.
51
            \param rateSpread    an optional spread added to each of the underlying fixings.
52
            \param gearing       an optional multiplier for the final coupon rate.
53
        */
54
        MultipleResetsCoupon(const Date& paymentDate,
55
                             Real nominal,
56
                             const Schedule& resetSchedule,
57
                             Natural fixingDays,
58
                             const ext::shared_ptr<IborIndex>& index,
59
                             Real gearing = 1.0,
60
                             Rate couponSpread = 0.0,
61
                             Rate rateSpread = 0.0,
62
                             const Date& refPeriodStart = Date(),
63
                             const Date& refPeriodEnd = Date(),
64
                             const DayCounter& dayCounter = DayCounter(),
65
                             const Date& exCouponDate = Date());
66
67
        /*! \deprecated Use the other constructor.
68
                        Deprecated in version 1.37.
69
        */
70
        [[deprecated("Use the other constructor")]]
71
        MultipleResetsCoupon(const Date& paymentDate,
72
                             Real nominal,
73
                             const Date& startDate,
74
                             const Date& endDate,
75
                             Natural fixingDays,
76
                             const ext::shared_ptr<IborIndex>& index,
77
                             Real gearing = 1.0,
78
                             Rate couponSpread = 0.0,
79
                             Rate rateSpread = 0.0,
80
                             const Date& refPeriodStart = Date(),
81
                             const Date& refPeriodEnd = Date(),
82
                             const DayCounter& dayCounter = DayCounter(),
83
                             const Date& exCouponDate = Date());
84
85
        //! \name Inspectors
86
        //@{
87
        //! fixing dates for the rates to be compounded
88
0
        const std::vector<Date>& fixingDates() const { return fixingDates_; }
89
        //! accrual (compounding) periods
90
0
        const std::vector<Time>& dt() const { return dt_; }
91
        //! value dates for the rates to be compounded
92
0
        const std::vector<Date>& valueDates() const { return valueDates_; }
93
        //! rate spread
94
0
        Spread rateSpread() const { return rateSpread_; }
95
        //@}
96
        //! \name FloatingRateCoupon interface
97
        //@{
98
        //! the date when the coupon is fully determined
99
0
        Date fixingDate() const override { return fixingDates_.back(); }
100
        //@}
101
        //! \name Visitability
102
        //@{
103
        void accept(AcyclicVisitor&) override;
104
        //@}
105
      private:
106
        Date fixingDate(const Date& valueDate) const;
107
108
        std::vector<Date> valueDates_, fixingDates_;
109
        Size n_;
110
        std::vector<Time> dt_;
111
        Rate rateSpread_;
112
    };
113
114
    /*! \deprecated Renamed to MultipleResetsCoupon.
115
                    Deprecated in version 1.37.
116
    */
117
    [[deprecated("Renamed to MultipleResetsCoupon")]]
118
    typedef MultipleResetsCoupon SubPeriodsCoupon;
119
120
121
    class MultipleResetsPricer: public FloatingRateCouponPricer {
122
      public:
123
        Rate swapletPrice() const override;
124
        Real capletPrice(Rate effectiveCap) const override;
125
        Rate capletRate(Rate effectiveCap) const override;
126
        Real floorletPrice(Rate effectiveFloor) const override;
127
        Rate floorletRate(Rate effectiveFloor) const override;
128
        void initialize(const FloatingRateCoupon& coupon) override;
129
130
      protected:
131
        const MultipleResetsCoupon* coupon_;
132
        std::vector<Real> subPeriodFixings_;
133
    };
134
135
    /*! \deprecated Renamed to MultipleResetsPricer.
136
                    Deprecated in version 1.37.
137
    */
138
    [[deprecated("Renamed to MultipleResetsPricer")]]
139
    typedef MultipleResetsPricer SubPeriodsPricer;
140
141
    class AveragingMultipleResetsPricer: public MultipleResetsPricer {
142
      public:
143
        Real swapletRate() const override;
144
    };
145
146
    /*! \deprecated Renamed to AveragingMultipleResetsPricer.
147
                    Deprecated in version 1.37.
148
    */
149
    [[deprecated("Renamed to AveragingMultipleResetsPricer")]]
150
    typedef AveragingMultipleResetsPricer AveragingRatePricer;
151
152
    class CompoundingMultipleResetsPricer: public MultipleResetsPricer {
153
      public:
154
        Real swapletRate() const override;
155
    };
156
157
    /*! \deprecated Renamed to CompoundingMultipleResetsPricer.
158
                    Deprecated in version 1.37.
159
    */
160
    [[deprecated("Renamed to CompoundingMultipleResetsPricer")]]
161
    typedef CompoundingMultipleResetsPricer CompoundingRatePricer;
162
163
164
    //! helper class building a sequence of multiple-reset coupons
165
    class MultipleResetsLeg {
166
      public:
167
        /*! \param fullResetSchedule the full schedule specifying reset periods for all coupons.
168
            \param index             the index whose fixings will be used; it should have the
169
                                     same tenor as the resets.
170
            \param resetsPerCoupon   the number of resets for each coupon; the number of periods
171
                                     in the schedule should be divided exactly by this number.
172
        */
173
        MultipleResetsLeg(Schedule fullResetSchedule,
174
                          ext::shared_ptr<IborIndex> index,
175
                          Size resetsPerCoupon);
176
        MultipleResetsLeg& withNotionals(Real notional);
177
        MultipleResetsLeg& withNotionals(const std::vector<Real>& notionals);
178
        MultipleResetsLeg& withPaymentDayCounter(const DayCounter&);
179
        MultipleResetsLeg& withPaymentAdjustment(BusinessDayConvention);
180
        MultipleResetsLeg& withPaymentCalendar(const Calendar&);
181
        MultipleResetsLeg& withPaymentLag(Integer lag);
182
        MultipleResetsLeg& withFixingDays(Natural fixingDays);
183
        MultipleResetsLeg& withFixingDays(const std::vector<Natural>& fixingDays);
184
        MultipleResetsLeg& withGearings(Real gearing);
185
        MultipleResetsLeg& withGearings(const std::vector<Real>& gearings);
186
        MultipleResetsLeg& withCouponSpreads(Spread spread);
187
        MultipleResetsLeg& withCouponSpreads(const std::vector<Spread>& spreads);
188
        MultipleResetsLeg& withRateSpreads(Spread spread);
189
        MultipleResetsLeg& withRateSpreads(const std::vector<Spread>& spreads);
190
        MultipleResetsLeg& withExCouponPeriod(const Period&,
191
                                              const Calendar&,
192
                                              BusinessDayConvention,
193
                                              bool endOfMonth = false);
194
        MultipleResetsLeg& withAveragingMethod(RateAveraging::Type averagingMethod);
195
        operator Leg() const;
196
197
      private:
198
        Schedule schedule_;
199
        ext::shared_ptr<IborIndex> index_;
200
        Size resetsPerCoupon_;
201
        std::vector<Real> notionals_;
202
        DayCounter paymentDayCounter_;
203
        Calendar paymentCalendar_;
204
        BusinessDayConvention paymentAdjustment_ = Following;
205
        Integer paymentLag_ = 0;
206
        std::vector<Natural> fixingDays_;
207
        std::vector<Real> gearings_;
208
        std::vector<Spread> couponSpreads_;
209
        std::vector<Spread> rateSpreads_;
210
        RateAveraging::Type averagingMethod_ = RateAveraging::Compound;
211
        Period exCouponPeriod_;
212
        Calendar exCouponCalendar_;
213
        BusinessDayConvention exCouponAdjustment_ = Unadjusted;
214
        bool exCouponEndOfMonth_ = false;
215
    };
216
217
218
    /*! \deprecated Use MultipleResetsLeg instead.
219
                    Deprecated in version 1.37.
220
    */
221
    class [[deprecated("Use MultipleResetsLeg instead")]] SubPeriodsLeg {
222
      public:
223
        SubPeriodsLeg(Schedule schedule, ext::shared_ptr<IborIndex> index);
224
        SubPeriodsLeg& withNotionals(Real notional);
225
        SubPeriodsLeg& withNotionals(const std::vector<Real>& notionals);
226
        SubPeriodsLeg& withPaymentDayCounter(const DayCounter&);
227
        SubPeriodsLeg& withPaymentAdjustment(BusinessDayConvention);
228
        SubPeriodsLeg& withPaymentCalendar(const Calendar&);
229
        SubPeriodsLeg& withPaymentLag(Integer lag);
230
        SubPeriodsLeg& withFixingDays(Natural fixingDays);
231
        SubPeriodsLeg& withFixingDays(const std::vector<Natural>& fixingDays);
232
        SubPeriodsLeg& withGearings(Real gearing);
233
        SubPeriodsLeg& withGearings(const std::vector<Real>& gearings);
234
        SubPeriodsLeg& withCouponSpreads(Spread spread);
235
        SubPeriodsLeg& withCouponSpreads(const std::vector<Spread>& spreads);
236
        SubPeriodsLeg& withRateSpreads(Spread spread);
237
        SubPeriodsLeg& withRateSpreads(const std::vector<Spread>& spreads);
238
        SubPeriodsLeg& withExCouponPeriod(const Period&,
239
                                          const Calendar&,
240
                                          BusinessDayConvention,
241
                                          bool endOfMonth = false);
242
        SubPeriodsLeg& withAveragingMethod(RateAveraging::Type averagingMethod);
243
        operator Leg() const;
244
245
      private:
246
        Schedule schedule_;
247
        ext::shared_ptr<IborIndex> index_;
248
        std::vector<Real> notionals_;
249
        DayCounter paymentDayCounter_;
250
        Calendar paymentCalendar_;
251
        BusinessDayConvention paymentAdjustment_ = Following;
252
        Integer paymentLag_ = 0;
253
        std::vector<Natural> fixingDays_;
254
        std::vector<Real> gearings_;
255
        std::vector<Spread> couponSpreads_;
256
        std::vector<Spread> rateSpreads_;
257
        RateAveraging::Type averagingMethod_ = RateAveraging::Compound;
258
        Period exCouponPeriod_;
259
        Calendar exCouponCalendar_;
260
        BusinessDayConvention exCouponAdjustment_ = Unadjusted;
261
        bool exCouponEndOfMonth_ = false;
262
    };
263
}
264
265
#endif