• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 26158115200

20 May 2026 10:57AM UTC coverage: 74.637% (-0.001%) from 74.638%
26158115200

Pull #2593

github

web-flow
Merge a81da9201 into 8d6014303
Pull Request #2593: Fix yield term structure time error message

1 of 1 new or added line in 1 file covered. (100.0%)

25 existing lines in 1 file now uncovered.

58971 of 79010 relevant lines covered (74.64%)

8614426.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.66
/ql/instruments/makecapfloor.cpp
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

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
 <https://www.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
#include "ql/errors.hpp"
22
#include <ql/instruments/makecapfloor.hpp>
23
#include <ql/cashflows/cashflows.hpp>
24
#include <ql/pricingengines/capfloor/blackcapfloorengine.hpp>
25
#include <ql/pricingengines/capfloor/bacheliercapfloorengine.hpp>
26

27
namespace QuantLib {
28

29
    MakeCapFloor::MakeCapFloor(CapFloor::Type capFloorType,
7,840✔
30
                               const Period& tenor,
31
                               const ext::shared_ptr<IborIndex>& iborIndex,
32
                               Rate strike,
33
                               const Period& forwardStart)
7,840✔
34
    : capFloorType_(capFloorType), strike_(strike), firstCapletExcluded_(forwardStart == 0 * Days),
7,840✔
35
      // setting the fixed leg tenor avoids that MakeVanillaSwap throws
36
      // because of an unknown fixed leg default tenor for a currency,
37
      // notice that only the floating leg of the swap is used anyway
38
      makeVanillaSwap_(MakeVanillaSwap(tenor, iborIndex, 0.0, forwardStart)
15,680✔
39
                           .withFixedLegTenor(1 * Years)
7,840✔
40
                           .withFixedLegDayCount(Actual365Fixed())) {}
23,520✔
41

42
    MakeCapFloor::operator CapFloor() const {
575✔
43
        ext::shared_ptr<CapFloor> capfloor = *this;
575✔
44
        return *capfloor;
1,150✔
45
    }
46

47
    MakeCapFloor::operator ext::shared_ptr<CapFloor>() const {
7,840✔
48

49
        VanillaSwap swap = makeVanillaSwap_;
7,840✔
50

51
        Leg leg = swap.floatingLeg();
7,840✔
52
        if (firstCapletExcluded_)
7,840✔
53
            leg.erase(leg.begin());
54

55
        // only leaves the last coupon
56
        if (asOptionlet_ && leg.size() > 1) {
7,840✔
57
            auto end = leg.end(); // Sun Studio needs an lvalue
58
            leg.erase(leg.begin(), --end);
59
        }
60

61
        std::vector<Rate> strikeVector(1, strike_);
7,840✔
62
        if (strike_ == Null<Rate>()) {
7,840✔
63

64
            // temporary patch...
65
            // should be fixed for every CapFloor::Engine
66
            Handle<YieldTermStructure> discountCurve;
26✔
67

68
            ext::shared_ptr<BlackCapFloorEngine> blackCapTemp = 
69
                ext::dynamic_pointer_cast<BlackCapFloorEngine>(engine_);
26✔
70
            ext::shared_ptr<BachelierCapFloorEngine> bachelierCapTemp = 
71
                ext::dynamic_pointer_cast<BachelierCapFloorEngine>(engine_);
26✔
72
            if (blackCapTemp)
26✔
73
            {
74
                discountCurve = blackCapTemp->termStructure();
52✔
75
            }
UNCOV
76
            else if(bachelierCapTemp)
×
77
            {
UNCOV
78
                discountCurve = bachelierCapTemp->termStructure();
×
79
            }
80
            else 
81
            {
82
                QL_FAIL("cannot calculate ATM without a BlackCapFloorEngine or BachelierCapFloorEngine");
×
83
            }
84

85
            strikeVector[0] = CashFlows::atmRate(leg,
52✔
86
                                                 **discountCurve,
26✔
87
                                                 false,
88
                                                 discountCurve->referenceDate());
26✔
89
        }
90

91
        ext::shared_ptr<CapFloor> capFloor(new
92
            CapFloor(capFloorType_, leg, strikeVector));
7,840✔
93
        capFloor->setPricingEngine(engine_);
7,840✔
94
        return capFloor;
7,840✔
95
    }
7,840✔
96

UNCOV
97
    MakeCapFloor& MakeCapFloor::withNominal(Real n) {
×
UNCOV
98
        makeVanillaSwap_.withNominal(n);
×
99
        return *this;
×
100
    }
101

UNCOV
102
    MakeCapFloor& MakeCapFloor::withEffectiveDate(const Date& effectiveDate,
×
103
                                                  bool firstCapletExcluded) {
UNCOV
104
        makeVanillaSwap_.withEffectiveDate(effectiveDate);
×
105
        firstCapletExcluded_ = firstCapletExcluded;
×
106
        return *this;
×
107
    }
108

UNCOV
109
    MakeCapFloor& MakeCapFloor::withTenor(const Period& t) {
×
UNCOV
110
        makeVanillaSwap_.withFloatingLegTenor(t);
×
UNCOV
111
        return *this;
×
112
    }
113

114

UNCOV
115
    MakeCapFloor& MakeCapFloor::withCalendar(const Calendar& cal) {
×
UNCOV
116
        makeVanillaSwap_.withFloatingLegCalendar(cal);
×
UNCOV
117
        return *this;
×
118
    }
119

120

UNCOV
121
    MakeCapFloor& MakeCapFloor::withConvention(BusinessDayConvention bdc) {
×
UNCOV
122
        makeVanillaSwap_.withFloatingLegConvention(bdc);
×
123
        return *this;
×
124
    }
125

126

127
    MakeCapFloor&
UNCOV
128
    MakeCapFloor::withTerminationDateConvention(BusinessDayConvention bdc) {
×
129
        makeVanillaSwap_.withFloatingLegTerminationDateConvention(bdc);
×
130
        return *this;
×
131
    }
132

133

134
    MakeCapFloor& MakeCapFloor::withRule(DateGeneration::Rule r) {
×
135
        makeVanillaSwap_.withFloatingLegRule(r);
×
136
        return *this;
×
137
    }
138

139
    MakeCapFloor& MakeCapFloor::withEndOfMonth(bool flag) {
×
140
        makeVanillaSwap_.withFloatingLegEndOfMonth(flag);
×
141
        return *this;
×
142
    }
143

144

145
    MakeCapFloor& MakeCapFloor::withFirstDate(const Date& d) {
×
146
        makeVanillaSwap_.withFloatingLegFirstDate(d);
×
UNCOV
147
        return *this;
×
148
    }
149

UNCOV
150
    MakeCapFloor& MakeCapFloor::withNextToLastDate(const Date& d) {
×
UNCOV
151
        makeVanillaSwap_.withFloatingLegNextToLastDate(d);
×
UNCOV
152
        return *this;
×
153
    }
154

UNCOV
155
    MakeCapFloor& MakeCapFloor::withDayCount(const DayCounter& dc) {
×
UNCOV
156
        makeVanillaSwap_.withFloatingLegDayCount(dc);
×
UNCOV
157
        return *this;
×
158
    }
159

UNCOV
160
    MakeCapFloor& MakeCapFloor::asOptionlet(bool b) {
×
UNCOV
161
        asOptionlet_ = b;
×
UNCOV
162
        return *this;
×
163
    }
164

165
    MakeCapFloor& MakeCapFloor::withPricingEngine(
7,814✔
166
                             const ext::shared_ptr<PricingEngine>& engine) {
167
        engine_ = engine;
7,814✔
168
        return *this;
7,814✔
169
    }
170

171
}
172

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc