QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
yearonyearinflationswap.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007, 2009 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/fixedratecoupon.hpp>
25#include <ql/cashflows/yoyinflationcoupon.hpp>
26#include <ql/indexes/inflationindex.hpp>
27#include <ql/instruments/yearonyearinflationswap.hpp>
28#include <ql/termstructures/yieldtermstructure.hpp>
29#include <ql/time/schedule.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35 Real nominal,
36 Schedule fixedSchedule,
37 Rate fixedRate,
38 DayCounter fixedDayCount,
39 Schedule yoySchedule,
40 ext::shared_ptr<YoYInflationIndex> yoyIndex,
41 const Period& observationLag,
42 Spread spread,
43 DayCounter yoyDayCount,
44 Calendar paymentCalendar,
45 BusinessDayConvention paymentConvention)
46 : Swap(2), type_(type), nominal_(nominal), fixedSchedule_(std::move(fixedSchedule)),
47 fixedRate_(fixedRate), fixedDayCount_(std::move(fixedDayCount)),
48 yoySchedule_(std::move(yoySchedule)), yoyIndex_(std::move(yoyIndex)),
49 observationLag_(observationLag), spread_(spread), yoyDayCount_(std::move(yoyDayCount)),
50 paymentCalendar_(std::move(paymentCalendar)), paymentConvention_(paymentConvention) {
51 // N.B. fixed leg gets its calendar from the schedule!
54 .withCouponRates(fixedRate_, fixedDayCount_) // Simple compounding by default
56
62
63 Leg::const_iterator i;
64 for (i = yoyLeg.begin(); i < yoyLeg.end(); ++i)
65 registerWith(*i);
66
67 legs_[0] = fixedLeg;
68 legs_[1] = yoyLeg;
69 if (type_==Payer) {
70 payer_[0] = -1.0;
71 payer_[1] = +1.0;
72 } else {
73 payer_[0] = +1.0;
74 payer_[1] = -1.0;
75 }
76 }
77
78
80
82
83 auto* arguments = dynamic_cast<YearOnYearInflationSwap::arguments*>(args);
84
85 if (arguments == nullptr) // it's a swap engine...
86 return;
87
90
91 const Leg& fixedCoupons = fixedLeg();
92
94 std::vector<Date>(fixedCoupons.size());
95 arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
96
97 for (Size i=0; i<fixedCoupons.size(); ++i) {
98 ext::shared_ptr<FixedRateCoupon> coupon =
99 ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
100
101 arguments->fixedPayDates[i] = coupon->date();
102 arguments->fixedResetDates[i] = coupon->accrualStartDate();
103 arguments->fixedCoupons[i] = coupon->amount();
104 }
105
106 const Leg& yoyCoupons = yoyLeg();
107
110 std::vector<Date>(yoyCoupons.size());
112 std::vector<Time>(yoyCoupons.size());
114 std::vector<Spread>(yoyCoupons.size());
115 arguments->yoyCoupons = std::vector<Real>(yoyCoupons.size());
116 for (Size i=0; i<yoyCoupons.size(); ++i) {
117 ext::shared_ptr<YoYInflationCoupon> coupon =
118 ext::dynamic_pointer_cast<YoYInflationCoupon>(yoyCoupons[i]);
119
120 arguments->yoyResetDates[i] = coupon->accrualStartDate();
121 arguments->yoyPayDates[i] = coupon->date();
122
123 arguments->yoyFixingDates[i] = coupon->fixingDate();
124 arguments->yoyAccrualTimes[i] = coupon->accrualPeriod();
125 arguments->yoySpreads[i] = coupon->spread();
126 try {
127 arguments->yoyCoupons[i] = coupon->amount();
128 } catch (Error&) {
130 }
131 }
132 }
133
134
136 calculate();
137 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
138 return fairRate_;
139 }
140
142 calculate();
143 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
144 return fairSpread_;
145 }
146
147
149 calculate();
150 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
151 return legNPV_[0];
152 }
153
155 calculate();
156 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
157 return legNPV_[1];
158 }
159
162 legBPS_[0] = legBPS_[1] = 0.0;
165 }
166
168 static const Spread basisPoint = 1.0e-4;
169
170 // copy from VanillaSwap
171 // works because similarly simple instrument
172 // that we always expect to be priced with a swap engine
173
175
176 const auto* results = dynamic_cast<const YearOnYearInflationSwap::results*>(r);
177 if (results != nullptr) { // might be a swap engine, so no error is thrown
180 } else {
183 }
184
185 if (fairRate_ == Null<Rate>()) {
186 // calculate it from other results
187 if (legBPS_[0] != Null<Real>())
188 fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
189 }
190 if (fairSpread_ == Null<Spread>()) {
191 // ditto
192 if (legBPS_[1] != Null<Real>())
193 fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
194 }
195
196 }
197
200 QL_REQUIRE(nominal != Null<Real>(), "nominal null or not set");
201 QL_REQUIRE(fixedResetDates.size() == fixedPayDates.size(),
202 "number of fixed start dates different from "
203 "number of fixed payment dates");
204 QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
205 "number of fixed payment dates different from "
206 "number of fixed coupon amounts");
207 QL_REQUIRE(yoyResetDates.size() == yoyPayDates.size(),
208 "number of yoy start dates different from "
209 "number of yoy payment dates");
210 QL_REQUIRE(yoyFixingDates.size() == yoyPayDates.size(),
211 "number of yoy fixing dates different from "
212 "number of yoy payment dates");
213 QL_REQUIRE(yoyAccrualTimes.size() == yoyPayDates.size(),
214 "number of yoy accrual Times different from "
215 "number of yoy payment dates");
216 QL_REQUIRE(yoySpreads.size() == yoyPayDates.size(),
217 "number of yoy spreads different from "
218 "number of yoy payment dates");
219 QL_REQUIRE(yoyPayDates.size() == yoyCoupons.size(),
220 "number of yoy payment dates different from "
221 "number of yoy coupon amounts");
222 }
223
228 }
229
230}
231
calendar class
Definition: calendar.hpp:61
day counter class
Definition: daycounter.hpp:44
Base error class.
Definition: errors.hpp:39
helper class building a sequence of fixed rate coupons
FixedRateLeg & withNotionals(Real)
FixedRateLeg & withPaymentAdjustment(BusinessDayConvention)
FixedRateLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
void calculate() const override
Definition: instrument.hpp:129
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
Payment schedule.
Definition: schedule.hpp:40
void validate() const override
Definition: swap.cpp:171
void reset() override
Definition: swap.cpp:176
Interest rate swap.
Definition: swap.hpp:41
void setupArguments(PricingEngine::arguments *) const override
Definition: swap.cpp:87
std::vector< Leg > legs_
Definition: swap.hpp:133
std::vector< Real > legNPV_
Definition: swap.hpp:135
std::vector< Real > legBPS_
Definition: swap.hpp:136
void setupExpired() const override
Definition: swap.cpp:78
void fetchResults(const PricingEngine::results *) const override
Definition: swap.cpp:95
std::vector< Real > payer_
Definition: swap.hpp:134
void setupArguments(PricingEngine::arguments *args) const override
YearOnYearInflationSwap(Type type, Real nominal, Schedule fixedSchedule, Rate fixedRate, DayCounter fixedDayCount, Schedule yoySchedule, ext::shared_ptr< YoYInflationIndex > yoyIndex, const Period &observationLag, Spread spread, DayCounter yoyDayCount, Calendar paymentCalendar, BusinessDayConvention paymentConvention=ModifiedFollowing)
void fetchResults(const PricingEngine::results *) const override
ext::shared_ptr< YoYInflationIndex > yoyIndex_
yoyInflationLeg & withSpreads(Spread spread)
yoyInflationLeg & withPaymentAdjustment(BusinessDayConvention)
yoyInflationLeg & withNotionals(Real notional)
yoyInflationLeg & withPaymentDayCounter(const DayCounter &)
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.