1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007, 2009, 2011 Chris Kenyon
5 Copyright (C) 2009 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 <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#include <ql/cashflows/cashflows.hpp>
22#include <ql/cashflows/cashflowvectors.hpp>
23#include <ql/cashflows/couponpricer.hpp>
24#include <ql/cashflows/cpicoupon.hpp>
25#include <ql/cashflows/fixedratecoupon.hpp>
26#include <ql/cashflows/iborcoupon.hpp>
27#include <ql/cashflows/simplecashflow.hpp>
28#include <ql/indexes/inflationindex.hpp>
29#include <ql/instruments/cpiswap.hpp>
30#include <ql/termstructures/yieldtermstructure.hpp>
31#include <ql/time/schedule.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 // accrual adjustment is already in the schedules, as are calendars
37 CPISwap::CPISwap(Type type,
38 Real nominal,
39 bool subtractInflationNominal,
40 // float + spread leg
41 Spread spread,
42 DayCounter floatDayCount,
43 Schedule floatSchedule,
44 const BusinessDayConvention& floatPaymentRoll,
45 Natural fixingDays,
46 ext::shared_ptr<IborIndex> floatIndex,
47 // fixed x inflation leg
48 Rate fixedRate,
49 Real baseCPI,
50 DayCounter fixedDayCount,
51 Schedule fixedSchedule,
52 const BusinessDayConvention& fixedPaymentRoll,
53 const Period& observationLag,
54 ext::shared_ptr<ZeroInflationIndex> fixedIndex,
55 CPI::InterpolationType observationInterpolation,
56 Real inflationNominal)
57 : Swap(2), type_(type), nominal_(nominal), subtractInflationNominal_(subtractInflationNominal),
58 spread_(spread), floatDayCount_(std::move(floatDayCount)),
59 floatSchedule_(std::move(floatSchedule)), floatPaymentRoll_(floatPaymentRoll),
60 fixingDays_(fixingDays), floatIndex_(std::move(floatIndex)), fixedRate_(fixedRate),
61 baseCPI_(baseCPI), fixedDayCount_(std::move(fixedDayCount)),
62 fixedSchedule_(std::move(fixedSchedule)), fixedPaymentRoll_(fixedPaymentRoll),
63 fixedIndex_(std::move(fixedIndex)), observationLag_(observationLag),
64 observationInterpolation_(observationInterpolation) {
65 QL_REQUIRE(!floatSchedule_.empty(), "empty float schedule");
66 QL_REQUIRE(!fixedSchedule_.empty(), "empty fixed schedule");
67 // \todo if roll!=unadjusted then need calendars ...
68
69 if (inflationNominal==Null<Real>()) inflationNominal_ = nominal_;
70 else inflationNominal_ = inflationNominal;
71
72 Leg floatingLeg;
73 if (floatSchedule_.size() > 1) {
74 floatingLeg = IborLeg(floatSchedule_, floatIndex_)
75 .withNotionals(notional: nominal_)
76 .withSpreads(spread: spread_)
77 .withPaymentDayCounter(floatDayCount_)
78 .withPaymentAdjustment(floatPaymentRoll_)
79 .withFixingDays(fixingDays: fixingDays_);
80 }
81
82 if (floatSchedule_.size()==1 ||
83 !subtractInflationNominal_ ||
84 (subtractInflationNominal && std::fabs(x: nominal_-inflationNominal_)>0.00001)
85 )
86 {
87 Date payNotional;
88 if (floatSchedule_.size()==1) { // no coupons
89 payNotional = floatSchedule_[0];
90 payNotional = floatSchedule_.calendar().adjust(payNotional, convention: floatPaymentRoll_);
91 } else { // use the pay date of the last coupon
92 payNotional = floatingLeg.back()->date();
93 }
94
95 Real floatAmount = subtractInflationNominal_ ? nominal_ - inflationNominal_ : nominal_;
96 ext::shared_ptr<CashFlow> nf(new SimpleCashFlow(floatAmount, payNotional));
97 floatingLeg.push_back(x: nf);
98 }
99
100 // a CPIleg know about zero legs and inclusion of base inflation notional
101 Leg cpiLeg = CPILeg(fixedSchedule_, fixedIndex_,
102 baseCPI_, observationLag_)
103 .withNotionals(notional: inflationNominal_)
104 .withFixedRates(fixedRate: fixedRate_)
105 .withPaymentDayCounter(fixedDayCount_)
106 .withPaymentAdjustment(fixedPaymentRoll_)
107 .withObservationInterpolation(observationInterpolation_)
108 .withSubtractInflationNominal(subtractInflationNominal_);
109
110
111 Leg::const_iterator i;
112 for (i = cpiLeg.begin(); i < cpiLeg.end(); ++i) {
113 registerWith(h: *i);
114 }
115
116 for (i = floatingLeg.begin(); i < floatingLeg.end(); ++i) {
117 registerWith(h: *i);
118 }
119
120 legs_[0] = cpiLeg;
121 legs_[1] = floatingLeg;
122
123 if (type_==Payer) {
124 payer_[0] = 1.0;
125 payer_[1] = -1.0;
126 } else {
127 payer_[0] = -1.0;
128 payer_[1] = 1.0;
129 }
130 }
131
132
133 //! for simple case sufficient to copy base class
134 void CPISwap::setupArguments(PricingEngine::arguments* args) const {
135
136 Swap::setupArguments(args);
137
138 auto* arguments = dynamic_cast<CPISwap::arguments*>(args);
139
140 if (arguments == nullptr)
141 return; // it's a swap engine...
142 }
143
144
145 Rate CPISwap::fairRate() const {
146 calculate();
147 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
148 return fairRate_;
149 }
150
151 Spread CPISwap::fairSpread() const {
152 calculate();
153 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
154 return fairSpread_;
155 }
156
157
158 Real CPISwap::fixedLegNPV() const {//FIXME
159 calculate();
160 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
161 return legNPV_[0];
162 }
163
164 Real CPISwap::floatLegNPV() const {//FIXME
165 calculate();
166 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
167 return legNPV_[1];
168 }
169
170 void CPISwap::setupExpired() const {
171 Swap::setupExpired();
172 legBPS_[0] = legBPS_[1] = 0.0;
173 fairRate_ = Null<Rate>();
174 fairSpread_ = Null<Spread>();
175 }
176
177 void CPISwap::fetchResults(const PricingEngine::results* r) const {
178 static const Spread basisPoint = 1.0e-4;
179
180 // copy from VanillaSwap
181 // works because similarly simple instrument
182 // that we always expect to be priced with a swap engine
183
184 Swap::fetchResults(r);
185
186 const auto* results = dynamic_cast<const CPISwap::results*>(r);
187 if (results != nullptr) { // might be a swap engine, so no error is thrown
188 fairRate_ = results->fairRate;
189 fairSpread_ = results->fairSpread;
190 } else {
191 fairRate_ = Null<Rate>();
192 fairSpread_ = Null<Spread>();
193 }
194
195 if (fairRate_ == Null<Rate>()) {
196 // calculate it from other results
197 if (legBPS_[0] != Null<Real>())
198 fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
199 }
200 if (fairSpread_ == Null<Spread>()) {
201 // ditto
202 if (legBPS_[1] != Null<Real>())
203 fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
204 }
205
206 }
207
208 void CPISwap::arguments::validate() const {
209 Swap::arguments::validate();
210 }
211
212 void CPISwap::results::reset() {
213 Swap::results::reset();
214 fairRate = Null<Rate>();
215 fairSpread = Null<Spread>();
216 }
217
218}
219
220

source code of quantlib/ql/instruments/cpiswap.cpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.