QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cashflows.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 StatPro Italia srl
5 Copyright (C) 2005 Charles Whitmore
6 Copyright (C) 2007, 2008, 2009, 2010, 2011 Ferdinando Ametrano
7 Copyright (C) 2008 Toyin Akin
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
27#ifndef quantlib_cashflows_hpp
28#define quantlib_cashflows_hpp
29
30#include <ql/cashflows/duration.hpp>
31#include <ql/cashflow.hpp>
32#include <ql/interestrate.hpp>
33#include <ql/shared_ptr.hpp>
34
35namespace QuantLib {
36
37 class YieldTermStructure;
38
40
41 class CashFlows {
42 private:
43 class IrrFinder {
44 public:
45 IrrFinder(const Leg& leg,
46 Real npv,
47 DayCounter dayCounter,
48 Compounding comp,
49 Frequency freq,
50 bool includeSettlementDateFlows,
51 Date settlementDate,
52 Date npvDate);
53
54 Real operator()(Rate y) const;
55 Real derivative(Rate y) const;
56 private:
57 void checkSign() const;
58
59 const Leg& leg_;
66 };
67 public:
68 CashFlows() = delete;
69 CashFlows(CashFlows&&) = delete;
70 CashFlows(const CashFlows&) = delete;
72 CashFlows& operator=(const CashFlows&) = delete;
73 ~CashFlows() = default;
74
76
77 static Date startDate(const Leg& leg);
78 static Date maturityDate(const Leg& leg);
79 static bool isExpired(const Leg& leg,
80 bool includeSettlementDateFlows,
81 Date settlementDate = Date());
83
85
86
87 static Leg::const_reverse_iterator
88 previousCashFlow(const Leg& leg,
89 bool includeSettlementDateFlows,
90 Date settlementDate = Date());
92 static Leg::const_iterator
93 nextCashFlow(const Leg& leg,
94 bool includeSettlementDateFlows,
95 Date settlementDate = Date());
96 static Date
97 previousCashFlowDate(const Leg& leg,
98 bool includeSettlementDateFlows,
99 Date settlementDate = Date());
100 static Date
101 nextCashFlowDate(const Leg& leg,
102 bool includeSettlementDateFlows,
103 Date settlementDate = Date());
104 static Real
105 previousCashFlowAmount(const Leg& leg,
106 bool includeSettlementDateFlows,
107 Date settlementDate = Date());
108 static Real
109 nextCashFlowAmount(const Leg& leg,
110 bool includeSettlementDateFlows,
111 Date settlementDate = Date());
113
115
116 static Rate
117 previousCouponRate(const Leg& leg,
118 bool includeSettlementDateFlows,
119 Date settlementDate = Date());
120 static Rate
121 nextCouponRate(const Leg& leg,
122 bool includeSettlementDateFlows,
123 Date settlementDate = Date());
124
125 static Real
126 nominal(const Leg& leg,
127 bool includeSettlementDateFlows,
128 Date settlDate = Date());
129 static Date
130 accrualStartDate(const Leg& leg,
131 bool includeSettlementDateFlows,
132 Date settlDate = Date());
133 static Date
134 accrualEndDate(const Leg& leg,
135 bool includeSettlementDateFlows,
136 Date settlementDate = Date());
137 static Date
138 referencePeriodStart(const Leg& leg,
139 bool includeSettlementDateFlows,
140 Date settlDate = Date());
141 static Date
142 referencePeriodEnd(const Leg& leg,
143 bool includeSettlementDateFlows,
144 Date settlDate = Date());
145 static Time
146 accrualPeriod(const Leg& leg,
147 bool includeSettlementDateFlows,
148 Date settlementDate = Date());
149 static Date::serial_type
150 accrualDays(const Leg& leg,
151 bool includeSettlementDateFlows,
152 Date settlementDate = Date());
153 static Time
154 accruedPeriod(const Leg& leg,
155 bool includeSettlementDateFlows,
156 Date settlementDate = Date());
157 static Date::serial_type
158 accruedDays(const Leg& leg,
159 bool includeSettlementDateFlows,
160 Date settlementDate = Date());
161 static Real
162 accruedAmount(const Leg& leg,
163 bool includeSettlementDateFlows,
164 Date settlementDate = Date());
166
168
169
173 static Real npv(const Leg& leg,
174 const YieldTermStructure& discountCurve,
175 bool includeSettlementDateFlows,
176 Date settlementDate = Date(),
177 Date npvDate = Date());
179
184 static Real bps(const Leg& leg,
185 const YieldTermStructure& discountCurve,
186 bool includeSettlementDateFlows,
187 Date settlementDate = Date(),
188 Date npvDate = Date());
189
191
194 static std::pair<Real, Real> npvbps(const Leg& leg,
195 const YieldTermStructure& discountCurve,
196 bool includeSettlementDateFlows,
197 Date settlementDate = Date(),
198 Date npvDate = Date());
199
201
204 QL_DEPRECATED
205 static void npvbps(const Leg& leg,
206 const YieldTermStructure& discountCurve,
207 bool includeSettlementDateFlows,
208 Date settlementDate,
209 Date npvDate,
210 Real& npv,
211 Real& bps);
212
214
219 static Rate atmRate(const Leg& leg,
220 const YieldTermStructure& discountCurve,
221 bool includeSettlementDateFlows,
222 Date settlementDate = Date(),
223 Date npvDate = Date(),
224 Real npv = Null<Real>());
226
228
233
238 static Real npv(const Leg& leg,
239 const InterestRate& yield,
240 bool includeSettlementDateFlows,
241 Date settlementDate = Date(),
242 Date npvDate = Date());
243 static Real npv(const Leg& leg,
244 Rate yield,
245 const DayCounter& dayCounter,
246 Compounding compounding,
247 Frequency frequency,
248 bool includeSettlementDateFlows,
249 Date settlementDate = Date(),
250 Date npvDate = Date());
252
259 static Real bps(const Leg& leg,
260 const InterestRate& yield,
261 bool includeSettlementDateFlows,
262 Date settlementDate = Date(),
263 Date npvDate = Date());
264 static Real bps(const Leg& leg,
265 Rate yield,
266 const DayCounter& dayCounter,
267 Compounding compounding,
268 Frequency frequency,
269 bool includeSettlementDateFlows,
270 Date settlementDate = Date(),
271 Date npvDate = Date());
273
277 static Rate yield(const Leg& leg,
278 Real npv,
279 const DayCounter& dayCounter,
280 Compounding compounding,
281 Frequency frequency,
282 bool includeSettlementDateFlows,
283 Date settlementDate = Date(),
284 Date npvDate = Date(),
285 Real accuracy = 1.0e-10,
286 Size maxIterations = 100,
287 Rate guess = 0.05);
288
289 template <typename Solver>
290 static Rate yield(const Solver& solver,
291 const Leg& leg,
292 Real npv,
293 const DayCounter& dayCounter,
294 Compounding compounding,
295 Frequency frequency,
296 bool includeSettlementDateFlows,
297 Date settlementDate = Date(),
298 Date npvDate = Date(),
299 Real accuracy = 1.0e-10,
300 Rate guess = 0.05) {
301 IrrFinder objFunction(leg, npv, dayCounter, compounding,
302 frequency, includeSettlementDateFlows,
303 settlementDate, npvDate);
304 return solver.solve(objFunction, accuracy, guess, guess/10.0);
305 }
306
308
331 static Time duration(const Leg& leg,
332 const InterestRate& yield,
333 Duration::Type type,
334 bool includeSettlementDateFlows,
335 Date settlementDate = Date(),
336 Date npvDate = Date());
337 static Time duration(const Leg& leg,
338 Rate yield,
339 const DayCounter& dayCounter,
340 Compounding compounding,
341 Frequency frequency,
342 Duration::Type type,
343 bool includeSettlementDateFlows,
344 Date settlementDate = Date(),
345 Date npvDate = Date());
346
348
355 static Real convexity(const Leg& leg,
356 const InterestRate& yield,
357 bool includeSettlementDateFlows,
358 Date settlementDate = Date(),
359 Date npvDate = Date());
360 static Real convexity(const Leg& leg,
361 Rate yield,
362 const DayCounter& dayCounter,
363 Compounding compounding,
364 Frequency frequency,
365 bool includeSettlementDateFlows,
366 Date settlementDate = Date(),
367 Date npvDate = Date());
368
370
373 static Real basisPointValue(const Leg& leg,
374 const InterestRate& yield,
375 bool includeSettlementDateFlows,
376 Date settlementDate = Date(),
377 Date npvDate = Date());
378 static Real basisPointValue(const Leg& leg,
379 Rate yield,
380 const DayCounter& dayCounter,
381 Compounding compounding,
382 Frequency frequency,
383 bool includeSettlementDateFlows,
384 Date settlementDate = Date(),
385 Date npvDate = Date());
386
388
392 static Real yieldValueBasisPoint(const Leg& leg,
393 const InterestRate& yield,
394 bool includeSettlementDateFlows,
395 Date settlementDate = Date(),
396 Date npvDate = Date());
397 static Real yieldValueBasisPoint(const Leg& leg,
398 Rate yield,
399 const DayCounter& dayCounter,
400 Compounding compounding,
401 Frequency frequency,
402 bool includeSettlementDateFlows,
403 Date settlementDate = Date(),
404 Date npvDate = Date());
406
408
414
419 static Real npv(const Leg& leg,
420 const ext::shared_ptr<YieldTermStructure>& discount,
422 const DayCounter& dayCounter,
423 Compounding compounding,
424 Frequency frequency,
425 bool includeSettlementDateFlows,
426 Date settlementDate = Date(),
427 Date npvDate = Date());
429 static Spread zSpread(const Leg& leg,
430 Real npv,
431 const ext::shared_ptr<YieldTermStructure>&,
432 const DayCounter& dayCounter,
433 Compounding compounding,
434 Frequency frequency,
435 bool includeSettlementDateFlows,
436 Date settlementDate = Date(),
437 Date npvDate = Date(),
438 Real accuracy = 1.0e-10,
439 Size maxIterations = 100,
440 Rate guess = 0.0);
442 static Spread zSpread(const Leg& leg,
443 const ext::shared_ptr<YieldTermStructure>& d,
444 Real npv,
445 const DayCounter& dayCounter,
446 Compounding compounding,
447 Frequency frequency,
448 bool includeSettlementDateFlows,
449 Date settlementDate = Date(),
450 Date npvDate = Date(),
451 Real accuracy = 1.0e-10,
452 Size maxIterations = 100,
453 Rate guess = 0.0) {
454 return zSpread(leg, npv, d, dayCounter, compounding, frequency,
455 includeSettlementDateFlows, settlementDate, npvDate,
456 accuracy, maxIterations, guess);
457 }
459
460 };
461
462}
463
464#endif
Real derivative(Rate y) const
Definition: cashflows.cpp:774
Real operator()(Rate y) const
Definition: cashflows.cpp:766
cashflow-analysis functions
Definition: cashflows.hpp:41
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
CashFlows(const CashFlows &)=delete
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 Rate yield(const Leg &leg, Real npv, 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.05)
Implied internal rate of return.
Definition: cashflows.cpp:915
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
CashFlows & operator=(CashFlows &&)=delete
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
static Real nominal(const Leg &leg, bool includeSettlementDateFlows, Date settlDate=Date())
Definition: cashflows.cpp:232
CashFlows & operator=(const CashFlows &)=delete
static Date previousCashFlowDate(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:119
static Rate yield(const Solver &solver, const Leg &leg, Real npv, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date(), Real accuracy=1.0e-10, Rate guess=0.05)
Definition: cashflows.hpp:290
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 bool isExpired(const Leg &leg, bool includeSettlementDateFlows, Date settlementDate=Date())
Definition: cashflows.cpp:66
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 Spread zSpread(const Leg &leg, const ext::shared_ptr< YieldTermStructure > &d, Real npv, 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)
deprecated implied Z-spread.
Definition: cashflows.hpp:442
CashFlows(CashFlows &&)=delete
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
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.
template class providing a null value for a given type.
Definition: null.hpp:76
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
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78