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 <http://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
28namespace QuantLib {
29
30 BondHelper::BondHelper(const Handle<Quote>& price,
31 const ext::shared_ptr<Bond>& bond,
32 const Bond::Price::Type priceType)
33 : RateHelper(price), bond_(ext::make_shared<Bond>(args&: *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 latestDate_ = bond_->cashflows().back()->date();
38 earliestDate_ = bond_->nextCashFlowDate();
39
40 bond_->setPricingEngine(
41 ext::make_shared<DiscountingBondEngine>(args&: termStructureHandle_));
42 }
43
44 void BondHelper::setTermStructure(YieldTermStructure* t) {
45 // do not set the relinkable handle as an observer -
46 // force recalculation when needed
47 termStructureHandle_.linkTo(
48 h: ext::shared_ptr<YieldTermStructure>(t, null_deleter()), registerAsObserver: false);
49
50 BootstrapHelper<YieldTermStructure>::setTermStructure(t);
51 }
52
53 Real BondHelper::impliedQuote() const {
54 QL_REQUIRE(termStructure_ != nullptr, "term structure not set");
55 // we didn't register as observers - force calculation
56 bond_->recalculate();
57
58 switch (priceType_) {
59 case Bond::Price::Clean:
60 return bond_->cleanPrice();
61 break;
62
63 case Bond::Price::Dirty:
64 return bond_->dirtyPrice();
65 break;
66
67 default:
68 QL_FAIL("This price type isn't implemented.");
69 }
70 }
71
72 void BondHelper::accept(AcyclicVisitor& v) {
73 auto* v1 = dynamic_cast<Visitor<BondHelper>*>(&v);
74 if (v1 != nullptr)
75 v1->visit(*this);
76 else
77 BootstrapHelper<YieldTermStructure>::accept(v);
78 }
79
80 FixedRateBondHelper::FixedRateBondHelper(
81 const Handle<Quote>& price,
82 Natural settlementDays,
83 Real faceAmount,
84 const Schedule& schedule,
85 const std::vector<Rate>& coupons,
86 const DayCounter& dayCounter,
87 BusinessDayConvention paymentConvention,
88 Real redemption,
89 const Date& issueDate,
90 const Calendar& paymentCalendar,
91 const Period& exCouponPeriod,
92 const Calendar& exCouponCalendar,
93 const BusinessDayConvention exCouponConvention,
94 bool exCouponEndOfMonth,
95 const Bond::Price::Type priceType)
96 : BondHelper(price,
97 ext::shared_ptr<Bond>(
98 new FixedRateBond(settlementDays, faceAmount, schedule,
99 coupons, dayCounter, paymentConvention,
100 redemption, issueDate, paymentCalendar,
101 exCouponPeriod, exCouponCalendar,
102 exCouponConvention, exCouponEndOfMonth)),
103 priceType) {
104 fixedRateBond_ = ext::dynamic_pointer_cast<FixedRateBond>(r: bond_);
105 }
106
107 void FixedRateBondHelper::accept(AcyclicVisitor& v) {
108 auto* v1 = dynamic_cast<Visitor<FixedRateBondHelper>*>(&v);
109 if (v1 != nullptr)
110 v1->visit(*this);
111 else
112 BondHelper::accept(v);
113 }
114
115 CPIBondHelper::CPIBondHelper(
116 const Handle<Quote>& price,
117 Natural settlementDays,
118 Real faceAmount,
119 const bool growthOnly,
120 Real baseCPI,
121 const Period& observationLag,
122 const ext::shared_ptr<ZeroInflationIndex>& cpiIndex,
123 CPI::InterpolationType observationInterpolation,
124 const 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 : BondHelper(price,
136 ext::shared_ptr<Bond>(
137 new CPIBond(settlementDays, faceAmount, growthOnly, baseCPI,
138 observationLag, cpiIndex, observationInterpolation,
139 schedule, fixedRate, accrualDayCounter, paymentConvention,
140 issueDate, paymentCalendar, exCouponPeriod, exCouponCalendar,
141 exCouponConvention, exCouponEndOfMonth)),
142 priceType) {
143 cpiBond_ = ext::dynamic_pointer_cast<CPIBond>(r: bond_);
144 }
145
146 void CPIBondHelper::accept(AcyclicVisitor& v) {
147 auto* v1 = dynamic_cast<Visitor<CPIBondHelper>*>(&v);
148 if (v1 != nullptr)
149 v1->visit(*this);
150 else
151 BondHelper::accept(v);
152 }
153
154}
155

source code of quantlib/ql/termstructures/yield/bondhelpers.cpp