Coverage Report

Created: 2026-10-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/cashflows/coupon.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5
 Copyright (C) 2003, 2004, 2007 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/coupon.hpp>
22
#include <ql/patterns/visitor.hpp>
23
#include <ql/time/daycounter.hpp>
24
25
namespace QuantLib {
26
27
    Coupon::Coupon(const Date& paymentDate,
28
                   Real nominal,
29
                   const Date& accrualStartDate,
30
                   const Date& accrualEndDate,
31
                   const Date& refPeriodStart,
32
                   const Date& refPeriodEnd,
33
                   const Date& exCouponDate)
34
25.2M
    : paymentDate_(paymentDate), nominal_(nominal), 
35
25.2M
      accrualStartDate_(accrualStartDate), accrualEndDate_(accrualEndDate),
36
25.2M
      refPeriodStart_(refPeriodStart), refPeriodEnd_(refPeriodEnd),
37
25.2M
      exCouponDate_(exCouponDate), accrualPeriod_(Null<Real>()) {
38
25.2M
        if (refPeriodStart_ == Date())
39
0
            refPeriodStart_ = accrualStartDate_;
40
25.2M
        if (refPeriodEnd_ == Date())
41
0
            refPeriodEnd_ = accrualEndDate_;
42
25.2M
    }
43
44
1.47k
    Time Coupon::accrualPeriod() const {
45
1.47k
        if (accrualPeriod_ == Null<Real>())
46
1.47k
            accrualPeriod_ =
47
1.47k
                dayCounter().yearFraction(accrualStartDate_, accrualEndDate_,
48
1.47k
                                          refPeriodStart_, refPeriodEnd_);
49
1.47k
        return accrualPeriod_;
50
1.47k
    }
51
52
0
    Date::serial_type Coupon::accrualDays() const {
53
0
        return dayCounter().dayCount(accrualStartDate_,
54
0
                                     accrualEndDate_);
55
0
    }
56
57
0
    Time Coupon::accruedPeriod(const Date& d) const {
58
0
        if (d <= accrualStartDate_ || d > paymentDate_) {
59
0
            return 0.0;
60
0
        } else if (tradingExCoupon(d)) {
61
0
            return -dayCounter().yearFraction(d, std::max(d, accrualEndDate_),
62
0
                                              refPeriodStart_, refPeriodEnd_);
63
0
        } else {
64
0
            return dayCounter().yearFraction(accrualStartDate_,
65
0
                                             std::min(d, accrualEndDate_),
66
0
                                             refPeriodStart_,
67
0
                                             refPeriodEnd_);
68
0
        }
69
0
    }
70
71
0
    Date::serial_type Coupon::accruedDays(const Date& d) const {
72
0
        if (d <= accrualStartDate_ || d > paymentDate_) {
73
0
            return 0;
74
0
        } else {
75
0
            return dayCounter().dayCount(accrualStartDate_,
76
0
                                         std::min(d, accrualEndDate_));
77
0
        }
78
0
    }
79
80
0
    void Coupon::accept(AcyclicVisitor& v) {
81
0
        auto* v1 = dynamic_cast<Visitor<Coupon>*>(&v);
82
0
        if (v1 != nullptr)
83
0
            v1->visit(*this);
84
0
        else
85
0
            CashFlow::accept(v);
86
0
    }
87
88
25.3M
    ext::shared_ptr<Coupon> coupon_cast(const ext::shared_ptr<CashFlow>& cf) {
89
25.3M
        return cf->isCoupon() ? ext::static_pointer_cast<Coupon>(cf) : nullptr;
90
25.3M
    }
91
}