Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
subperiodscoupon.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <ql/indexes/interestrateindex.hpp>
20#include <ql/time/schedule.hpp>
21#include <ql/utilities/vectors.hpp>
22
26
27using namespace QuantLib;
28
29namespace QuantExt {
30
31SubPeriodsCoupon1::SubPeriodsCoupon1(const Date& paymentDate, Real nominal, const Date& startDate, const Date& endDate,
32 const QuantLib::ext::shared_ptr<InterestRateIndex>& index, Type type,
33 BusinessDayConvention convention, Spread spread, const DayCounter& dayCounter,
34 bool includeSpread, Real gearing)
35 : FloatingRateCoupon(paymentDate, nominal, startDate, endDate, index->fixingDays(), index, gearing, spread, Date(),
36 Date(), dayCounter, false),
37 type_(type), includeSpread_(includeSpread) {
38
39 // Populate the value dates.
40 Schedule sch = MakeSchedule()
41 .from(startDate)
42 .to(endDate)
43 .withTenor(index->tenor())
44 .withCalendar(index->fixingCalendar())
45 .withConvention(convention)
46 .withTerminationDateConvention(convention)
47 .backwards();
48 valueDates_ = sch.dates();
49 QL_ENSURE(valueDates_.size() >= 2, "Degenerate schedule.");
50
51 // Populate the fixing dates.
52 numPeriods_ = valueDates_.size() - 1;
53 if (index->fixingDays() == 0) {
54 fixingDates_ = std::vector<Date>(valueDates_.begin(), valueDates_.end() - 1);
55 } else {
57 for (Size i = 0; i < numPeriods_; ++i)
58 fixingDates_[i] = index->fixingDate(valueDates_[i]);
59 }
60
61 // Populate the accrual periods.
63 for (Size i = 0; i < numPeriods_; ++i) {
64 accrualFractions_[i] = dayCounter.yearFraction(valueDates_[i], valueDates_[i + 1]);
65 }
66}
67
68const std::vector<Rate>& SubPeriodsCoupon1::indexFixings() const {
69
70 fixings_.resize(numPeriods_);
71
72 for (Size i = 0; i < numPeriods_; ++i) {
73 fixings_[i] = index_->fixing(fixingDates_[i]);
74 }
75
76 return fixings_;
77}
78
79void SubPeriodsCoupon1::accept(AcyclicVisitor& v) {
80 Visitor<SubPeriodsCoupon1>* v1 = dynamic_cast<Visitor<SubPeriodsCoupon1>*>(&v);
81 if (v1 != 0) {
82 v1->visit(*this);
83 } else {
84 FloatingRateCoupon::accept(v);
85 }
86}
87
88SubPeriodsLeg1::SubPeriodsLeg1(const Schedule& schedule, const QuantLib::ext::shared_ptr<InterestRateIndex>& index)
89 : schedule_(schedule), index_(index), notionals_(std::vector<Real>(1, 1.0)), paymentAdjustment_(Following),
90 paymentCalendar_(Calendar()), type_(SubPeriodsCoupon1::Compounding) {}
91
93 notionals_ = std::vector<Real>(1, notional);
94 return *this;
95}
96
97SubPeriodsLeg1& SubPeriodsLeg1::withNotionals(const std::vector<Real>& notionals) {
98 notionals_ = notionals;
99 return *this;
100}
101
103 paymentDayCounter_ = dayCounter;
104 return *this;
105}
106
107SubPeriodsLeg1& SubPeriodsLeg1::withPaymentAdjustment(BusinessDayConvention convention) {
108 paymentAdjustment_ = convention;
109 return *this;
110}
111
113 gearings_ = std::vector<Real>(1, gearing);
114 return *this;
115}
116
117SubPeriodsLeg1& SubPeriodsLeg1::withGearings(const std::vector<Real>& gearings) {
118 gearings_ = gearings;
119 return *this;
120}
121
123 spreads_ = std::vector<Spread>(1, spread);
124 return *this;
125}
126
127SubPeriodsLeg1& SubPeriodsLeg1::withSpreads(const std::vector<Spread>& spreads) {
128 spreads_ = spreads;
129 return *this;
130}
131
133 paymentCalendar_ = calendar;
134 return *this;
135}
136
138 type_ = type;
139 return *this;
140}
141
144 return *this;
145}
146
147SubPeriodsLeg1::operator Leg() const {
148
149 Leg cashflows;
150 Date startDate;
151 Date endDate;
152 Date paymentDate;
153
154 Calendar calendar;
155 if (!paymentCalendar_.empty()) {
156 calendar = paymentCalendar_;
157 } else {
158 calendar = schedule_.calendar();
159 }
160
161 Size numPeriods = schedule_.size() - 1;
162 if (numPeriods == 0)
163 return cashflows;
164
165 startDate = schedule_.date(0);
166 for (Size i = 0; i < numPeriods; ++i) {
167 endDate = schedule_.date(i + 1);
168 paymentDate = calendar.adjust(endDate, paymentAdjustment_);
169 // the sub periods coupon might produce degenerated schedules, in this
170 // case we just join the current period with the next one
171 // we catch all QL exceptions here, although we should only pick the one
172 // that is thrown in case of a degenerated schedule, but there is no way
173 // of identifying it except parsing the exception text, which isn't a
174 // clean solution either
175 try {
176 QuantLib::ext::shared_ptr<SubPeriodsCoupon1> cashflow(
177 new SubPeriodsCoupon1(paymentDate, detail::get(notionals_, i, notionals_.back()), startDate, endDate,
178 index_, type_, paymentAdjustment_, detail::get(spreads_, i, 0.0),
179 paymentDayCounter_, includeSpread_, detail::get(gearings_, i, 1.0)));
180
181 cashflows.push_back(cashflow);
182 startDate = endDate;
183 } catch (const QuantLib::Error&) {
184 }
185 }
186
187 QuantLib::ext::shared_ptr<SubPeriodsCouponPricer1> pricer(new SubPeriodsCouponPricer1);
188 QuantExt::setCouponPricer(cashflows, pricer);
189
190 return cashflows;
191}
192} // namespace QuantExt
std::vector< Date > fixingDates_
std::vector< Time > accrualFractions_
void accept(AcyclicVisitor &) override
std::vector< Rate > fixings_
std::vector< Date > valueDates_
const std::vector< Rate > & indexFixings() const
fixings for the sub-periods
SubPeriodsCoupon1(const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, const QuantLib::ext::shared_ptr< InterestRateIndex > &index, Type type, BusinessDayConvention convention, Spread spread=0.0, const DayCounter &dayCounter=DayCounter(), bool includeSpread=false, Real gearing=1.0)
Pricer for sub-period coupons.
helper class building a sequence of sub-period coupons
BusinessDayConvention paymentAdjustment_
SubPeriodsLeg1 & withPaymentDayCounter(const DayCounter &dayCounter)
SubPeriodsLeg1 & withGearing(Real gearing)
SubPeriodsLeg1 & withType(SubPeriodsCoupon1::Type type)
SubPeriodsLeg1 & withSpreads(const std::vector< Spread > &spreads)
std::vector< Real > notionals_
std::vector< Spread > spreads_
SubPeriodsLeg1(const Schedule &schedule, const QuantLib::ext::shared_ptr< InterestRateIndex > &index)
SubPeriodsLeg1 & withSpread(Spread spread)
SubPeriodsCoupon1::Type type_
SubPeriodsLeg1 & includeSpread(bool includeSpread)
SubPeriodsLeg1 & withGearings(const std::vector< Real > &gearings)
SubPeriodsLeg1 & withPaymentCalendar(const Calendar &calendar)
SubPeriodsLeg1 & withNotional(Real notional)
SubPeriodsLeg1 & withNotionals(const std::vector< Real > &notionals)
SubPeriodsLeg1 & withPaymentAdjustment(BusinessDayConvention convention)
std::vector< Real > gearings_
Utility functions for setting coupon pricers on legs.
void setCouponPricer(const Leg &leg, const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > &pricer)
Set Coupon Pricer.
Coupon with a number of sub-periods.
Pricer for sub-period coupons.