/src/quantlib/ql/pricingengines/bond/discountingbondengine.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2007 Giorgio Facchinetti |
5 | | Copyright (C) 2009 StatPro Italia srl |
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 | | <https://www.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 | | |
21 | | #include <ql/cashflows/cashflows.hpp> |
22 | | #include <ql/pricingengines/bond/discountingbondengine.hpp> |
23 | | #include <ql/optional.hpp> |
24 | | #include <utility> |
25 | | |
26 | | namespace QuantLib { |
27 | | |
28 | | DiscountingBondEngine::DiscountingBondEngine( |
29 | | Handle<YieldTermStructure> discountCurve, |
30 | | const ext::optional<bool>& includeSettlementDateFlows) |
31 | 0 | : discountCurve_(std::move(discountCurve)), |
32 | 0 | includeSettlementDateFlows_(includeSettlementDateFlows) { |
33 | 0 | registerWith(discountCurve_); |
34 | 0 | } |
35 | | |
36 | 0 | void DiscountingBondEngine::calculate() const { |
37 | 0 | QL_REQUIRE(!discountCurve_.empty(), |
38 | 0 | "discounting term structure handle is empty"); |
39 | | |
40 | 0 | results_.valuationDate = (*discountCurve_)->referenceDate(); |
41 | |
|
42 | 0 | bool includeRefDateFlows = includeSettlementDateFlows_ ? // NOLINT(readability-implicit-bool-conversion) |
43 | 0 | *includeSettlementDateFlows_ : |
44 | 0 | Settings::instance().includeReferenceDateEvents(); |
45 | |
|
46 | 0 | results_.value = CashFlows::npv(arguments_.cashflows, |
47 | 0 | **discountCurve_, |
48 | 0 | includeRefDateFlows, |
49 | 0 | results_.valuationDate, |
50 | 0 | results_.valuationDate); |
51 | | |
52 | | // a bond's cashflow on settlement date is never taken into |
53 | | // account, so we might have to play it safe and recalculate |
54 | 0 | if (!includeRefDateFlows |
55 | 0 | && results_.valuationDate == arguments_.settlementDate) { |
56 | | // same parameters as above, we can avoid another call |
57 | 0 | results_.settlementValue = results_.value; |
58 | 0 | } else { |
59 | | // no such luck |
60 | 0 | results_.settlementValue = |
61 | 0 | CashFlows::npv(arguments_.cashflows, |
62 | 0 | **discountCurve_, |
63 | 0 | false, |
64 | 0 | arguments_.settlementDate, |
65 | 0 | arguments_.settlementDate); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | } |