Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/makeyoyinflationcapfloor.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2006, 2007 Ferdinando Ametrano
5
 Copyright (C) 2007 StatPro Italia srl
6
 Copyright (C) 2009 Chris Kenyon
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
 */
21
22
#include <ql/cashflows/cashflows.hpp>
23
#include <ql/instruments/makeyoyinflationcapfloor.hpp>
24
#include <ql/time/daycounters/thirty360.hpp>
25
#include <utility>
26
27
namespace QuantLib {
28
29
    MakeYoYInflationCapFloor::MakeYoYInflationCapFloor(YoYInflationCapFloor::Type capFloorType,
30
                                                       ext::shared_ptr<YoYInflationIndex> index,
31
                                                       const Size& length,
32
                                                       Calendar cal,
33
                                                       const Period& observationLag,
34
                                                       CPI::InterpolationType interpolation)
35
0
    : capFloorType_(capFloorType), length_(length), calendar_(std::move(cal)),
36
0
      index_(std::move(index)), observationLag_(observationLag),
37
0
      interpolation_(interpolation), strike_(Null<Rate>()),
38
39
0
      dayCounter_(Thirty360(Thirty360::BondBasis)) {}
40
41
0
    MakeYoYInflationCapFloor::operator YoYInflationCapFloor() const {
42
0
        ext::shared_ptr<YoYInflationCapFloor> capfloor = *this;
43
0
        return *capfloor;
44
0
    }
45
46
0
    MakeYoYInflationCapFloor::operator ext::shared_ptr<YoYInflationCapFloor>() const {
47
48
0
        Date startDate;
49
0
        if (effectiveDate_ != Date()) {
50
0
            startDate = effectiveDate_;
51
0
        } else {
52
0
            Date referenceDate = Settings::instance().evaluationDate();
53
0
            Date spotDate = calendar_.advance(referenceDate,
54
0
                                              fixingDays_*Days);
55
0
            startDate = spotDate+forwardStart_;
56
0
        }
57
58
0
        Date endDate = calendar_.advance(startDate,length_*Years,Unadjusted);
59
0
        Schedule schedule(startDate, endDate, Period(Annual), calendar_,
60
0
                          Unadjusted, Unadjusted, // ref periods & acc periods
61
0
                          DateGeneration::Forward, false);
62
0
        Leg leg = yoyInflationLeg(schedule, calendar_, index_,
63
0
                                  observationLag_, interpolation_)
64
0
        .withPaymentAdjustment(roll_)
65
0
        .withPaymentDayCounter(dayCounter_)
66
0
        .withNotionals(nominal_)
67
0
        ;
68
69
0
        if (firstCapletExcluded_)
70
0
            leg.erase(leg.begin());
71
72
        // only leaves the last coupon
73
0
        if (asOptionlet_ && leg.size() > 1) {
74
0
            auto end = leg.end(); // Sun Studio needs an lvalue
75
0
            leg.erase(leg.begin(), --end);
76
0
        }
77
78
0
        std::vector<Rate> strikeVector(1, strike_);
79
0
        if (strike_ == Null<Rate>()) {
80
            // ATM on the forecasting curve
81
0
            strikeVector[0] = CashFlows::atmRate(leg, **nominalTermStructure_,
82
0
                                                 false, nominalTermStructure_->referenceDate());
83
0
        }
84
85
0
        ext::shared_ptr<YoYInflationCapFloor> capFloor(new
86
0
                    YoYInflationCapFloor(capFloorType_, leg, strikeVector));
87
0
        capFloor->setPricingEngine(engine_);
88
0
        return capFloor;
89
0
    }
90
91
0
    MakeYoYInflationCapFloor& MakeYoYInflationCapFloor::withNominal(Real n) {
92
0
        nominal_ = n;
93
0
        return *this;
94
0
    }
95
96
    MakeYoYInflationCapFloor& MakeYoYInflationCapFloor::withEffectiveDate(
97
0
                                            const Date& effectiveDate) {
98
0
        effectiveDate_ = effectiveDate;
99
0
        return *this;
100
0
    }
101
102
    MakeYoYInflationCapFloor&
103
0
    MakeYoYInflationCapFloor::withPaymentAdjustment(BusinessDayConvention bdc) {
104
0
        roll_ = bdc;
105
0
        return *this;
106
0
    }
107
108
    MakeYoYInflationCapFloor&
109
0
    MakeYoYInflationCapFloor::withPaymentDayCounter(const DayCounter& dc) {
110
0
        dayCounter_ = dc;
111
0
        return *this;
112
0
    }
113
114
    MakeYoYInflationCapFloor&
115
0
    MakeYoYInflationCapFloor::withFixingDays(Natural n) {
116
0
        fixingDays_ = n;
117
0
        return *this;
118
0
    }
119
120
0
    MakeYoYInflationCapFloor& MakeYoYInflationCapFloor::asOptionlet(bool b) {
121
0
        asOptionlet_ = b;
122
0
        return *this;
123
0
    }
124
125
    MakeYoYInflationCapFloor& MakeYoYInflationCapFloor::withPricingEngine(
126
0
        const ext::shared_ptr<PricingEngine>& engine) {
127
0
        engine_ = engine;
128
0
        return *this;
129
0
    }
130
131
    MakeYoYInflationCapFloor&
132
0
    MakeYoYInflationCapFloor::withStrike(Rate strike) {
133
0
        QL_REQUIRE(nominalTermStructure_.empty(), "ATM strike already given");
134
0
        strike_ = strike;
135
0
        return *this;
136
0
    }
137
138
    MakeYoYInflationCapFloor&
139
    MakeYoYInflationCapFloor::withAtmStrike(
140
0
                      const Handle<YieldTermStructure>& nominalTermStructure) {
141
0
        QL_REQUIRE(strike_ == Null<Rate>(), "explicit strike already given");
142
0
        nominalTermStructure_ = nominalTermStructure;
143
0
        return *this;
144
0
    }
145
146
    MakeYoYInflationCapFloor&
147
0
    MakeYoYInflationCapFloor::withForwardStart(Period forwardStart) {
148
0
        forwardStart_ = forwardStart;
149
0
        return *this;
150
0
    }
151
152
}
153