/src/quantlib/ql/termstructures/yield/bondhelpers.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008, 2009 Ferdinando Ametrano |
5 | | Copyright (C) 2005 Toyin Akin |
6 | | Copyright (C) 2007 StatPro Italia srl |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <https://www.quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | #include <ql/termstructures/yield/bondhelpers.hpp> |
23 | | #include <ql/pricingengines/bond/discountingbondengine.hpp> |
24 | | #include <ql/time/schedule.hpp> |
25 | | #include <ql/settings.hpp> |
26 | | #include <ql/utilities/null_deleter.hpp> |
27 | | |
28 | | namespace QuantLib { |
29 | | |
30 | | BondHelper::BondHelper(const Handle<Quote>& price, |
31 | | const ext::shared_ptr<Bond>& bond, |
32 | | const Bond::Price::Type priceType) |
33 | 0 | : RateHelper(price), bond_(ext::make_shared<Bond>(*bond)), priceType_(priceType) { |
34 | | |
35 | | // the bond's last cashflow date, which can be later than |
36 | | // bond's maturity date because of adjustment |
37 | 0 | latestDate_ = bond_->cashflows().back()->date(); |
38 | 0 | earliestDate_ = bond_->nextCashFlowDate(); |
39 | |
|
40 | 0 | bond_->setPricingEngine( |
41 | 0 | ext::make_shared<DiscountingBondEngine>(termStructureHandle_)); |
42 | 0 | } |
43 | | |
44 | 0 | void BondHelper::setTermStructure(YieldTermStructure* t) { |
45 | | // do not set the relinkable handle as an observer - |
46 | | // force recalculation when needed |
47 | 0 | termStructureHandle_.linkTo( |
48 | 0 | ext::shared_ptr<YieldTermStructure>(t, null_deleter()), false); |
49 | |
|
50 | 0 | BootstrapHelper<YieldTermStructure>::setTermStructure(t); |
51 | 0 | } |
52 | | |
53 | 0 | Real BondHelper::impliedQuote() const { |
54 | 0 | QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); |
55 | | // we didn't register as observers - force calculation |
56 | 0 | bond_->recalculate(); |
57 | |
|
58 | 0 | switch (priceType_) { |
59 | 0 | case Bond::Price::Clean: |
60 | 0 | return bond_->cleanPrice(); |
61 | 0 | break; |
62 | | |
63 | 0 | case Bond::Price::Dirty: |
64 | 0 | return bond_->dirtyPrice(); |
65 | 0 | break; |
66 | | |
67 | 0 | default: |
68 | 0 | QL_FAIL("This price type isn't implemented."); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | void BondHelper::accept(AcyclicVisitor& v) { |
73 | 0 | auto* v1 = dynamic_cast<Visitor<BondHelper>*>(&v); |
74 | 0 | if (v1 != nullptr) |
75 | 0 | v1->visit(*this); |
76 | 0 | else |
77 | 0 | BootstrapHelper<YieldTermStructure>::accept(v); |
78 | 0 | } |
79 | | |
80 | | |
81 | | FixedRateBondHelper::FixedRateBondHelper( |
82 | | const Handle<Quote>& price, |
83 | | Natural settlementDays, |
84 | | Real faceAmount, |
85 | | Schedule schedule, |
86 | | const std::vector<Rate>& coupons, |
87 | | const DayCounter& dayCounter, |
88 | | BusinessDayConvention paymentConvention, |
89 | | Real redemption, |
90 | | const Date& issueDate, |
91 | | const Calendar& paymentCalendar, |
92 | | const Period& exCouponPeriod, |
93 | | const Calendar& exCouponCalendar, |
94 | | const BusinessDayConvention exCouponConvention, |
95 | | bool exCouponEndOfMonth, |
96 | | const Bond::Price::Type priceType) |
97 | 0 | : BondHelper(price, |
98 | 0 | ext::make_shared<FixedRateBond>(settlementDays, faceAmount, std::move(schedule), |
99 | 0 | coupons, dayCounter, paymentConvention, |
100 | 0 | redemption, issueDate, paymentCalendar, |
101 | 0 | exCouponPeriod, exCouponCalendar, |
102 | 0 | exCouponConvention, exCouponEndOfMonth), |
103 | 0 | priceType) {} |
104 | | |
105 | 0 | void FixedRateBondHelper::accept(AcyclicVisitor& v) { |
106 | 0 | auto* v1 = dynamic_cast<Visitor<FixedRateBondHelper>*>(&v); |
107 | 0 | if (v1 != nullptr) |
108 | 0 | v1->visit(*this); |
109 | 0 | else |
110 | 0 | BondHelper::accept(v); |
111 | 0 | } |
112 | | |
113 | | |
114 | | QL_DEPRECATED_DISABLE_WARNING |
115 | | |
116 | | CPIBondHelper::CPIBondHelper( |
117 | | const Handle<Quote>& price, |
118 | | Natural settlementDays, |
119 | | Real faceAmount, |
120 | | Real baseCPI, |
121 | | const Period& observationLag, |
122 | | const ext::shared_ptr<ZeroInflationIndex>& cpiIndex, |
123 | | CPI::InterpolationType observationInterpolation, |
124 | | Schedule schedule, |
125 | | const std::vector<Rate>& fixedRate, |
126 | | const DayCounter& accrualDayCounter, |
127 | | BusinessDayConvention paymentConvention, |
128 | | const Date& issueDate, |
129 | | const Calendar& paymentCalendar, |
130 | | const Period& exCouponPeriod, |
131 | | const Calendar& exCouponCalendar, |
132 | | const BusinessDayConvention exCouponConvention, |
133 | | bool exCouponEndOfMonth, |
134 | | const Bond::Price::Type priceType) |
135 | 0 | : CPIBondHelper(price, settlementDays, faceAmount, false, baseCPI, observationLag, |
136 | 0 | cpiIndex, observationInterpolation, std::move(schedule), fixedRate, |
137 | 0 | accrualDayCounter, paymentConvention, issueDate, paymentCalendar, |
138 | 0 | exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth, |
139 | 0 | priceType) {} |
140 | | |
141 | | CPIBondHelper::CPIBondHelper( |
142 | | const Handle<Quote>& price, |
143 | | Natural settlementDays, |
144 | | Real faceAmount, |
145 | | const bool growthOnly, |
146 | | Real baseCPI, |
147 | | const Period& observationLag, |
148 | | const ext::shared_ptr<ZeroInflationIndex>& cpiIndex, |
149 | | CPI::InterpolationType observationInterpolation, |
150 | | Schedule schedule, |
151 | | const std::vector<Rate>& fixedRate, |
152 | | const DayCounter& accrualDayCounter, |
153 | | BusinessDayConvention paymentConvention, |
154 | | const Date& issueDate, |
155 | | const Calendar& paymentCalendar, |
156 | | const Period& exCouponPeriod, |
157 | | const Calendar& exCouponCalendar, |
158 | | const BusinessDayConvention exCouponConvention, |
159 | | bool exCouponEndOfMonth, |
160 | | const Bond::Price::Type priceType) |
161 | 0 | : BondHelper(price, |
162 | | // make_shared and deprecation interfere; restore later |
163 | 0 | ext::shared_ptr<Bond>(new CPIBond(settlementDays, faceAmount, growthOnly, baseCPI, |
164 | 0 | observationLag, cpiIndex, observationInterpolation, |
165 | 0 | std::move(schedule), fixedRate, accrualDayCounter, paymentConvention, |
166 | 0 | issueDate, paymentCalendar, exCouponPeriod, exCouponCalendar, |
167 | 0 | exCouponConvention, exCouponEndOfMonth)), |
168 | 0 | priceType) {} |
169 | | |
170 | | QL_DEPRECATED_ENABLE_WARNING |
171 | | |
172 | 0 | void CPIBondHelper::accept(AcyclicVisitor& v) { |
173 | 0 | auto* v1 = dynamic_cast<Visitor<CPIBondHelper>*>(&v); |
174 | 0 | if (v1 != nullptr) |
175 | 0 | v1->visit(*this); |
176 | 0 | else |
177 | 0 | BondHelper::accept(v); |
178 | 0 | } |
179 | | |
180 | | } |