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 StatPro Italia srl 6 Copyright (C) 2003 Nicolas Di Césaré 7 Copyright (C) 2006, 2007 Cristina Duminuco 8 Copyright (C) 2006 Ferdinando Ametrano 9 Copyright (C) 2007 Giorgio Facchinetti 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 /*! \file floatingratecoupon.hpp 26 \brief Coupon paying a variable index-based rate 27 */ 28 29 #ifndef quantlib_floating_rate_coupon_hpp 30 #define quantlib_floating_rate_coupon_hpp 31 32 #include <ql/cashflows/coupon.hpp> 33 #include <ql/patterns/visitor.hpp> 34 #include <ql/time/daycounter.hpp> 35 #include <ql/handle.hpp> 36 37 namespace QuantLib { 38 39 class InterestRateIndex; 40 class YieldTermStructure; 41 class FloatingRateCouponPricer; 42 43 //! base floating-rate coupon class 44 class FloatingRateCoupon : public Coupon, 45 public Observer { 46 public: 47 FloatingRateCoupon(const Date& paymentDate, 48 Real nominal, 49 const Date& startDate, 50 const Date& endDate, 51 Natural fixingDays, 52 const ext::shared_ptr<InterestRateIndex>& index, 53 Real gearing = 1.0, 54 Spread spread = 0.0, 55 const Date& refPeriodStart = Date(), 56 const Date& refPeriodEnd = Date(), 57 const DayCounter& dayCounter = DayCounter(), 58 bool isInArrears = false, 59 const Date& exCouponDate = Date()); 60 61 //! \name CashFlow interface 62 //@{ amount() const63 Real amount() const { return rate() * accrualPeriod() * nominal(); } 64 //@} 65 66 //! \name Coupon interface 67 //@{ 68 Rate rate() const; 69 Real price(const Handle<YieldTermStructure>& discountingCurve) const; dayCounter() const70 DayCounter dayCounter() const { return dayCounter_; } 71 Real accruedAmount(const Date&) const; 72 //@} 73 74 //! \name Inspectors 75 //@{ 76 //! floating index 77 const ext::shared_ptr<InterestRateIndex>& index() const; 78 //! fixing days fixingDays() const79 Natural fixingDays() const { return fixingDays_; } 80 //! fixing date 81 virtual Date fixingDate() const; 82 //! index gearing, i.e. multiplicative coefficient for the index gearing() const83 Real gearing() const { return gearing_; } 84 //! spread paid over the fixing of the underlying index spread() const85 Spread spread() const { return spread_; } 86 //! fixing of the underlying index 87 virtual Rate indexFixing() const; 88 //! convexity adjustment 89 virtual Rate convexityAdjustment() const; 90 //! convexity-adjusted fixing 91 virtual Rate adjustedFixing() const; 92 //! whether or not the coupon fixes in arrears isInArrears() const93 bool isInArrears() const { return isInArrears_; } 94 //@} 95 96 //! \name Observer interface 97 //@{ update()98 void update() { notifyObservers(); } 99 //@} 100 101 //! \name Visitability 102 //@{ 103 virtual void accept(AcyclicVisitor&); 104 //@} 105 106 virtual void setPricer(const ext::shared_ptr<FloatingRateCouponPricer>&); 107 ext::shared_ptr<FloatingRateCouponPricer> pricer() const; 108 protected: 109 //! convexity adjustment for the given index fixing 110 Rate convexityAdjustmentImpl(Rate fixing) const; 111 ext::shared_ptr<InterestRateIndex> index_; 112 DayCounter dayCounter_; 113 Natural fixingDays_; 114 Real gearing_; 115 Spread spread_; 116 bool isInArrears_; 117 ext::shared_ptr<FloatingRateCouponPricer> pricer_; 118 }; 119 120 // inline definitions 121 122 inline const ext::shared_ptr<InterestRateIndex>& index() const123 FloatingRateCoupon::index() const { 124 return index_; 125 } 126 convexityAdjustment() const127 inline Rate FloatingRateCoupon::convexityAdjustment() const { 128 return convexityAdjustmentImpl(indexFixing()); 129 } 130 adjustedFixing() const131 inline Rate FloatingRateCoupon::adjustedFixing() const { 132 return (rate()-spread())/gearing(); 133 } 134 135 inline ext::shared_ptr<FloatingRateCouponPricer> pricer() const136 FloatingRateCoupon::pricer() const { 137 return pricer_; 138 } 139 140 inline Rate convexityAdjustmentImpl(Rate fixing) const141 FloatingRateCoupon::convexityAdjustmentImpl(Rate fixing) const { 142 return (gearing() == 0.0 ? 0.0 : adjustedFixing()-fixing); 143 } 144 accept(AcyclicVisitor & v)145 inline void FloatingRateCoupon::accept(AcyclicVisitor& v) { 146 Visitor<FloatingRateCoupon>* v1 = 147 dynamic_cast<Visitor<FloatingRateCoupon>*>(&v); 148 if (v1 != 0) 149 v1->visit(*this); 150 else 151 Coupon::accept(v); 152 } 153 154 } 155 156 #endif 157