QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
capflooredinflationcoupon.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Chris Kenyon
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 <http://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/cashflows/capflooredinflationcoupon.hpp>
21#include <ql/cashflows/inflationcouponpricer.hpp>
22
23namespace QuantLib {
24
26 Rate cap, Rate floor) {
27
28 isCapped_ = false;
29 isFloored_ = false;
30
31 if (gearing_ > 0) {
32 if (cap != Null<Rate>()) {
33 isCapped_ = true;
34 cap_ = cap;
35 }
36 if (floor != Null<Rate>()) {
37 floor_ = floor;
38 isFloored_ = true;
39 }
40 } else {
41 if (cap != Null<Rate>()){
42 floor_ = cap;
43 isFloored_ = true;
44 }
45 if (floor != Null<Rate>()){
46 isCapped_ = true;
47 cap_ = floor;
48 }
49 }
50 if (isCapped_ && isFloored_) {
51 QL_REQUIRE(cap >= floor, "cap level (" << cap <<
52 ") less than floor level (" << floor << ")");
53 }
54 }
55
56
58 const ext::shared_ptr<YoYInflationCoupon>& underlying,
59 Rate cap, Rate floor)
60 : YoYInflationCoupon(underlying->date(),
61 underlying->nominal(),
62 underlying->accrualStartDate(),
63 underlying->accrualEndDate(),
64 underlying->fixingDays(),
65 underlying->yoyIndex(),
66 underlying->observationLag(),
67 underlying->dayCounter(),
68 underlying->gearing(),
69 underlying->spread(),
70 underlying->referencePeriodStart(),
71 underlying->referencePeriodEnd()),
72 underlying_(underlying), isFloored_(false), isCapped_(false) {
73 QL_DEPRECATED_DISABLE_WARNING
75 QL_DEPRECATED_ENABLE_WARNING
76 registerWith(underlying);
77 }
78
79
81 const ext::shared_ptr<YoYInflationCouponPricer>& pricer) {
82
84 if (underlying_ != nullptr)
85 underlying_->setPricer(pricer);
86 }
87
88
90 return underlying_ != nullptr ? underlying_->rate() : YoYInflationCoupon::rate();
91 }
92
94 Rate swapletRate = underlyingRate();
95
96 auto couponPricer = underlying_ != nullptr ? underlying_->pricer() : pricer();
97
98 if (isFloored_ || isCapped_) {
99 QL_REQUIRE(couponPricer, "pricer not set");
100 }
101
102 Rate floorletRate = isFloored_ ? couponPricer->floorletRate(effectiveFloor()) : 0.0;
103 Rate capletRate = isCapped_? couponPricer->capletRate(effectiveCap()) : 0.0;
104
105 return swapletRate + floorletRate - capletRate;
106 }
107
108
110 if ( (gearing_ > 0) && isCapped_)
111 return cap_;
112 if ( (gearing_ < 0) && isFloored_)
113 return floor_;
114 return Null<Rate>();
115 }
116
117
119 if ( (gearing_ > 0) && isFloored_)
120 return floor_;
121 if ( (gearing_ < 0) && isCapped_)
122 return cap_;
123 return Null<Rate>();
124 }
125
126
128 return (cap_ - spread())/gearing();
129 }
130
131
133 return (floor_ - spread())/gearing();
134 }
135
136
139 }
140
141
143 typedef YoYInflationCoupon super;
144 auto* v1 = dynamic_cast<Visitor<CappedFlooredYoYInflationCoupon>*>(&v);
145
146 if (v1 != nullptr)
147 v1->visit(*this);
148 else
149 super::accept(v);
150 }
151
152}
153
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
CappedFlooredYoYInflationCoupon(const ext::shared_ptr< YoYInflationCoupon > &underlying, Rate cap=Null< Rate >(), Rate floor=Null< Rate >())
ext::shared_ptr< YoYInflationCoupon > underlying_
virtual QL_DEPRECATED void setCommon(Rate cap, Rate floor)
void setPricer(const ext::shared_ptr< YoYInflationCouponPricer > &)
Rate effectiveCap() const
effective cap of fixing
Rate underlyingRate() const
this returns the expected rate before cap and floor are applied
Rate effectiveFloor() const
effective floor of fixing
virtual Rate rate() const =0
accrued rate
ext::shared_ptr< InflationCouponPricer > pricer() const
void setPricer(const ext::shared_ptr< InflationCouponPricer > &)
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Coupon paying a YoY-inflation type index
Real gearing() const
index gearing, i.e. multiplicative coefficient for the index
Spread spread() const
spread paid over the fixing of the underlying index
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35