QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
bondfunctions.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) 2009 Nathan Abbott
5 Copyright (C) 2007, 2008, 2009, 2010 Ferdinando Ametrano
6 Copyright (C) 2007 Chiara Fornarola
7 Copyright (C) 2008 Simon Ibbotson
8 Copyright (C) 2004 M-Dimension Consulting Inc.
9 Copyright (C) 2005, 2006, 2007, 2008, 2009 StatPro Italia srl
10 Copyright (C) 2004 Jeff Yu
11
12 This file is part of QuantLib, a free-software/open-source library
13 for financial quantitative analysts and developers - http://quantlib.org/
14
15 QuantLib is free software: you can redistribute it and/or modify it
16 under the terms of the QuantLib license. You should have received a
17 copy of the license along with this program; if not, please email
18 <quantlib-dev@lists.sf.net>. The license is also available online at
19 <http://quantlib.org/license.shtml>.
20
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the license for more details.
24*/
25
28
29namespace QuantLib {
30
32 return CashFlows::startDate(bond.cashflows());
33 }
34
37 }
38
40 Date settlement) {
41 if (settlement == Date())
42 settlement = bond.settlementDate();
43
44 return bond.notional(settlement)!=0.0;
45 }
46
47 Leg::const_reverse_iterator
49 Date settlement) {
50 if (settlement == Date())
51 settlement = bond.settlementDate();
52
54 false, settlement);
55 }
56
57 Leg::const_iterator BondFunctions::nextCashFlow(const Bond& bond,
58 Date settlement) {
59 if (settlement == Date())
60 settlement = bond.settlementDate();
61
63 false, settlement);
64 }
65
67 Date settlement) {
68 if (settlement == Date())
69 settlement = bond.settlementDate();
70
72 false, settlement);
73 }
74
76 Date settlement) {
77 if (settlement == Date())
78 settlement = bond.settlementDate();
79
81 false, settlement);
82 }
83
85 Date settlement) {
86 if (settlement == Date())
87 settlement = bond.settlementDate();
88
90 false, settlement);
91 }
92
94 Date settlement) {
95 if (settlement == Date())
96 settlement = bond.settlementDate();
97
99 false, settlement);
100 }
101
103 Date settlement) {
104 if (settlement == Date())
105 settlement = bond.settlementDate();
106
108 false, settlement);
109 }
110
112 Date settlement) {
113 if (settlement == Date())
114 settlement = bond.settlementDate();
115
117 false, settlement);
118 }
119
121 Date settlement) {
122 if (settlement == Date())
123 settlement = bond.settlementDate();
124
125 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
126 "non tradable at " << settlement <<
127 " (maturity being " << bond.maturityDate() << ")");
128
130 false, settlement);
131 }
132
134 Date settlement) {
135 if (settlement == Date())
136 settlement = bond.settlementDate();
137
138 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
139 "non tradable at " << settlement <<
140 " (maturity being " << bond.maturityDate() << ")");
141
143 false, settlement);
144 }
145
147 Date settlement) {
148 if (settlement == Date())
149 settlement = bond.settlementDate();
150
151 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
152 "non tradable at " << settlement <<
153 " (maturity being " << bond.maturityDate() << ")");
154
156 false, settlement);
157 }
158
160 Date settlement) {
161 if (settlement == Date())
162 settlement = bond.settlementDate();
163
164 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
165 "non tradable at " << settlement <<
166 " (maturity being " << bond.maturityDate() << ")");
167
169 false, settlement);
170 }
171
173 Date settlement) {
174 if (settlement == Date())
175 settlement = bond.settlementDate();
176
177 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
178 "non tradable at " << settlement <<
179 " (maturity being " << bond.maturityDate() << ")");
180
182 false, settlement);
183 }
184
186 Date settlement) {
187 if (settlement == Date())
188 settlement = bond.settlementDate();
189
190 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
191 "non tradable at " << settlement <<
192 " (maturity being " << bond.maturityDate() << ")");
193
194 return CashFlows::accrualDays(bond.cashflows(),
195 false, settlement);
196 }
197
199 Date settlement) {
200 if (settlement == Date())
201 settlement = bond.settlementDate();
202
203 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
204 "non tradable at " << settlement <<
205 " (maturity being " << bond.maturityDate() << ")");
206
208 false, settlement);
209 }
210
212 Date settlement) {
213 if (settlement == Date())
214 settlement = bond.settlementDate();
215
216 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
217 "non tradable at " << settlement <<
218 " (maturity being " << bond.maturityDate() << ")");
219
220 return CashFlows::accruedDays(bond.cashflows(),
221 false, settlement);
222 }
223
225 Date settlement) {
226 if (settlement == Date())
227 settlement = bond.settlementDate();
228
229 if (!BondFunctions::isTradable(bond, settlement))
230 return 0.0;
231
233 false, settlement) *
234 100.0 / bond.notional(settlement);
235 }
236
237
238
240 const YieldTermStructure& discountCurve,
241 Date settlement) {
242 if (settlement == Date())
243 settlement = bond.settlementDate();
244
245 return dirtyPrice(bond, discountCurve, settlement) - bond.accruedAmount(settlement);
246 }
247
249 const YieldTermStructure& discountCurve,
250 Date settlement) {
251 if (settlement == Date())
252 settlement = bond.settlementDate();
253
254 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
255 "non tradable at " << settlement <<
256 " settlement date (maturity being " <<
257 bond.maturityDate() << ")");
258
259 Real dirtyPrice = CashFlows::npv(bond.cashflows(), discountCurve,
260 false, settlement) *
261 100.0 / bond.notional(settlement);
262 return dirtyPrice;
263 }
264
266 const YieldTermStructure& discountCurve,
267 Date settlement) {
268 if (settlement == Date())
269 settlement = bond.settlementDate();
270
271 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
272 "non tradable at " << settlement <<
273 " (maturity being " << bond.maturityDate() << ")");
274
275 return CashFlows::bps(bond.cashflows(), discountCurve,
276 false, settlement) *
277 100.0 / bond.notional(settlement);
278 }
279
281 const YieldTermStructure& discountCurve,
282 Date settlement,
283 const Bond::Price price) {
284 if (settlement == Date())
285 settlement = bond.settlementDate();
286
287 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
288 "non tradable at " << settlement <<
289 " (maturity being " << bond.maturityDate() << ")");
290
291 Real npv = Null<Real>();
292 if (price.isValid()) {
294 price.amount() +
295 (price.type() == Bond::Price::Clean ? bond.accruedAmount(settlement) : 0);
296
297 Real currentNotional = bond.notional(settlement);
298 npv = dirtyPrice / 100.0 * currentNotional;
299
300 }
301 return CashFlows::atmRate(bond.cashflows(), discountCurve,
302 false, settlement, settlement,
303 npv);
304 }
305
307 const InterestRate& yield,
308 Date settlement) {
309 return dirtyPrice(bond, yield, settlement) - bond.accruedAmount(settlement);
310 }
311
313 Rate yield,
314 const DayCounter& dayCounter,
315 Compounding compounding,
316 Frequency frequency,
317 Date settlement) {
318 InterestRate y(yield, dayCounter, compounding, frequency);
319 return cleanPrice(bond, y, settlement);
320 }
321
323 const InterestRate& yield,
324 Date settlement) {
325 if (settlement == Date())
326 settlement = bond.settlementDate();
327
328 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
329 "non tradable at " << settlement <<
330 " (maturity being " << bond.maturityDate() << ")");
331
333 false, settlement) *
334 100.0 / bond.notional(settlement);
335 return dirtyPrice;
336 }
337
339 Rate yield,
340 const DayCounter& dayCounter,
341 Compounding compounding,
342 Frequency frequency,
343 Date settlement) {
344 InterestRate y(yield, dayCounter, compounding, frequency);
345 return dirtyPrice(bond, y, settlement);
346 }
347
349 const InterestRate& yield,
350 Date settlement) {
351 if (settlement == Date())
352 settlement = bond.settlementDate();
353
354 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
355 "non tradable at " << settlement <<
356 " (maturity being " << bond.maturityDate() << ")");
357
358 return CashFlows::bps(bond.cashflows(), yield,
359 false, settlement) *
360 100.0 / bond.notional(settlement);
361 }
362
364 Rate yield,
365 const DayCounter& dayCounter,
366 Compounding compounding,
367 Frequency frequency,
368 Date settlement) {
369 InterestRate y(yield, dayCounter, compounding, frequency);
370 return bps(bond, y, settlement);
371 }
372
374 Bond::Price price,
375 const DayCounter& dayCounter,
376 Compounding compounding,
377 Frequency frequency,
378 Date settlement,
379 Real accuracy,
380 Size maxIterations,
381 Rate guess) {
382 NewtonSafe solver;
383 solver.setMaxEvaluations(maxIterations);
384 return yield<NewtonSafe>(solver, bond, price, dayCounter,
385 compounding, frequency, settlement,
386 accuracy, guess);
387 }
388
390 const InterestRate& yield,
391 Duration::Type type,
392 Date settlement) {
393 if (settlement == Date())
394 settlement = bond.settlementDate();
395
396 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
397 "non tradable at " << settlement <<
398 " (maturity being " << bond.maturityDate() << ")");
399
400 return CashFlows::duration(bond.cashflows(), yield,
401 type,
402 false, settlement);
403 }
404
406 Rate yield,
407 const DayCounter& dayCounter,
408 Compounding compounding,
409 Frequency frequency,
410 Duration::Type type,
411 Date settlement) {
412 InterestRate y(yield, dayCounter, compounding, frequency);
413 return duration(bond, y, type, settlement);
414 }
415
417 const InterestRate& yield,
418 Date settlement) {
419 if (settlement == Date())
420 settlement = bond.settlementDate();
421
422 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
423 "non tradable at " << settlement <<
424 " (maturity being " << bond.maturityDate() << ")");
425
426 return CashFlows::convexity(bond.cashflows(), yield,
427 false, settlement);
428 }
429
431 Rate yield,
432 const DayCounter& dayCounter,
433 Compounding compounding,
434 Frequency frequency,
435 Date settlement) {
436 InterestRate y(yield, dayCounter, compounding, frequency);
437 return convexity(bond, y, settlement);
438 }
439
441 const InterestRate& yield,
442 Date settlement) {
443 if (settlement == Date())
444 settlement = bond.settlementDate();
445
446 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
447 "non tradable at " << settlement <<
448 " (maturity being " << bond.maturityDate() << ")");
449
451 false, settlement);
452 }
453
455 Rate yield,
456 const DayCounter& dayCounter,
457 Compounding compounding,
458 Frequency frequency,
459 Date settlement) {
460 InterestRate y(yield, dayCounter, compounding, frequency);
461 return basisPointValue(bond, y, settlement);
462 }
463
465 const InterestRate& yield,
466 Date settlement) {
467 if (settlement == Date())
468 settlement = bond.settlementDate();
469
470 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
471 "non tradable at " << settlement <<
472 " (maturity being " << bond.maturityDate() << ")");
473
475 false, settlement);
476 }
477
479 Rate yield,
480 const DayCounter& dayCounter,
481 Compounding compounding,
482 Frequency frequency,
483 Date settlement) {
484 InterestRate y(yield, dayCounter, compounding, frequency);
485 return yieldValueBasisPoint(bond, y, settlement);
486 }
487
489 const ext::shared_ptr<YieldTermStructure>& d,
490 Spread zSpread,
491 const DayCounter& dc,
492 Compounding comp,
493 Frequency freq,
494 Date settlement) {
495 if (settlement == Date())
496 settlement = bond.settlementDate();
497
498 return dirtyPrice(bond, d, zSpread, dc, comp, freq, settlement) - bond.accruedAmount(settlement);
499 }
500
502 const ext::shared_ptr<YieldTermStructure>& d,
503 Spread zSpread,
504 const DayCounter& dc,
505 Compounding comp,
506 Frequency freq,
507 Date settlement) {
508 if (settlement == Date())
509 settlement = bond.settlementDate();
510
511 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
512 "non tradable at " << settlement <<
513 " (maturity being " << bond.maturityDate() << ")");
514
516 zSpread, dc, comp, freq,
517 false, settlement) *
518 100.0 / bond.notional(settlement);
519 return dirtyPrice;
520 }
521
523 Bond::Price price,
524 const ext::shared_ptr<YieldTermStructure>& d,
525 const DayCounter& dayCounter,
526 Compounding compounding,
527 Frequency frequency,
528 Date settlement,
529 Real accuracy,
530 Size maxIterations,
531 Rate guess) {
532 if (settlement == Date())
533 settlement = bond.settlementDate();
534
535 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
536 "non tradable at " << settlement <<
537 " (maturity being " << bond.maturityDate() << ")");
538
540 price.amount() +
541 (price.type() == Bond::Price::Clean ? bond.accruedAmount(settlement) : 0);
542
543 dirtyPrice /= 100.0 / bond.notional(settlement);
544
545 return CashFlows::zSpread(bond.cashflows(),
547 d,
548 dayCounter, compounding, frequency,
549 false, settlement, settlement,
550 accuracy, maxIterations, guess);
551 }
552}
bond functions
Bond price information.
Definition: bond.hpp:62
bool isValid() const
Definition: bond.hpp:72
Type type() const
Definition: bond.hpp:71
Real amount() const
Definition: bond.hpp:67
Base bond class.
Definition: bond.hpp:59
virtual Real accruedAmount(Date d=Date()) const
accrued amount at a given date
Definition: bond.cpp:256
const Leg & cashflows() const
Definition: bond.hpp:330
Date maturityDate() const
Definition: bond.cpp:151
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:113
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:162
static Date::serial_type accruedDays(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:359
static Real previousCashFlowAmount(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:143
static Rate previousCouponRate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:215
static Rate nextCouponRate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:224
static Date referencePeriodStart(const Leg &leg, bool includeSettlementDateFlows, Date settlDate=Date())
Definition: cashflows.cpp:279
static Real nextCashFlowAmount(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:159
static Real yieldValueBasisPoint(const Leg &leg, const InterestRate &yield, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
Yield value of a basis point.
Definition: cashflows.cpp:1105
static Real accruedAmount(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:377
static Real basisPointValue(const Leg &leg, const InterestRate &yield, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
Basis-point value.
Definition: cashflows.cpp:1058
static Leg::const_iterator nextCashFlow(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
the first cashflow paying after the given date
Definition: cashflows.cpp:102
static Spread zSpread(const Leg &leg, Real npv, const ext::shared_ptr< YieldTermStructure > &, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.0)
implied Z-spread.
Definition: cashflows.cpp:1228
static Real npv(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
NPV of the cash flows.
Definition: cashflows.cpp:425
static Date accrualEndDate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:263
static Date accrualStartDate(const Leg &leg, bool includeSettlementDateFlows, Date settlDate=Date())
Definition: cashflows.cpp:247
static Leg::const_reverse_iterator previousCashFlow(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
the last cashflow paying before or at the given date
Definition: cashflows.cpp:84
static Date nextCashFlowDate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:131
static Time accrualPeriod(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:311
static Date::serial_type accrualDays(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:326
static Date maturityDate(const Leg &leg)
Definition: cashflows.cpp:52
static Time duration(const Leg &leg, const InterestRate &yield, Duration::Type type, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
Cash-flow duration.
Definition: cashflows.cpp:925
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
static Date previousCashFlowDate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:119
static Rate atmRate(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date(), Real npv=Null< Real >())
At-the-money rate of the cash flows.
Definition: cashflows.cpp:510
static Real bps(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
Basis-point sensitivity of the cash flows.
Definition: cashflows.cpp:450
static Date referencePeriodEnd(const Leg &leg, bool includeSettlementDateFlows, Date settlDate=Date())
Definition: cashflows.cpp:295
static Time accruedPeriod(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:341
static Real convexity(const Leg &leg, const InterestRate &yield, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
Cash-flow convexity.
Definition: cashflows.cpp:974
Concrete date class.
Definition: date.hpp:125
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
day counter class
Definition: daycounter.hpp:44
Concrete interest rate class.
safe Newton 1-D solver
Definition: newtonsafe.hpp:40
template class providing a null value for a given type.
Definition: null.hpp:59
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Interest-rate term structure.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
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:37
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
Safe (bracketed) Newton 1-D solver.
static Real yieldValueBasisPoint(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Leg::const_iterator nextCashFlow(const Bond &bond, Date refDate=Date())
static Leg::const_reverse_iterator previousCashFlow(const Bond &bond, Date refDate=Date())
static Rate atmRate(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date(), Bond::Price price={})
static Rate nextCouponRate(const Bond &bond, Date settlementDate=Date())
static Date startDate(const Bond &bond)
static Date referencePeriodStart(const Bond &bond, Date settlementDate=Date())
static bool isTradable(const Bond &bond, Date settlementDate=Date())
static Real bps(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Date maturityDate(const Bond &bond)
static Date::serial_type accruedDays(const Bond &bond, Date settlementDate=Date())
static Real dirtyPrice(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Date previousCashFlowDate(const Bond &bond, Date refDate=Date())
static Real nextCashFlowAmount(const Bond &bond, Date refDate=Date())
static Real convexity(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Real basisPointValue(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Date::serial_type accrualDays(const Bond &bond, Date settlementDate=Date())
static Date nextCashFlowDate(const Bond &bond, Date refDate=Date())
static Date referencePeriodEnd(const Bond &bond, Date settlementDate=Date())
static Spread zSpread(const Bond &bond, Bond::Price price, const ext::shared_ptr< YieldTermStructure > &, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.0)
static Real accruedAmount(const Bond &bond, Date settlementDate=Date())
static Date accrualStartDate(const Bond &bond, Date settlementDate=Date())
static Real cleanPrice(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Rate previousCouponRate(const Bond &bond, Date settlementDate=Date())
static Date accrualEndDate(const Bond &bond, Date settlementDate=Date())
static Rate yield(const Bond &bond, Bond::Price price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05)
static Time duration(const Bond &bond, const InterestRate &yield, Duration::Type type=Duration::Modified, Date settlementDate=Date())
static Time accruedPeriod(const Bond &bond, Date settlementDate=Date())
static Time accrualPeriod(const Bond &bond, Date settlementDate=Date())
static Real previousCashFlowAmount(const Bond &bond, Date refDate=Date())