Coverage Report

Created: 2026-06-23 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/cashflows/iborcoupon.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2007, 2011 Ferdinando Ametrano
5
 Copyright (C) 2007 Giorgio Facchinetti
6
 Copyright (C) 2007 Cristina Duminuco
7
 Copyright (C) 2007 StatPro Italia srl
8
 Copyright (C) 2017 Joseph Jeisman
9
 Copyright (C) 2017 Fabrice Lecuyer
10
11
 This file is part of QuantLib, a free-software/open-source library
12
 for financial quantitative analysts and developers - http://quantlib.org/
13
14
 QuantLib is free software: you can redistribute it and/or modify it
15
 under the terms of the QuantLib license.  You should have received a
16
 copy of the license along with this program; if not, please email
17
 <quantlib-dev@lists.sf.net>. The license is also available online at
18
 <https://www.quantlib.org/license.shtml>.
19
20
 This program is distributed in the hope that it will be useful, but WITHOUT
21
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22
 FOR A PARTICULAR PURPOSE.  See the license for more details.
23
*/
24
25
/*! \file iborcoupon.hpp
26
    \brief Coupon paying a Libor-type index
27
*/
28
29
#ifndef quantlib_ibor_coupon_hpp
30
#define quantlib_ibor_coupon_hpp
31
32
#include <ql/cashflows/floatingratecoupon.hpp>
33
#include <ql/indexes/iborindex.hpp>
34
#include <ql/patterns/singleton.hpp>
35
#include <ql/time/schedule.hpp>
36
#include <ql/optional.hpp>
37
38
namespace QuantLib {
39
40
    //! %Coupon paying a Libor-type index
41
    class IborCoupon : public FloatingRateCoupon {
42
      public:
43
        IborCoupon(const Date& paymentDate,
44
                   Real nominal,
45
                   const Date& startDate,
46
                   const Date& endDate,
47
                   Natural fixingDays,
48
                   const ext::shared_ptr<IborIndex>& index,
49
                   Real gearing = 1.0,
50
                   Spread spread = 0.0,
51
                   const Date& refPeriodStart = Date(),
52
                   const Date& refPeriodEnd = Date(),
53
                   const DayCounter& dayCounter = DayCounter(),
54
                   bool isInArrears = false,
55
                   const Date& exCouponDate = Date(),
56
                   BusinessDayConvention fixingConvention = Preceding);
57
        //! \name Inspectors
58
        //@{
59
0
        const ext::shared_ptr<IborIndex>& iborIndex() const { return iborIndex_; }
60
        bool hasFixed() const;
61
        //@}
62
        //! \name FloatingRateCoupon interface
63
        //@{
64
        Date fixingDate() const override;
65
        // implemented in order to manage the case of par coupon
66
        Rate indexFixing() const override;
67
        void setPricer(const ext::shared_ptr<FloatingRateCouponPricer>&) override;
68
        //@}
69
        //! \name Visitability
70
        //@{
71
        void accept(AcyclicVisitor&) override;
72
        //@}
73
        /*! \name Internal calculations
74
75
            You won't probably need these methods unless you're implementing
76
            a coupon pricer.
77
        */
78
        //@{
79
        //! Start of the deposit period underlying the index fixing
80
        const Date& fixingValueDate() const;
81
        //! End of the deposit period underlying the index fixing
82
        const Date& fixingMaturityDate() const;
83
        //! End of the deposit period underlying the coupon fixing
84
        /*! This might be not the same as fixingMaturityDate if par coupons are used. */
85
        const Date& fixingEndDate() const;
86
        //! Period underlying the index fixing, as a year fraction
87
        Time spanningTimeIndexMaturity() const;
88
        //! Period underlying the coupon fixing, as a year fraction
89
        /*! This might be not the same as spanningTimeIndexMaturity if par coupons are used. */
90
        Time spanningTime() const;
91
        //@}
92
93
      private:
94
        friend class IborCouponPricer;
95
        ext::shared_ptr<IborIndex> iborIndex_;
96
        Date fixingDate_;
97
        // computed by coupon pricer (depending on par coupon flag) and stored here
98
        void initializeCachedData() const;
99
        mutable bool cachedDataIsInitialized_ = false;
100
        mutable Date fixingValueDate_, fixingEndDate_, fixingMaturityDate_;
101
        mutable Time spanningTime_, spanningTimeIndexMaturity_;
102
103
      public:
104
        // IborCoupon::Settings forward declaration
105
        class Settings;
106
    };
107
108
109
    //! Per-session settings for IborCoupon class
110
    class IborCoupon::Settings : public Singleton<IborCoupon::Settings> {
111
        friend class Singleton<IborCoupon::Settings>;
112
      private:
113
        Settings() = default;
114
115
      public:
116
        //! When called, IborCoupons are created as indexed coupons instead of par coupons.
117
        void createAtParCoupons();
118
119
        //! When called, IborCoupons are created as par coupons instead of indexed coupons.
120
        void createIndexedCoupons();
121
122
        /*! If true the IborCoupons are created as par coupons and vice versa.
123
            The default depends on the compiler flag QL_USE_INDEXED_COUPON and can be overwritten by
124
            createAtParCoupons() and createIndexedCoupons() */
125
        bool usingAtParCoupons() const;
126
127
      private:
128
        #ifndef QL_USE_INDEXED_COUPON
129
        bool usingAtParCoupons_ = true;
130
        #else
131
        bool usingAtParCoupons_ = false;
132
        #endif
133
    };
134
135
    //! helper class building a sequence of capped/floored ibor-rate coupons
136
    class IborLeg {
137
      public:
138
        IborLeg(Schedule schedule, ext::shared_ptr<IborIndex> index);
139
        IborLeg& withNotionals(Real notional);
140
        IborLeg& withNotionals(const std::vector<Real>& notionals);
141
        IborLeg& withPaymentDayCounter(const DayCounter&);
142
        IborLeg& withPaymentAdjustment(BusinessDayConvention);
143
        IborLeg& withPaymentLag(Integer lag);
144
        IborLeg& withPaymentCalendar(const Calendar&);
145
        IborLeg& withFixingDays(Natural fixingDays);
146
        IborLeg& withFixingDays(const std::vector<Natural>& fixingDays);
147
        IborLeg& withGearings(Real gearing);
148
        IborLeg& withGearings(const std::vector<Real>& gearings);
149
        IborLeg& withSpreads(Spread spread);
150
        IborLeg& withSpreads(const std::vector<Spread>& spreads);
151
        IborLeg& withCaps(Rate cap);
152
        IborLeg& withCaps(const std::vector<Rate>& caps);
153
        IborLeg& withFloors(Rate floor);
154
        IborLeg& withFloors(const std::vector<Rate>& floors);
155
        IborLeg& inArrears(bool flag = true);
156
        IborLeg& withZeroPayments(bool flag = true);
157
        IborLeg& withExCouponPeriod(const Period&,
158
                                    const Calendar&,
159
                                    BusinessDayConvention,
160
                                    bool endOfMonth = false);
161
        IborLeg& withFixingConvention(BusinessDayConvention);
162
        IborLeg& withIndexedCoupons(ext::optional<bool> b = true);
163
        IborLeg& withAtParCoupons(bool b = true);
164
        operator Leg() const;
165
166
      private:
167
        Schedule schedule_;
168
        ext::shared_ptr<IborIndex> index_;
169
        std::vector<Real> notionals_;
170
        DayCounter paymentDayCounter_;
171
        BusinessDayConvention paymentAdjustment_ = Following;
172
        Integer paymentLag_ = 0;
173
        Calendar paymentCalendar_;
174
        std::vector<Natural> fixingDays_;
175
        std::vector<Real> gearings_;
176
        std::vector<Spread> spreads_;
177
        std::vector<Rate> caps_, floors_;
178
        bool inArrears_ = false, zeroPayments_ = false;
179
        BusinessDayConvention fixingConvention_ = Preceding;
180
        Period exCouponPeriod_;
181
        Calendar exCouponCalendar_;
182
        BusinessDayConvention exCouponAdjustment_ = Unadjusted;
183
        bool exCouponEndOfMonth_ = false;
184
        ext::optional<bool> useIndexedCoupons_;
185
    };
186
187
}
188
189
#endif