QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
btp.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010, 2011 Ferdinando Ametrano
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/instruments/bonds/btp.hpp>
21#include <ql/instruments/makevanillaswap.hpp>
22#include <ql/pricingengines/bond/bondfunctions.hpp>
23#include <ql/time/calendars/nullcalendar.hpp>
24#include <ql/time/calendars/target.hpp>
25#include <ql/time/daycounters/actual360.hpp>
26#include <ql/time/daycounters/actualactual.hpp>
27#include <ql/time/schedule.hpp>
28#include <ql/utilities/dataformatters.hpp>
29#include <utility>
30
31namespace QuantLib {
32
33 CCTEU::CCTEU(const Date& maturityDate,
34 Spread spread,
35 const Handle<YieldTermStructure>& fwdCurve,
36 const Date& startDate,
37 const Date& issueDate)
38 : FloatingRateBond(2, 100.0,
39 Schedule(startDate,
40 maturityDate, 6*Months,
42 DateGeneration::Backward, true),
43 ext::make_shared<Euribor6M>(fwdCurve),
44 Actual360(),
46 Euribor6M().fixingDays(),
47 std::vector<Real>(1, 1.0), // gearing
48 std::vector<Spread>(1, spread),
49 std::vector<Rate>(), // caps
50 std::vector<Rate>(), // floors
51 false, // in arrears
52 100.0, // redemption
53 issueDate) {}
54
55 BTP::BTP(const Date& maturityDate,
56 Rate fixedRate,
57 const Date& startDate,
58 const Date& issueDate)
59 : FixedRateBond(2, 100.0,
60 Schedule(startDate,
61 maturityDate, 6*Months,
63 DateGeneration::Backward, true),
64 std::vector<Rate>(1, fixedRate),
66 ModifiedFollowing, 100.0, issueDate, TARGET()) {}
67
68 BTP::BTP(const Date& maturityDate,
69 Rate fixedRate,
70 Real redemption,
71 const Date& startDate,
72 const Date& issueDate)
73 : FixedRateBond(2, 100.0,
74 Schedule(startDate,
75 maturityDate, 6*Months,
77 DateGeneration::Backward, true),
78 std::vector<Rate>(1, fixedRate),
80 ModifiedFollowing, redemption, issueDate, TARGET()) {}
81
82 Rate BTP::yield(Real cleanPrice,
83 Date settlementDate,
84 Real accuracy,
85 Size maxEvaluations) const {
88 settlementDate, accuracy, maxEvaluations);
89 }
90
91
92 RendistatoBasket::RendistatoBasket(const std::vector<ext::shared_ptr<BTP> >& btps,
93 const std::vector<Real>& outstandings,
94 std::vector<Handle<Quote> > cleanPriceQuotes)
95 : btps_(btps), outstandings_(outstandings), quotes_(std::move(cleanPriceQuotes)) {
96
97 QL_REQUIRE(!btps_.empty(), "empty RendistatoCalculator Basket");
98 Size k = btps_.size();
99
100 QL_REQUIRE(outstandings_.size()==k,
101 "mismatch between number of BTPs (" << k <<
102 ") and number of outstandings (" <<
103 outstandings_.size() << ")");
104 QL_REQUIRE(quotes_.size()==k,
105 "mismatch between number of BTPs (" << k <<
106 ") and number of clean prices quotes (" <<
107 quotes_.size() << ")");
108
109 // require non-negative outstanding
110 for (Size i=0; i<k; ++i) {
111 QL_REQUIRE(outstandings[i]>=0,
112 "negative outstanding for " << io::ordinal(i) <<
113 " bond, maturity " << btps[i]->maturityDate());
114 // add check for prices ??
115 }
116
117 // TODO: filter out expired bonds, zero outstanding bond, etc
118
119 QL_REQUIRE(!btps_.empty(), "invalid bonds only in RendistatoCalculator Basket");
120 n_ = btps_.size();
121
122 outstanding_ = 0.0;
123 for (Size i=0; i<n_; ++i)
125
126 weights_.resize(n_);
127 for (Size i=0; i<n_; ++i) {
130 }
131 }
132
133
134 RendistatoCalculator::RendistatoCalculator(ext::shared_ptr<RendistatoBasket> basket,
135 ext::shared_ptr<Euribor> euriborIndex,
136 Handle<YieldTermStructure> discountCurve)
137 : basket_(std::move(basket)), euriborIndex_(std::move(euriborIndex)),
138 discountCurve_(std::move(discountCurve)), yields_(basket_->size(), 0.05),
139 durations_(basket_->size()),
140 // TODO: generalize number of swaps and their lengths
141 swaps_(nSwaps_), swapLengths_(nSwaps_), swapBondDurations_(nSwaps_, Null<Time>()),
142 swapBondYields_(nSwaps_, 0.05), swapRates_(nSwaps_, Null<Rate>()) {
146
147 Rate dummyRate = 0.05;
148 for (Size i=0; i<nSwaps_; ++i) {
149 swapLengths_[i] = static_cast<Real>(i+1);
151 swapLengths_[i]*Years, euriborIndex_, dummyRate, 1*Days)
153 }
154 }
155
157
158 const std::vector<ext::shared_ptr<BTP> >& btps = basket_->btps();
159 const std::vector<Handle<Quote> >& quotes = basket_->cleanPriceQuotes();
160 Date bondSettlementDate = btps[0]->settlementDate();
161 for (Size i=0; i<basket_->size(); ++i) {
163 *btps[i], quotes[i]->value(),
165 bondSettlementDate,
166 // accuracy, maxIterations, guess
167 1.0e-10, 100, yields_[i]);
169 *btps[i], yields_[i],
171 Duration::Modified, bondSettlementDate);
172 }
173 duration_ = std::inner_product(basket_->weights().begin(),
174 basket_->weights().end(),
175 durations_.begin(), Real(0.0));
176
177 Natural settlDays = 2;
178 DayCounter fixedDayCount = swaps_[0]->fixedDayCount();
180 swapRates_[0]= swaps_[0]->fairRate();
181 FixedRateBond swapBond(settlDays,
182 100.0, // faceAmount
183 swaps_[0]->fixedSchedule(),
184 std::vector<Rate>(1, swapRates_[0]),
185 fixedDayCount,
186 Following, // paymentConvention
187 100.0); // redemption
189 100.0, // floating leg NPV including end payment
191 bondSettlementDate,
192 // accuracy, maxIterations, guess
193 1.0e-10, 100, swapBondYields_[0]);
195 swapBond, swapBondYields_[0],
197 Duration::Modified, bondSettlementDate);
198 for (Size i=1; i<nSwaps_; ++i) {
199 swapRates_[i]= swaps_[i]->fairRate();
200 FixedRateBond swapBond(settlDays,
201 100.0, // faceAmount
202 swaps_[i]->fixedSchedule(),
203 std::vector<Rate>(1, swapRates_[i]),
204 fixedDayCount,
205 Following, // paymentConvention
206 100.0); // redemption
208 100.0, // floating leg NPV including end payment
210 bondSettlementDate,
211 // accuracy, maxIterations, guess
212 1.0e-10, 100, swapBondYields_[i]);
214 swapBond, swapBondYields_[i],
216 Duration::Modified, bondSettlementDate);
217 if (swapBondDurations_[i] > duration_) {
219 break; // exit the loop
220 }
221 }
222 }
223
225 ext::shared_ptr<RendistatoCalculator> r)
226 : r_(std::move(r)) {}
227
229 try {
230 value();
231 return true;
232 } catch (...) {
233 return false;
234 }
235 }
236
238 ext::shared_ptr<RendistatoCalculator> r)
239 : r_(std::move(r)) {}
240
242 try {
243 value();
244 return true;
245 } catch (...) {
246 return false;
247 }
248 }
249}
Actual/360 day count convention.
Definition: actual360.hpp:37
Actual/Actual day count.
Rate yield(Real cleanPrice, Date settlementDate=Date(), Real accuracy=1.0e-8, Size maxEvaluations=100) const
BTP yield given a (clean) price and settlement date.
Definition: btp.cpp:82
BTP(const Date &maturityDate, Rate fixedRate, const Date &startDate=Date(), const Date &issueDate=Date())
Definition: btp.cpp:55
Real cleanPrice() const
theoretical clean price
Definition: bond.cpp:174
Rate yield(const DayCounter &dc, Compounding comp, Frequency freq, Real accuracy=1.0e-8, Size maxEvaluations=100, Real guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean) const
theoretical bond yield
Definition: bond.cpp:198
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:161
CCTEU(const Date &maturityDate, Spread spread, const Handle< YieldTermStructure > &fwdCurve=Handle< YieldTermStructure >(), const Date &startDate=Date(), const Date &issueDate=Date())
Definition: btp.cpp:33
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
6-months Euribor index
Definition: euribor.hpp:116
floating-rate bond (possibly capped and/or floored)
Shared handle to an observable.
Definition: handle.hpp:41
MakeVanillaSwap & withDiscountingTermStructure(const Handle< YieldTermStructure > &discountCurve)
Calendar for reproducing theoretical calculations.
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
const std::vector< ext::shared_ptr< BTP > > & btps() const
Definition: btp.hpp:204
const std::vector< Real > & outstandings() const
Definition: btp.hpp:103
std::vector< ext::shared_ptr< BTP > > btps_
Definition: btp.hpp:112
std::vector< Real > weights_
Definition: btp.hpp:117
std::vector< Real > outstandings_
Definition: btp.hpp:113
RendistatoBasket(const std::vector< ext::shared_ptr< BTP > > &btps, const std::vector< Real > &outstandings, std::vector< Handle< Quote > > cleanPriceQuotes)
Definition: btp.cpp:92
std::vector< Handle< Quote > > quotes_
Definition: btp.hpp:114
void performCalculations() const override
Definition: btp.cpp:156
Handle< YieldTermStructure > discountCurve_
Definition: btp.hpp:155
std::vector< Time > swapBondDurations_
Definition: btp.hpp:165
ext::shared_ptr< Euribor > euriborIndex_
Definition: btp.hpp:154
std::vector< Time > swapLengths_
Definition: btp.hpp:164
std::vector< Rate > swapBondYields_
Definition: btp.hpp:166
std::vector< ext::shared_ptr< VanillaSwap > > swaps_
Definition: btp.hpp:163
std::vector< Time > durations_
Definition: btp.hpp:158
RendistatoCalculator(ext::shared_ptr< RendistatoBasket > basket, ext::shared_ptr< Euribor > euriborIndex, Handle< YieldTermStructure > discountCurve)
Definition: btp.cpp:134
std::vector< Rate > yields_
Definition: btp.hpp:157
std::vector< Rate > swapRates_
Definition: btp.hpp:166
ext::shared_ptr< RendistatoBasket > basket_
Definition: btp.hpp:153
RendistatoEquivalentSwapLengthQuote(ext::shared_ptr< RendistatoCalculator > r)
Definition: btp.cpp:224
Real value() const override
returns the current value
Definition: btp.hpp:283
bool isValid() const override
returns true if the Quote holds a valid value
Definition: btp.cpp:228
RendistatoEquivalentSwapSpreadQuote(ext::shared_ptr< RendistatoCalculator > r)
Definition: btp.cpp:237
Real value() const override
returns the current value
Definition: btp.hpp:287
bool isValid() const override
returns true if the Quote holds a valid value
Definition: btp.cpp:241
Payment schedule.
Definition: schedule.hpp:40
TARGET calendar
Definition: target.hpp:50
@ Annual
once a year
Definition: frequency.hpp:39
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
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
STL namespace.
static Rate yield(const Bond &bond, Real price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean)
static Time duration(const Bond &bond, const InterestRate &yield, Duration::Type type=Duration::Modified, Date settlementDate=Date())
Date-generation rule.