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  <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/coupon.hpp>
22 #include <ql/patterns/visitor.hpp>
23 #include <ql/time/daycounter.hpp>
24 
25 namespace QuantLib {
26 
Coupon(const Date & paymentDate,Real nominal,const Date & accrualStartDate,const Date & accrualEndDate,const Date & refPeriodStart,const Date & refPeriodEnd,const Date & exCouponDate)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     : paymentDate_(paymentDate), nominal_(nominal),
35       accrualStartDate_(accrualStartDate), accrualEndDate_(accrualEndDate),
36       refPeriodStart_(refPeriodStart), refPeriodEnd_(refPeriodEnd),
37       exCouponDate_(exCouponDate), accrualPeriod_(Null<Real>()) {
38         if (refPeriodStart_ == Date())
39             refPeriodStart_ = accrualStartDate_;
40         if (refPeriodEnd_ == Date())
41             refPeriodEnd_ = accrualEndDate_;
42     }
43 
accrualPeriod() const44     Time Coupon::accrualPeriod() const {
45         if (accrualPeriod_ == Null<Real>())
46             accrualPeriod_ =
47                 dayCounter().yearFraction(accrualStartDate_, accrualEndDate_,
48                                           refPeriodStart_, refPeriodEnd_);
49         return accrualPeriod_;
50     }
51 
accrualDays() const52     Date::serial_type Coupon::accrualDays() const {
53         return dayCounter().dayCount(accrualStartDate_,
54                                      accrualEndDate_);
55     }
56 
accruedPeriod(const Date & d) const57     Time Coupon::accruedPeriod(const Date& d) const {
58         if (d <= accrualStartDate_ || d > paymentDate_) {
59             return 0.0;
60         } else {
61             return dayCounter().yearFraction(accrualStartDate_,
62                                              std::min(d, accrualEndDate_),
63                                              refPeriodStart_,
64                                              refPeriodEnd_);
65         }
66     }
67 
accruedDays(const Date & d) const68     Date::serial_type Coupon::accruedDays(const Date& d) const {
69         if (d <= accrualStartDate_ || d > paymentDate_) {
70             return 0;
71         } else {
72             return dayCounter().dayCount(accrualStartDate_,
73                                          std::min(d, accrualEndDate_));
74         }
75     }
76 
accept(AcyclicVisitor & v)77     void Coupon::accept(AcyclicVisitor& v) {
78         Visitor<Coupon>* v1 = dynamic_cast<Visitor<Coupon>*>(&v);
79         if (v1 != 0)
80             v1->visit(*this);
81         else
82             CashFlow::accept(v);
83     }
84 
85 }
86