QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
bondfunctions.cpp
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
26#include <ql/math/solvers1d/newtonsafe.hpp>
27#include <ql/pricingengines/bond/bondfunctions.hpp>
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 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
246 "non tradable at " << settlement <<
247 " settlement date (maturity being " <<
248 bond.maturityDate() << ")");
249
250 Real dirtyPrice = CashFlows::npv(bond.cashflows(), discountCurve,
251 false, settlement) *
252 100.0 / bond.notional(settlement);
253 return dirtyPrice - bond.accruedAmount(settlement);
254 }
255
257 const YieldTermStructure& discountCurve,
258 Date settlement) {
259 if (settlement == Date())
260 settlement = bond.settlementDate();
261
262 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
263 "non tradable at " << settlement <<
264 " (maturity being " << bond.maturityDate() << ")");
265
266 return CashFlows::bps(bond.cashflows(), discountCurve,
267 false, settlement) *
268 100.0 / bond.notional(settlement);
269 }
270
272 const YieldTermStructure& discountCurve,
273 Date settlement,
274 Real cleanPrice) {
275 if (settlement == Date())
276 settlement = bond.settlementDate();
277
278 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
279 "non tradable at " << settlement <<
280 " (maturity being " << bond.maturityDate() << ")");
281
283 cleanPrice + bond.accruedAmount(settlement);
284 Real currentNotional = bond.notional(settlement);
285 Real npv = dirtyPrice==Null<Real>() ? Null<Real>() :
286 dirtyPrice/100.0 * currentNotional;
287
288 return CashFlows::atmRate(bond.cashflows(), discountCurve,
289 false, settlement, settlement,
290 npv);
291 }
292
294 const InterestRate& yield,
295 Date settlement) {
296 return dirtyPrice(bond, yield, settlement) - bond.accruedAmount(settlement);
297 }
298
300 Rate yield,
301 const DayCounter& dayCounter,
302 Compounding compounding,
303 Frequency frequency,
304 Date settlement) {
305 InterestRate y(yield, dayCounter, compounding, frequency);
306 return cleanPrice(bond, y, settlement);
307 }
308
310 const InterestRate& yield,
311 Date settlement) {
312 if (settlement == Date())
313 settlement = bond.settlementDate();
314
315 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
316 "non tradable at " << settlement <<
317 " (maturity being " << bond.maturityDate() << ")");
318
320 false, settlement) *
321 100.0 / bond.notional(settlement);
322 return dirtyPrice;
323 }
324
326 Rate yield,
327 const DayCounter& dayCounter,
328 Compounding compounding,
329 Frequency frequency,
330 Date settlement) {
331 InterestRate y(yield, dayCounter, compounding, frequency);
332 return dirtyPrice(bond, y, settlement);
333 }
334
336 const InterestRate& yield,
337 Date settlement) {
338 if (settlement == Date())
339 settlement = bond.settlementDate();
340
341 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
342 "non tradable at " << settlement <<
343 " (maturity being " << bond.maturityDate() << ")");
344
345 return CashFlows::bps(bond.cashflows(), yield,
346 false, settlement) *
347 100.0 / bond.notional(settlement);
348 }
349
351 Rate yield,
352 const DayCounter& dayCounter,
353 Compounding compounding,
354 Frequency frequency,
355 Date settlement) {
356 InterestRate y(yield, dayCounter, compounding, frequency);
357 return bps(bond, y, settlement);
358 }
359
361 Real price,
362 const DayCounter& dayCounter,
363 Compounding compounding,
364 Frequency frequency,
365 Date settlement,
366 Real accuracy,
367 Size maxIterations,
368 Rate guess,
369 Bond::Price::Type priceType) {
370 NewtonSafe solver;
371 solver.setMaxEvaluations(maxIterations);
372 return yield<NewtonSafe>(solver, bond, price, dayCounter,
373 compounding, frequency, settlement,
374 accuracy, guess, priceType);
375 }
376
378 const InterestRate& yield,
379 Duration::Type type,
380 Date settlement) {
381 if (settlement == Date())
382 settlement = bond.settlementDate();
383
384 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
385 "non tradable at " << settlement <<
386 " (maturity being " << bond.maturityDate() << ")");
387
388 return CashFlows::duration(bond.cashflows(), yield,
389 type,
390 false, settlement);
391 }
392
394 Rate yield,
395 const DayCounter& dayCounter,
396 Compounding compounding,
397 Frequency frequency,
398 Duration::Type type,
399 Date settlement) {
400 InterestRate y(yield, dayCounter, compounding, frequency);
401 return duration(bond, y, type, settlement);
402 }
403
405 const InterestRate& yield,
406 Date settlement) {
407 if (settlement == Date())
408 settlement = bond.settlementDate();
409
410 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
411 "non tradable at " << settlement <<
412 " (maturity being " << bond.maturityDate() << ")");
413
414 return CashFlows::convexity(bond.cashflows(), yield,
415 false, settlement);
416 }
417
419 Rate yield,
420 const DayCounter& dayCounter,
421 Compounding compounding,
422 Frequency frequency,
423 Date settlement) {
424 InterestRate y(yield, dayCounter, compounding, frequency);
425 return convexity(bond, y, settlement);
426 }
427
429 const InterestRate& yield,
430 Date settlement) {
431 if (settlement == Date())
432 settlement = bond.settlementDate();
433
434 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
435 "non tradable at " << settlement <<
436 " (maturity being " << bond.maturityDate() << ")");
437
439 false, settlement);
440 }
441
443 Rate yield,
444 const DayCounter& dayCounter,
445 Compounding compounding,
446 Frequency frequency,
447 Date settlement) {
448 InterestRate y(yield, dayCounter, compounding, frequency);
449 return CashFlows::basisPointValue(bond.cashflows(), y,
450 false, settlement);
451 }
452
454 const InterestRate& yield,
455 Date settlement) {
456 if (settlement == Date())
457 settlement = bond.settlementDate();
458
459 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
460 "non tradable at " << settlement <<
461 " (maturity being " << bond.maturityDate() << ")");
462
464 false, settlement);
465 }
466
468 Rate yield,
469 const DayCounter& dayCounter,
470 Compounding compounding,
471 Frequency frequency,
472 Date settlement) {
473 InterestRate y(yield, dayCounter, compounding, frequency);
475 false, settlement);
476 }
477
479 const ext::shared_ptr<YieldTermStructure>& d,
480 Spread zSpread,
481 const DayCounter& dc,
482 Compounding comp,
483 Frequency freq,
484 Date settlement) {
485 if (settlement == Date())
486 settlement = bond.settlementDate();
487
488 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
489 "non tradable at " << settlement <<
490 " (maturity being " << bond.maturityDate() << ")");
491
493 zSpread, dc, comp, freq,
494 false, settlement) *
495 100.0 / bond.notional(settlement);
496 return dirtyPrice - bond.accruedAmount(settlement);
497 }
498
500 Real cleanPrice,
501 const ext::shared_ptr<YieldTermStructure>& d,
502 const DayCounter& dayCounter,
503 Compounding compounding,
504 Frequency frequency,
505 Date settlement,
506 Real accuracy,
507 Size maxIterations,
508 Rate guess) {
509 if (settlement == Date())
510 settlement = bond.settlementDate();
511
512 QL_REQUIRE(BondFunctions::isTradable(bond, settlement),
513 "non tradable at " << settlement <<
514 " (maturity being " << bond.maturityDate() << ")");
515
516 Real dirtyPrice = cleanPrice + bond.accruedAmount(settlement);
517 dirtyPrice /= 100.0 / bond.notional(settlement);
518
519 return CashFlows::zSpread(bond.cashflows(),
520 d,
522 dayCounter, compounding, frequency,
523 false, settlement, settlement,
524 accuracy, maxIterations, guess);
525 }
526
527}
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:150
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:112
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:161
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:1116
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:1069
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:1239
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:936
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:521
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:985
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:76
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Interest-rate term structure.
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:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
static Real yieldValueBasisPoint(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
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 Leg::const_iterator nextCashFlow(const Bond &bond, Date refDate=Date())
static Spread zSpread(const Bond &bond, Real cleanPrice, 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 Leg::const_reverse_iterator previousCashFlow(const Bond &bond, Date refDate=Date())
static Rate atmRate(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date(), Real cleanPrice=Null< Real >())
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 InterestRate &yield, 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 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 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())