| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004 Jeff Yu |
| 5 | Copyright (C) 2004 M-Dimension Consulting Inc. |
| 6 | Copyright (C) 2005, 2006, 2007 StatPro Italia srl |
| 7 | Copyright (C) 2007, 2008, 2010 Ferdinando Ametrano |
| 8 | Copyright (C) 2009 Piter Dias |
| 9 | |
| 10 | This file is part of QuantLib, a free-software/open-source library |
| 11 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 12 | |
| 13 | QuantLib is free software: you can redistribute it and/or modify it |
| 14 | under the terms of the QuantLib license. You should have received a |
| 15 | copy of the license along with this program; if not, please email |
| 16 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 17 | <http://quantlib.org/license.shtml>. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 21 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <ql/instruments/bonds/fixedratebond.hpp> |
| 25 | #include <ql/cashflows/cashflowvectors.hpp> |
| 26 | #include <ql/cashflows/simplecashflow.hpp> |
| 27 | #include <ql/time/schedule.hpp> |
| 28 | |
| 29 | namespace QuantLib { |
| 30 | |
| 31 | FixedRateBond::FixedRateBond(Natural settlementDays, |
| 32 | Real faceAmount, |
| 33 | const Schedule& schedule, |
| 34 | const std::vector<Rate>& coupons, |
| 35 | const DayCounter& accrualDayCounter, |
| 36 | BusinessDayConvention paymentConvention, |
| 37 | Real redemption, |
| 38 | const Date& issueDate, |
| 39 | const Calendar& paymentCalendar, |
| 40 | const Period& exCouponPeriod, |
| 41 | const Calendar& exCouponCalendar, |
| 42 | const BusinessDayConvention exCouponConvention, |
| 43 | bool exCouponEndOfMonth, |
| 44 | const DayCounter& firstPeriodDayCounter) |
| 45 | : Bond(settlementDays, |
| 46 | paymentCalendar==Calendar() ? schedule.calendar() : paymentCalendar, |
| 47 | issueDate), |
| 48 | frequency_(schedule.hasTenor() ? schedule.tenor().frequency() : NoFrequency), |
| 49 | dayCounter_(accrualDayCounter), |
| 50 | firstPeriodDayCounter_(firstPeriodDayCounter) { |
| 51 | |
| 52 | maturityDate_ = schedule.endDate(); |
| 53 | |
| 54 | cashflows_ = FixedRateLeg(schedule) |
| 55 | .withNotionals(faceAmount) |
| 56 | .withCouponRates(coupons, paymentDayCounter: accrualDayCounter) |
| 57 | .withFirstPeriodDayCounter(firstPeriodDayCounter) |
| 58 | .withPaymentCalendar(calendar_) |
| 59 | .withPaymentAdjustment(paymentConvention) |
| 60 | .withExCouponPeriod(exCouponPeriod, |
| 61 | exCouponCalendar, |
| 62 | exCouponConvention, |
| 63 | endOfMonth: exCouponEndOfMonth); |
| 64 | |
| 65 | addRedemptionsToCashflows(redemptions: std::vector<Real>(1, redemption)); |
| 66 | |
| 67 | QL_ENSURE(!cashflows().empty(), "bond with no cashflows!" ); |
| 68 | QL_ENSURE(redemptions_.size() == 1, "multiple redemptions created" ); |
| 69 | } |
| 70 | |
| 71 | FixedRateBond::FixedRateBond(Natural settlementDays, |
| 72 | const Calendar& calendar, |
| 73 | Real faceAmount, |
| 74 | const Date& startDate, |
| 75 | const Date& maturityDate, |
| 76 | const Period& tenor, |
| 77 | const std::vector<Rate>& coupons, |
| 78 | const DayCounter& accrualDayCounter, |
| 79 | BusinessDayConvention accrualConvention, |
| 80 | BusinessDayConvention paymentConvention, |
| 81 | Real redemption, |
| 82 | const Date& issueDate, |
| 83 | const Date& stubDate, |
| 84 | DateGeneration::Rule rule, |
| 85 | bool endOfMonth, |
| 86 | const Calendar& paymentCalendar, |
| 87 | const Period& exCouponPeriod, |
| 88 | const Calendar& exCouponCalendar, |
| 89 | const BusinessDayConvention exCouponConvention, |
| 90 | bool exCouponEndOfMonth, |
| 91 | const DayCounter& firstPeriodDayCounter) |
| 92 | : Bond(settlementDays, |
| 93 | paymentCalendar==Calendar() ? calendar : paymentCalendar, |
| 94 | issueDate), |
| 95 | frequency_(tenor.frequency()), dayCounter_(accrualDayCounter), |
| 96 | firstPeriodDayCounter_(firstPeriodDayCounter) { |
| 97 | |
| 98 | maturityDate_ = maturityDate; |
| 99 | |
| 100 | Date firstDate, nextToLastDate; |
| 101 | switch (rule) { |
| 102 | case DateGeneration::Backward: |
| 103 | firstDate = Date(); |
| 104 | nextToLastDate = stubDate; |
| 105 | break; |
| 106 | case DateGeneration::Forward: |
| 107 | firstDate = stubDate; |
| 108 | nextToLastDate = Date(); |
| 109 | break; |
| 110 | case DateGeneration::Zero: |
| 111 | case DateGeneration::ThirdWednesday: |
| 112 | case DateGeneration::Twentieth: |
| 113 | case DateGeneration::TwentiethIMM: |
| 114 | QL_FAIL("stub date (" << stubDate << ") not allowed with " << |
| 115 | rule << " DateGeneration::Rule" ); |
| 116 | default: |
| 117 | QL_FAIL("unknown DateGeneration::Rule (" << Integer(rule) << ")" ); |
| 118 | } |
| 119 | |
| 120 | Schedule schedule(startDate, maturityDate_, tenor, |
| 121 | calendar, accrualConvention, accrualConvention, |
| 122 | rule, endOfMonth, |
| 123 | firstDate, nextToLastDate); |
| 124 | |
| 125 | cashflows_ = FixedRateLeg(schedule) |
| 126 | .withNotionals(faceAmount) |
| 127 | .withCouponRates(coupons, paymentDayCounter: accrualDayCounter) |
| 128 | .withFirstPeriodDayCounter(firstPeriodDayCounter) |
| 129 | .withPaymentCalendar(calendar_) |
| 130 | .withPaymentAdjustment(paymentConvention) |
| 131 | .withExCouponPeriod(exCouponPeriod, |
| 132 | exCouponCalendar, |
| 133 | exCouponConvention, |
| 134 | endOfMonth: exCouponEndOfMonth); |
| 135 | |
| 136 | addRedemptionsToCashflows(redemptions: std::vector<Real>(1, redemption)); |
| 137 | |
| 138 | QL_ENSURE(!cashflows().empty(), "bond with no cashflows!" ); |
| 139 | QL_ENSURE(redemptions_.size() == 1, "multiple redemptions created" ); |
| 140 | } |
| 141 | |
| 142 | FixedRateBond::FixedRateBond(Natural settlementDays, |
| 143 | Real faceAmount, |
| 144 | const Schedule& schedule, |
| 145 | const std::vector<InterestRate>& coupons, |
| 146 | BusinessDayConvention paymentConvention, |
| 147 | Real redemption, |
| 148 | const Date& issueDate, |
| 149 | const Calendar& paymentCalendar, |
| 150 | const Period& exCouponPeriod, |
| 151 | const Calendar& exCouponCalendar, |
| 152 | const BusinessDayConvention exCouponConvention, |
| 153 | bool exCouponEndOfMonth) |
| 154 | : Bond(settlementDays, |
| 155 | paymentCalendar==Calendar() ? schedule.calendar() : paymentCalendar, |
| 156 | issueDate), |
| 157 | frequency_(schedule.tenor().frequency()), |
| 158 | dayCounter_(coupons[0].dayCounter()) { |
| 159 | |
| 160 | maturityDate_ = schedule.endDate(); |
| 161 | |
| 162 | cashflows_ = FixedRateLeg(schedule) |
| 163 | .withNotionals(faceAmount) |
| 164 | .withCouponRates(coupons) |
| 165 | .withPaymentCalendar(calendar_) |
| 166 | .withPaymentAdjustment(paymentConvention) |
| 167 | .withExCouponPeriod(exCouponPeriod, |
| 168 | exCouponCalendar, |
| 169 | exCouponConvention, |
| 170 | endOfMonth: exCouponEndOfMonth); |
| 171 | |
| 172 | addRedemptionsToCashflows(redemptions: std::vector<Real>(1, redemption)); |
| 173 | |
| 174 | QL_ENSURE(!cashflows().empty(), "bond with no cashflows!" ); |
| 175 | QL_ENSURE(redemptions_.size() == 1, "multiple redemptions created" ); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |