Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
zerofixedcoupon.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
20#include <iostream>
21namespace QuantExt {
22
23 ZeroFixedCoupon::ZeroFixedCoupon( const Date& paymentDate,
24 double notional,
25 double rate,
26 const DayCounter& dc,
27 const std::vector<Date>& dates,
28 const Compounding& comp,
29 bool subtractNotional) :
30 Coupon(paymentDate, notional, dates.front(), dates.back()),
31 notional_(notional), rate_(rate), dc_(dc), dates_(dates),
32 comp_(comp), subtractNotional_(subtractNotional)
33 {
34
35 QL_REQUIRE(comp_ == QuantLib::Compounded || comp_ == QuantLib::Simple,
36 "Compounding method " << comp_ << " not supported");
37
38 QL_REQUIRE(dates_.size() >= 2, "Number of schedule dates expected at least 2, got " << dates_.size());
39
41
42 }//ctor
43
44Real ZeroFixedCoupon::amount() const { return amount_; }
45
46Real ZeroFixedCoupon::nominal() const { return notional_; }
47
48Real ZeroFixedCoupon::rate() const { return rate_; }
49
50DayCounter ZeroFixedCoupon::dayCounter() const { return dc_; }
51
52Real ZeroFixedCoupon::accruedAmount(const Date& accrualEnd) const {
53
54 //outside this period, return zero
55 if(dates_.front() > accrualEnd || dates_.back() < accrualEnd)
56 return 0.0;
57
58 double totalDCF = 0.0;
59 double compoundFactor = 1.0;
60
61 // we loop over the dates in the schedule, computing the compound factor.
62 // For the Compounded rule:
63 // (1+r)^dcf_0 * (1+r)^dcf_1 * ... = (1+r)^(dcf_0 + dcf_1 + ...)
64 // So we compute the sum of all DayCountFractions in the loop.
65 // For the Simple rule:
66 // (1 + r * dcf_0) * (1 + r * dcf_1)...
67
68 for (Size i = 0; i < dates_.size() - 1; i++) {
69
70 Date startDate = dates_[i];
71 Date endDate = dates_[i+1];
72
73 if(startDate > accrualEnd)
74 break;
75
76 if(endDate > accrualEnd)
77 endDate = accrualEnd;
78
79 double dcf = dc_.yearFraction(startDate, endDate);
80
81 if (comp_ == QuantLib::Simple)
82 compoundFactor *= (1 + rate_ * dcf);
83
84 totalDCF += dcf;
85 }
86
87 if (comp_ == QuantLib::Compounded)
88 compoundFactor = pow(1.0 + rate_, totalDCF);
89
90 return notional_ * (subtractNotional_ ? compoundFactor - 1.0 : compoundFactor);
91
92
93
94}
95
96
97
98void ZeroFixedCoupon::accept(AcyclicVisitor& v) {
99 Visitor<ZeroFixedCoupon>* v1 = dynamic_cast<Visitor<ZeroFixedCoupon>*>(&v);
100 if (v1 != 0)
101 v1->visit(*this);
102 else
103 Coupon::accept(v);
104}
105
106} // namespace QuantExt
ZeroFixedCoupon(const Date &paymentDate, double notional, double rate, const DayCounter &dc, const std::vector< Date > &dates, const Compounding &comp, bool subtractNotional)
std::vector< Date > dates_
Real amount() const override
void accept(AcyclicVisitor &) override
Real nominal() const override
DayCounter dayCounter() const override
Real accruedAmount(const Date &accrualEnd) const override
Real rate() const override
CompiledFormula pow(CompiledFormula x, const CompiledFormula &y)
Nominal flow associated with a floating annuity coupon.