QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
bondhelpers.cpp
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
31 const ext::shared_ptr<Bond>& bond,
32 const Bond::Price::Type priceType)
33 : 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 latestDate_ = bond_->cashflows().back()->date();
38 earliestDate_ = bond_->nextCashFlowDate();
39
40 bond_->setPricingEngine(
41 ext::make_shared<DiscountingBondEngine>(termStructureHandle_));
42 }
43
45 // do not set the relinkable handle as an observer -
46 // force recalculation when needed
48 ext::shared_ptr<YieldTermStructure>(t, null_deleter()), false);
49
51 }
52
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_) {
60 return bond_->cleanPrice();
61 break;
62
64 return bond_->dirtyPrice();
65 break;
66
67 default:
68 QL_FAIL("This price type isn't implemented.");
69 }
70 }
71
73 auto* v1 = dynamic_cast<Visitor<BondHelper>*>(&v);
74 if (v1 != nullptr)
75 v1->visit(*this);
76 else
78 }
79
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>(bond_);
105 }
106
108 auto* v1 = dynamic_cast<Visitor<FixedRateBondHelper>*>(&v);
109 if (v1 != nullptr)
110 v1->visit(*this);
111 else
113 }
114
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>(bond_);
144 }
145
147 auto* v1 = dynamic_cast<Visitor<CPIBondHelper>*>(&v);
148 if (v1 != nullptr)
149 v1->visit(*this);
150 else
152 }
153
154}
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
Bond helper for curve bootstrap.
Definition: bondhelpers.hpp:40
Bond::Price::Type priceType_
Definition: bondhelpers.hpp:70
void setTermStructure(YieldTermStructure *) override
Definition: bondhelpers.cpp:44
RelinkableHandle< YieldTermStructure > termStructureHandle_
Definition: bondhelpers.hpp:69
void accept(AcyclicVisitor &) override
Definition: bondhelpers.cpp:72
Real impliedQuote() const override
Definition: bondhelpers.cpp:53
BondHelper(const Handle< Quote > &price, const ext::shared_ptr< Bond > &bond, Bond::Price::Type priceType=Bond::Price::Clean)
Definition: bondhelpers.cpp:30
ext::shared_ptr< Bond > bond_
Definition: bondhelpers.hpp:68
Base bond class.
Definition: bond.hpp:59
Base helper class for bootstrapping.
virtual void accept(AcyclicVisitor &)
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
ext::shared_ptr< CPIBond > cpiBond_
void accept(AcyclicVisitor &) override
CPIBondHelper(const Handle< Quote > &price, Natural settlementDays, Real faceAmount, bool growthOnly, Real baseCPI, const Period &observationLag, const ext::shared_ptr< ZeroInflationIndex > &cpiIndex, CPI::InterpolationType observationInterpolation, const Schedule &schedule, const std::vector< Rate > &fixedRate, const DayCounter &accrualDayCounter, BusinessDayConvention paymentConvention=Following, const Date &issueDate=Date(), const Calendar &paymentCalendar=Calendar(), const Period &exCouponPeriod=Period(), const Calendar &exCouponCalendar=Calendar(), BusinessDayConvention exCouponConvention=Unadjusted, bool exCouponEndOfMonth=false, Bond::Price::Type priceType=Bond::Price::Clean)
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
FixedRateBondHelper(const Handle< Quote > &price, Natural settlementDays, Real faceAmount, const Schedule &schedule, const std::vector< Rate > &coupons, const DayCounter &dayCounter, BusinessDayConvention paymentConv=Following, Real redemption=100.0, const Date &issueDate=Date(), const Calendar &paymentCalendar=Calendar(), const Period &exCouponPeriod=Period(), const Calendar &exCouponCalendar=Calendar(), BusinessDayConvention exCouponConvention=Unadjusted, bool exCouponEndOfMonth=false, Bond::Price::Type priceType=Bond::Price::Clean)
Definition: bondhelpers.cpp:80
void accept(AcyclicVisitor &) override
ext::shared_ptr< FixedRateBond > fixedRateBond_
Shared handle to an observable.
Definition: handle.hpp:41
Payment schedule.
Definition: schedule.hpp:40
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Interest-rate term structure.
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Definition: any.hpp:35
InterpolationType
when you observe an index, how do you interpolate between fixings?