/src/quantlib/ql/instruments/cpiswap.cpp
Line | Count | Source |
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 | | <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/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 | | |
34 | | namespace 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 | 0 | : Swap(2), type_(type), nominal_(nominal), subtractInflationNominal_(subtractInflationNominal), |
58 | 0 | spread_(spread), floatDayCount_(std::move(floatDayCount)), |
59 | 0 | floatSchedule_(std::move(floatSchedule)), floatPaymentRoll_(floatPaymentRoll), |
60 | 0 | fixingDays_(fixingDays), floatIndex_(std::move(floatIndex)), fixedRate_(fixedRate), |
61 | 0 | baseCPI_(baseCPI), fixedDayCount_(std::move(fixedDayCount)), |
62 | 0 | fixedSchedule_(std::move(fixedSchedule)), fixedPaymentRoll_(fixedPaymentRoll), |
63 | 0 | fixedIndex_(std::move(fixedIndex)), observationLag_(observationLag), |
64 | 0 | observationInterpolation_(observationInterpolation) { |
65 | 0 | QL_REQUIRE(!floatSchedule_.empty(), "empty float schedule"); |
66 | 0 | QL_REQUIRE(!fixedSchedule_.empty(), "empty fixed schedule"); |
67 | | // \todo if roll!=unadjusted then need calendars ... |
68 | | |
69 | 0 | if (inflationNominal==Null<Real>()) inflationNominal_ = nominal_; |
70 | 0 | else inflationNominal_ = inflationNominal; |
71 | |
|
72 | 0 | Leg floatingLeg; |
73 | 0 | if (floatSchedule_.size() > 1) { |
74 | 0 | floatingLeg = IborLeg(floatSchedule_, floatIndex_) |
75 | 0 | .withNotionals(nominal_) |
76 | 0 | .withSpreads(spread_) |
77 | 0 | .withPaymentDayCounter(floatDayCount_) |
78 | 0 | .withPaymentAdjustment(floatPaymentRoll_) |
79 | 0 | .withFixingDays(fixingDays_); |
80 | 0 | } |
81 | |
|
82 | 0 | if (floatSchedule_.size()==1 || |
83 | 0 | !subtractInflationNominal_ || |
84 | 0 | (subtractInflationNominal && std::fabs(nominal_-inflationNominal_)>0.00001) |
85 | 0 | ) |
86 | 0 | { |
87 | 0 | Date payNotional; |
88 | 0 | if (floatSchedule_.size()==1) { // no coupons |
89 | 0 | payNotional = floatSchedule_[0]; |
90 | 0 | payNotional = floatSchedule_.calendar().adjust(payNotional, floatPaymentRoll_); |
91 | 0 | } else { // use the pay date of the last coupon |
92 | 0 | payNotional = floatingLeg.back()->date(); |
93 | 0 | } |
94 | |
|
95 | 0 | Real floatAmount = subtractInflationNominal_ ? nominal_ - inflationNominal_ : nominal_; |
96 | 0 | ext::shared_ptr<CashFlow> nf(new SimpleCashFlow(floatAmount, payNotional)); |
97 | 0 | floatingLeg.push_back(nf); |
98 | 0 | } |
99 | | |
100 | | // a CPIleg know about zero legs and inclusion of base inflation notional |
101 | 0 | Leg cpiLeg = CPILeg(fixedSchedule_, fixedIndex_, |
102 | 0 | baseCPI_, observationLag_) |
103 | 0 | .withNotionals(inflationNominal_) |
104 | 0 | .withFixedRates(fixedRate_) |
105 | 0 | .withPaymentDayCounter(fixedDayCount_) |
106 | 0 | .withPaymentAdjustment(fixedPaymentRoll_) |
107 | 0 | .withObservationInterpolation(observationInterpolation_) |
108 | 0 | .withSubtractInflationNominal(subtractInflationNominal_); |
109 | | |
110 | |
|
111 | 0 | Leg::const_iterator i; |
112 | 0 | for (i = cpiLeg.begin(); i < cpiLeg.end(); ++i) { |
113 | 0 | registerWith(*i); |
114 | 0 | } |
115 | |
|
116 | 0 | for (i = floatingLeg.begin(); i < floatingLeg.end(); ++i) { |
117 | 0 | registerWith(*i); |
118 | 0 | } |
119 | |
|
120 | 0 | legs_[0] = cpiLeg; |
121 | 0 | legs_[1] = floatingLeg; |
122 | |
|
123 | 0 | if (type_==Payer) { |
124 | 0 | payer_[0] = 1.0; |
125 | 0 | payer_[1] = -1.0; |
126 | 0 | } else { |
127 | 0 | payer_[0] = -1.0; |
128 | 0 | payer_[1] = 1.0; |
129 | 0 | } |
130 | 0 | } Unexecuted instantiation: QuantLib::CPISwap::CPISwap(QuantLib::Swap::Type, double, bool, double, QuantLib::DayCounter, QuantLib::Schedule, QuantLib::BusinessDayConvention const&, unsigned int, boost::shared_ptr<QuantLib::IborIndex>, double, double, QuantLib::DayCounter, QuantLib::Schedule, QuantLib::BusinessDayConvention const&, QuantLib::Period const&, boost::shared_ptr<QuantLib::ZeroInflationIndex>, QuantLib::CPI::InterpolationType, double) Unexecuted instantiation: QuantLib::CPISwap::CPISwap(QuantLib::Swap::Type, double, bool, double, QuantLib::DayCounter, QuantLib::Schedule, QuantLib::BusinessDayConvention const&, unsigned int, boost::shared_ptr<QuantLib::IborIndex>, double, double, QuantLib::DayCounter, QuantLib::Schedule, QuantLib::BusinessDayConvention const&, QuantLib::Period const&, boost::shared_ptr<QuantLib::ZeroInflationIndex>, QuantLib::CPI::InterpolationType, double) |
131 | | |
132 | | |
133 | | //! for simple case sufficient to copy base class |
134 | 0 | void CPISwap::setupArguments(PricingEngine::arguments* args) const { |
135 | |
|
136 | 0 | Swap::setupArguments(args); |
137 | |
|
138 | 0 | auto* arguments = dynamic_cast<CPISwap::arguments*>(args); |
139 | |
|
140 | 0 | if (arguments == nullptr) |
141 | 0 | return; // it's a swap engine... |
142 | 0 | } |
143 | | |
144 | | |
145 | 0 | Rate CPISwap::fairRate() const { |
146 | 0 | calculate(); |
147 | 0 | QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available"); |
148 | 0 | return fairRate_; |
149 | 0 | } |
150 | | |
151 | 0 | Spread CPISwap::fairSpread() const { |
152 | 0 | calculate(); |
153 | 0 | QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available"); |
154 | 0 | return fairSpread_; |
155 | 0 | } |
156 | | |
157 | | |
158 | 0 | Real CPISwap::fixedLegNPV() const {//FIXME |
159 | 0 | calculate(); |
160 | 0 | QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available"); |
161 | 0 | return legNPV_[0]; |
162 | 0 | } |
163 | | |
164 | 0 | Real CPISwap::floatLegNPV() const {//FIXME |
165 | 0 | calculate(); |
166 | 0 | QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available"); |
167 | 0 | return legNPV_[1]; |
168 | 0 | } |
169 | | |
170 | 0 | void CPISwap::setupExpired() const { |
171 | 0 | Swap::setupExpired(); |
172 | 0 | legBPS_[0] = legBPS_[1] = 0.0; |
173 | 0 | fairRate_ = Null<Rate>(); |
174 | 0 | fairSpread_ = Null<Spread>(); |
175 | 0 | } |
176 | | |
177 | 0 | void CPISwap::fetchResults(const PricingEngine::results* r) const { |
178 | 0 | 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 | 0 | Swap::fetchResults(r); |
185 | |
|
186 | 0 | const auto* results = dynamic_cast<const CPISwap::results*>(r); |
187 | 0 | if (results != nullptr) { // might be a swap engine, so no error is thrown |
188 | 0 | fairRate_ = results->fairRate; |
189 | 0 | fairSpread_ = results->fairSpread; |
190 | 0 | } else { |
191 | 0 | fairRate_ = Null<Rate>(); |
192 | 0 | fairSpread_ = Null<Spread>(); |
193 | 0 | } |
194 | |
|
195 | 0 | if (fairRate_ == Null<Rate>()) { |
196 | | // calculate it from other results |
197 | 0 | if (legBPS_[0] != Null<Real>()) |
198 | 0 | fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint); |
199 | 0 | } |
200 | 0 | if (fairSpread_ == Null<Spread>()) { |
201 | | // ditto |
202 | 0 | if (legBPS_[1] != Null<Real>()) |
203 | 0 | fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint); |
204 | 0 | } |
205 | |
|
206 | 0 | } |
207 | | |
208 | 0 | void CPISwap::arguments::validate() const { |
209 | 0 | Swap::arguments::validate(); |
210 | 0 | } |
211 | | |
212 | 0 | void CPISwap::results::reset() { |
213 | 0 | Swap::results::reset(); |
214 | 0 | fairRate = Null<Rate>(); |
215 | 0 | fairSpread = Null<Spread>(); |
216 | 0 | } |
217 | | |
218 | | } |
219 | | |