Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cashflows.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 - 2016 Quaternion Risk Management Ltd.
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <ql/cashflows/floatingratecoupon.hpp>
20
22
23using namespace std;
24
25namespace QuantExt {
26
27Real CashFlows::spreadNpv(const Leg& leg, const YieldTermStructure& discountCurve, bool includeSettlementDateFlows,
28 Date settlementDate, Date npvDate) {
29
30 if (leg.empty())
31 return 0.0;
32
33 if (settlementDate == Date())
34 settlementDate = Settings::instance().evaluationDate();
35
36 if (npvDate == Date())
37 npvDate = settlementDate;
38
39 Real spreadNpv = 0.0;
40 for (Size i = 0; i < leg.size(); ++i) {
41
42 QuantLib::ext::shared_ptr<FloatingRateCoupon> floatCoupon = QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(leg[i]);
43
44 if (floatCoupon && !floatCoupon->hasOccurred(settlementDate, includeSettlementDateFlows)) {
45
46 spreadNpv += floatCoupon->nominal() * floatCoupon->accrualPeriod() * floatCoupon->spread() *
47 discountCurve.discount(floatCoupon->date());
48 }
49 }
50
51 return spreadNpv / discountCurve.discount(npvDate);
52}
53
54Real CashFlows::sumCashflows(const Leg& leg, const Date& startDate, const Date& endDate) {
55
56 // Empty leg return 0
57 if (leg.empty())
58 return 0.0;
59
60 // If leg is not empty
61 Real sumCashflows = 0.0;
62 for (Size i = 0; i < leg.size(); ++i) {
63 Date cashflowDate = leg[i]->date();
64 if (startDate < cashflowDate && cashflowDate <= endDate)
65 sumCashflows += leg[i]->amount();
66 }
67 return sumCashflows;
68}
69
70vector<Rate> CashFlows::couponRates(const Leg& leg) {
71
72 vector<Rate> couponRates;
73 // Non-empty leg
74 for (Size i = 0; i < leg.size(); ++i) {
75 QuantLib::ext::shared_ptr<Coupon> coupon = QuantLib::ext::dynamic_pointer_cast<Coupon>(leg[i]);
76 if (coupon)
77 couponRates.push_back(coupon->rate());
78 }
79 return couponRates;
80}
81
82vector<Rate> CashFlows::couponDcfRates(const Leg& leg) {
83
84 vector<Rate> couponDcfRates;
85 // Non-empty leg
86 for (Size i = 0; i < leg.size(); ++i) {
87 QuantLib::ext::shared_ptr<Coupon> coupon = QuantLib::ext::dynamic_pointer_cast<Coupon>(leg[i]);
88 if (coupon)
89 couponDcfRates.push_back(coupon->rate() * coupon->accrualPeriod());
90 }
91 return couponDcfRates;
92}
93
94} // namespace QuantExt
additional cash-flow analysis functions
static Real spreadNpv(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date())
NPV due to any spreads on a leg.
Definition: cashflows.cpp:27
static Real sumCashflows(const Leg &leg, const Date &startDate, const Date &endDate)
Return the sum of the cashflows on leg after startDate and before or on endDate.
Definition: cashflows.cpp:54
static std::vector< Rate > couponDcfRates(const Leg &leg)
Definition: cashflows.cpp:82
static std::vector< Rate > couponRates(const Leg &leg)
Definition: cashflows.cpp:70