QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
discountingswapengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007, 2009 StatPro Italia srl
5 Copyright (C) 2011 Ferdinando Ametrano
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
21#include <ql/cashflows/cashflows.hpp>
22#include <ql/pricingengines/swap/discountingswapengine.hpp>
23#include <ql/utilities/dataformatters.hpp>
24#include <ql/optional.hpp>
25#include <utility>
26
27namespace QuantLib {
28
30 Handle<YieldTermStructure> discountCurve,
31 const ext::optional<bool>& includeSettlementDateFlows,
32 Date settlementDate,
33 Date npvDate)
34 : discountCurve_(std::move(discountCurve)),
35 includeSettlementDateFlows_(includeSettlementDateFlows), settlementDate_(settlementDate),
36 npvDate_(npvDate) {
38 }
39
41 QL_REQUIRE(!discountCurve_.empty(),
42 "discounting term structure handle is empty");
43
44 results_.value = 0.0;
46
47 Date refDate = discountCurve_->referenceDate();
48
49 Date settlementDate = settlementDate_;
50 if (settlementDate_==Date()) {
51 settlementDate = refDate;
52 } else {
53 QL_REQUIRE(settlementDate>=refDate,
54 "settlement date (" << settlementDate << ") before "
55 "discount curve reference date (" << refDate << ")");
56 }
57
59 if (npvDate_==Date()) {
60 results_.valuationDate = refDate;
61 } else {
62 QL_REQUIRE(npvDate_>=refDate,
63 "npv date (" << npvDate_ << ") before "
64 "discount curve reference date (" << refDate << ")");
65 }
67
68 Size n = arguments_.legs.size();
69 results_.legNPV.resize(n);
70 results_.legBPS.resize(n);
71 results_.startDiscounts.resize(n);
72 results_.endDiscounts.resize(n);
73
74 bool includeRefDateFlows = includeSettlementDateFlows_ ? // NOLINT(readability-implicit-bool-conversion)
77
78 for (Size i=0; i<n; ++i) {
79 try {
80 const YieldTermStructure& discount_ref = **discountCurve_;
81 std::tie(results_.legNPV[i], results_.legBPS[i]) =
83 discount_ref,
84 includeRefDateFlows,
85 settlementDate,
89
90 if (!arguments_.legs[i].empty()) {
92 if (d1>=refDate)
93 results_.startDiscounts[i] = discountCurve_->discount(d1);
94 else
96
98 if (d2>=refDate)
99 results_.endDiscounts[i] = discountCurve_->discount(d2);
100 else
102 } else {
105 }
106
107 } catch (std::exception &e) {
108 QL_FAIL(io::ordinal(i+1) << " leg: " << e.what());
109 }
111 }
112 }
113
114}
static Date maturityDate(const Leg &leg)
Definition: cashflows.cpp:52
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
static std::pair< Real, Real > npvbps(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
NPV and BPS of the cash flows.
Definition: cashflows.cpp:484
Concrete date class.
Definition: date.hpp:125
Handle< YieldTermStructure > discountCurve_
DiscountingSwapEngine(Handle< YieldTermStructure > discountCurve=Handle< YieldTermStructure >(), const ext::optional< bool > &includeSettlementDateFlows=ext::nullopt, Date settlementDate=Date(), Date npvDate=Date())
ext::optional< bool > includeSettlementDateFlows_
Shared handle to an observable.
Definition: handle.hpp:41
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
bool & includeReferenceDateEvents()
Definition: settings.hpp:155
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
std::vector< Real > payer
Definition: swap.hpp:145
std::vector< Leg > legs
Definition: swap.hpp:144
std::vector< Real > legBPS
Definition: swap.hpp:152
DiscountFactor npvDateDiscount
Definition: swap.hpp:154
std::vector< DiscountFactor > endDiscounts
Definition: swap.hpp:153
std::vector< DiscountFactor > startDiscounts
Definition: swap.hpp:153
std::vector< Real > legNPV
Definition: swap.hpp:151
Interest-rate term structure.
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.