QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
riskybondengine.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) 2008 Roland Lichters
5 Copyright (C) 2021 Lew Wei Hao
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
24#include <utility>
25
26
27namespace QuantLib {
28
30 Real recoveryRate,
32 : defaultTS_(std::move(defaultTS)),
33 recoveryRate_(recoveryRate),
34 yieldTS_(std::move(yieldTS)) {
37 }
38
40 Date npvDate = yieldTS()->referenceDate();
41 Date settlementDate = arguments_.settlementDate;
43 Date d1 = std::max(npvDate, startDate);
44
45 Real NPV = 0.0;
46 Real settlementValue = 0.0;
47 for (auto& cf : arguments_.cashflows) {
48 Date d2 = cf->date();
49 if (d2 > npvDate) {
50
51 Real weightedCouponAmount = cf->amount() * defaultTS()->survivalProbability(d2);
52 NPV += weightedCouponAmount * yieldTS()->discount(d2);
53 if (d2 > settlementDate)
54 settlementValue += weightedCouponAmount * yieldTS()->discount(d2);
55
56 auto coupon = ext::dynamic_pointer_cast<Coupon>(cf);
57 if (coupon != nullptr) {
58 Date defaultDate = d1 + (d2 - d1) / 2;
59 Real weightedRecovery = coupon->nominal() * recoveryRate() *
60 (defaultTS()->survivalProbability(d1) -
61 defaultTS()->survivalProbability(d2));
62 NPV += weightedRecovery * yieldTS()->discount(defaultDate);
63 if (d2 > settlementDate)
64 settlementValue += weightedRecovery * yieldTS()->discount(defaultDate);
65
66 d1 = d2;
67 }
68 }
69 }
70
71 results_.value = NPV;
72 results_.settlementValue = settlementValue / yieldTS()->discount(settlementDate);
73 results_.valuationDate = npvDate;
74 }
75
76}
Cash-flow analysis functions.
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Handle< DefaultProbabilityTermStructure > defaultTS() const
void calculate() const override
Handle< DefaultProbabilityTermStructure > defaultTS_
Handle< YieldTermStructure > yieldTS() const
RiskyBondEngine(Handle< DefaultProbabilityTermStructure > defaultTS, Real recoveryRate, Handle< YieldTermStructure > yieldTS)
Handle< YieldTermStructure > yieldTS_
Coupon accruing over a fixed period.
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
STL namespace.
risky bond engine