| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007 Giorgio Facchinetti |
| 5 | Copyright (C) 2006, 2007 Cristina Duminuco |
| 6 | Copyright (C) 2006 Ferdinando Ametrano |
| 7 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 8 | Copyright (C) 2003, 2004 StatPro Italia srl |
| 9 | Copyright (C) 2003 Nicolas Di Césaré |
| 10 | |
| 11 | This file is part of QuantLib, a free-software/open-source library |
| 12 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 13 | |
| 14 | QuantLib is free software: you can redistribute it and/or modify it |
| 15 | under the terms of the QuantLib license. You should have received a |
| 16 | copy of the license along with this program; if not, please email |
| 17 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 18 | <http://quantlib.org/license.shtml>. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 21 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 22 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 23 | */ |
| 24 | |
| 25 | #include <ql/cashflows/couponpricer.hpp> |
| 26 | #include <ql/cashflows/floatingratecoupon.hpp> |
| 27 | #include <ql/indexes/interestrateindex.hpp> |
| 28 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 29 | #include <utility> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | FloatingRateCoupon::FloatingRateCoupon(const Date& paymentDate, |
| 34 | Real nominal, |
| 35 | const Date& startDate, |
| 36 | const Date& endDate, |
| 37 | Natural fixingDays, |
| 38 | const ext::shared_ptr<InterestRateIndex>& index, |
| 39 | Real gearing, |
| 40 | Spread spread, |
| 41 | const Date& refPeriodStart, |
| 42 | const Date& refPeriodEnd, |
| 43 | DayCounter dayCounter, |
| 44 | bool isInArrears, |
| 45 | const Date& exCouponDate) |
| 46 | : Coupon(paymentDate, nominal, startDate, endDate, refPeriodStart, refPeriodEnd, exCouponDate), |
| 47 | index_(index), dayCounter_(std::move(dayCounter)), |
| 48 | fixingDays_(fixingDays == Null<Natural>() ? (index ? index->fixingDays() : 0) : fixingDays), |
| 49 | gearing_(gearing), spread_(spread), isInArrears_(isInArrears) { |
| 50 | QL_REQUIRE(index_, "no index provided" ); |
| 51 | QL_REQUIRE(gearing_!=0, "Null gearing not allowed" ); |
| 52 | |
| 53 | if (dayCounter_.empty()) |
| 54 | dayCounter_ = index_->dayCounter(); |
| 55 | |
| 56 | registerWith(h: index_); |
| 57 | registerWith(h: Settings::instance().evaluationDate()); |
| 58 | } |
| 59 | |
| 60 | void FloatingRateCoupon::setPricer( |
| 61 | const ext::shared_ptr<FloatingRateCouponPricer>& pricer) { |
| 62 | if (pricer_ != nullptr) |
| 63 | unregisterWith(h: pricer_); |
| 64 | pricer_ = pricer; |
| 65 | if (pricer_ != nullptr) |
| 66 | registerWith(h: pricer_); |
| 67 | update(); |
| 68 | } |
| 69 | |
| 70 | Real FloatingRateCoupon::accruedAmount(const Date& d) const { |
| 71 | if (d <= accrualStartDate_ || d > paymentDate_) { |
| 72 | // out of coupon range |
| 73 | return 0.0; |
| 74 | } else { |
| 75 | return nominal() * rate() * accruedPeriod(d); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Date FloatingRateCoupon::fixingDate() const { |
| 80 | // if isInArrears_ fix at the end of period |
| 81 | Date refDate = isInArrears_ ? accrualEndDate_ : accrualStartDate_; |
| 82 | return index_->fixingCalendar().advance(refDate, |
| 83 | n: -static_cast<Integer>(fixingDays_), unit: Days, convention: Preceding); |
| 84 | } |
| 85 | |
| 86 | Rate FloatingRateCoupon::rate() const { |
| 87 | calculate(); |
| 88 | return rate_; |
| 89 | } |
| 90 | |
| 91 | void FloatingRateCoupon::performCalculations() const { |
| 92 | QL_REQUIRE(pricer_, "pricer not set" ); |
| 93 | pricer_->initialize(coupon: *this); |
| 94 | rate_ = pricer_->swapletRate(); |
| 95 | } |
| 96 | |
| 97 | Real FloatingRateCoupon::price(const Handle<YieldTermStructure>& discountingCurve) const { |
| 98 | return amount() * discountingCurve->discount(d: date()); |
| 99 | } |
| 100 | |
| 101 | Rate FloatingRateCoupon::indexFixing() const { |
| 102 | return index_->fixing(fixingDate: fixingDate()); |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |