QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
btp.cpp
Go to the documentation of this file.
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
27#include <ql/time/schedule.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 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) {
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(), Bond::Price::Clean},
164 ActualActual(ActualActual::ISMA), Compounded, Annual, bondSettlementDate,
165 // accuracy, maxIterations, guess
166 1.0e-10, 100, yields_[i]);
168 *btps[i], yields_[i],
170 Duration::Modified, bondSettlementDate);
171 }
172 duration_ = std::inner_product(basket_->weights().begin(),
173 basket_->weights().end(),
174 durations_.begin(), Real(0.0));
175
176 Natural settlDays = 2;
177 DayCounter fixedDayCount = swaps_[0]->fixedDayCount();
179 swapRates_[0]= swaps_[0]->fairRate();
180 FixedRateBond swapBond(settlDays,
181 100.0, // faceAmount
182 swaps_[0]->fixedSchedule(),
183 std::vector<Rate>(1, swapRates_[0]),
184 fixedDayCount,
185 Following, // paymentConvention
186 100.0); // redemption
188 {100.0, Bond::Price::Clean}, // floating leg NPV including end payment
190 bondSettlementDate,
191 // accuracy, maxIterations, guess
192 1.0e-10, 100, swapBondYields_[0]);
194 swapBond, swapBondYields_[0],
196 Duration::Modified, bondSettlementDate);
197 for (Size i=1; i<nSwaps_; ++i) {
198 swapRates_[i]= swaps_[i]->fairRate();
199 FixedRateBond swapBond(settlDays,
200 100.0, // faceAmount
201 swaps_[i]->fixedSchedule(),
202 std::vector<Rate>(1, swapRates_[i]),
203 fixedDayCount,
204 Following, // paymentConvention
205 100.0); // redemption
207 {100.0, Bond::Price::Clean}, // floating leg NPV including end payment
209 bondSettlementDate,
210 // accuracy, maxIterations, guess
211 1.0e-10, 100, swapBondYields_[i]);
213 swapBond, swapBondYields_[i],
215 Duration::Modified, bondSettlementDate);
216 if (swapBondDurations_[i] > duration_) {
218 break; // exit the loop
219 }
220 }
221 }
222
224 ext::shared_ptr<RendistatoCalculator> r)
225 : r_(std::move(r)) {}
226
228 try {
229 value();
230 return true;
231 } catch (...) {
232 return false;
233 }
234 }
235
237 ext::shared_ptr<RendistatoCalculator> r)
238 : r_(std::move(r)) {}
239
241 try {
242 value();
243 return true;
244 } catch (...) {
245 return false;
246 }
247 }
248}
act/360 day counter
act/act day counters
bond functions
Italian BTP (Buoni Poliennali del Tesoro) fixed rate bond.
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
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:132
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:223
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:227
RendistatoEquivalentSwapSpreadQuote(ext::shared_ptr< RendistatoCalculator > r)
Definition: btp.cpp:236
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:240
Payment schedule.
Definition: schedule.hpp:40
TARGET calendar
Definition: target.hpp:50
output manipulators
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
@ 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
Helper class to instantiate standard market swaps.
Definition: any.hpp:35
STL namespace.
Calendar for reproducing theoretical calculations.
ext::shared_ptr< YieldTermStructure > r
date schedule
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.
TARGET calendar.