| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2009, 2014, 2015 Ferdinando Ametrano |
| 5 | Copyright (C) 2015 Paolo Mazzocchi |
| 6 | Copyright (C) 2017 Joseph Jeisman |
| 7 | Copyright (C) 2017 Fabrice Lecuyer |
| 8 | |
| 9 | This file is part of QuantLib, a free-software/open-source library |
| 10 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 11 | |
| 12 | QuantLib is free software: you can redistribute it and/or modify it |
| 13 | under the terms of the QuantLib license. You should have received a |
| 14 | copy of the license along with this program; if not, please email |
| 15 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 16 | <http://quantlib.org/license.shtml>. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 21 | */ |
| 22 | |
| 23 | #include <ql/instruments/makeois.hpp> |
| 24 | #include <ql/pricingengines/swap/discountingswapengine.hpp> |
| 25 | #include <ql/indexes/iborindex.hpp> |
| 26 | #include <ql/time/schedule.hpp> |
| 27 | |
| 28 | namespace QuantLib { |
| 29 | |
| 30 | MakeOIS::MakeOIS(const Period& swapTenor, |
| 31 | const ext::shared_ptr<OvernightIndex>& overnightIndex, |
| 32 | Rate fixedRate, |
| 33 | const Period& forwardStart) |
| 34 | : swapTenor_(swapTenor), overnightIndex_(overnightIndex), fixedRate_(fixedRate), |
| 35 | forwardStart_(forwardStart), |
| 36 | |
| 37 | calendar_(overnightIndex->fixingCalendar()), |
| 38 | |
| 39 | fixedDayCount_(overnightIndex->dayCounter()) {} |
| 40 | |
| 41 | MakeOIS::operator OvernightIndexedSwap() const { |
| 42 | ext::shared_ptr<OvernightIndexedSwap> ois = *this; |
| 43 | return *ois; |
| 44 | } |
| 45 | |
| 46 | MakeOIS::operator ext::shared_ptr<OvernightIndexedSwap>() const { |
| 47 | |
| 48 | Date startDate; |
| 49 | if (effectiveDate_ != Date()) |
| 50 | startDate = effectiveDate_; |
| 51 | else { |
| 52 | Date refDate = Settings::instance().evaluationDate(); |
| 53 | // if the evaluation date is not a business day |
| 54 | // then move to the next business day |
| 55 | refDate = calendar_.adjust(refDate); |
| 56 | Date spotDate = calendar_.advance(date: refDate, |
| 57 | period: settlementDays_*Days); |
| 58 | startDate = spotDate+forwardStart_; |
| 59 | if (forwardStart_.length()<0) |
| 60 | startDate = calendar_.adjust(startDate, convention: Preceding); |
| 61 | else |
| 62 | startDate = calendar_.adjust(startDate, convention: Following); |
| 63 | } |
| 64 | |
| 65 | // OIS end of month default |
| 66 | bool usedEndOfMonth = |
| 67 | isDefaultEOM_ ? calendar_.isEndOfMonth(d: startDate) : endOfMonth_; |
| 68 | |
| 69 | Date endDate = terminationDate_; |
| 70 | if (endDate == Date()) { |
| 71 | if (usedEndOfMonth) |
| 72 | endDate = calendar_.advance(date: startDate, |
| 73 | period: swapTenor_, |
| 74 | convention: ModifiedFollowing, |
| 75 | endOfMonth: usedEndOfMonth); |
| 76 | else |
| 77 | endDate = startDate + swapTenor_; |
| 78 | } |
| 79 | |
| 80 | Schedule schedule(startDate, endDate, |
| 81 | Period(paymentFrequency_), |
| 82 | calendar_, |
| 83 | ModifiedFollowing, |
| 84 | ModifiedFollowing, |
| 85 | rule_, |
| 86 | usedEndOfMonth); |
| 87 | |
| 88 | Rate usedFixedRate = fixedRate_; |
| 89 | if (fixedRate_ == Null<Rate>()) { |
| 90 | OvernightIndexedSwap temp(type_, nominal_, |
| 91 | schedule, |
| 92 | 0.0, // fixed rate |
| 93 | fixedDayCount_, |
| 94 | overnightIndex_, overnightSpread_, |
| 95 | paymentLag_, paymentAdjustment_, |
| 96 | paymentCalendar_, telescopicValueDates_); |
| 97 | if (engine_ == nullptr) { |
| 98 | Handle<YieldTermStructure> disc = |
| 99 | overnightIndex_->forwardingTermStructure(); |
| 100 | QL_REQUIRE(!disc.empty(), |
| 101 | "null term structure set to this instance of " << |
| 102 | overnightIndex_->name()); |
| 103 | bool includeSettlementDateFlows = false; |
| 104 | ext::shared_ptr<PricingEngine> engine(new |
| 105 | DiscountingSwapEngine(disc, includeSettlementDateFlows)); |
| 106 | temp.setPricingEngine(engine); |
| 107 | } else |
| 108 | temp.setPricingEngine(engine_); |
| 109 | |
| 110 | usedFixedRate = temp.fairRate(); |
| 111 | } |
| 112 | |
| 113 | ext::shared_ptr<OvernightIndexedSwap> ois(new |
| 114 | OvernightIndexedSwap(type_, nominal_, |
| 115 | schedule, |
| 116 | usedFixedRate, fixedDayCount_, |
| 117 | overnightIndex_, overnightSpread_, |
| 118 | paymentLag_, paymentAdjustment_, |
| 119 | paymentCalendar_, telescopicValueDates_, |
| 120 | averagingMethod_)); |
| 121 | |
| 122 | if (engine_ == nullptr) { |
| 123 | Handle<YieldTermStructure> disc = |
| 124 | overnightIndex_->forwardingTermStructure(); |
| 125 | bool includeSettlementDateFlows = false; |
| 126 | ext::shared_ptr<PricingEngine> engine(new |
| 127 | DiscountingSwapEngine(disc, includeSettlementDateFlows)); |
| 128 | ois->setPricingEngine(engine); |
| 129 | } else |
| 130 | ois->setPricingEngine(engine_); |
| 131 | |
| 132 | return ois; |
| 133 | } |
| 134 | |
| 135 | MakeOIS& MakeOIS::receiveFixed(bool flag) { |
| 136 | type_ = flag ? Swap::Receiver : Swap::Payer ; |
| 137 | return *this; |
| 138 | } |
| 139 | |
| 140 | MakeOIS& MakeOIS::withType(Swap::Type type) { |
| 141 | type_ = type; |
| 142 | return *this; |
| 143 | } |
| 144 | |
| 145 | MakeOIS& MakeOIS::withNominal(Real n) { |
| 146 | nominal_ = n; |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | MakeOIS& MakeOIS::withSettlementDays(Natural settlementDays) { |
| 151 | settlementDays_ = settlementDays; |
| 152 | effectiveDate_ = Date(); |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | MakeOIS& MakeOIS::withEffectiveDate(const Date& effectiveDate) { |
| 157 | effectiveDate_ = effectiveDate; |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | MakeOIS& MakeOIS::withTerminationDate(const Date& terminationDate) { |
| 162 | terminationDate_ = terminationDate; |
| 163 | swapTenor_ = Period(); |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | MakeOIS& MakeOIS::withPaymentFrequency(Frequency f) { |
| 168 | paymentFrequency_ = f; |
| 169 | if (paymentFrequency_==Once) |
| 170 | rule_ = DateGeneration::Zero; |
| 171 | return *this; |
| 172 | } |
| 173 | |
| 174 | MakeOIS& MakeOIS::withPaymentAdjustment(BusinessDayConvention convention) { |
| 175 | paymentAdjustment_ = convention; |
| 176 | return *this; |
| 177 | } |
| 178 | |
| 179 | MakeOIS& MakeOIS::withPaymentLag(Natural lag) { |
| 180 | paymentLag_ = lag; |
| 181 | return *this; |
| 182 | } |
| 183 | |
| 184 | MakeOIS& MakeOIS::withPaymentCalendar(const Calendar& cal) { |
| 185 | paymentCalendar_ = cal; |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | MakeOIS& MakeOIS::withRule(DateGeneration::Rule r) { |
| 190 | rule_ = r; |
| 191 | if (r==DateGeneration::Zero) |
| 192 | paymentFrequency_ = Once; |
| 193 | return *this; |
| 194 | } |
| 195 | |
| 196 | MakeOIS& MakeOIS::withDiscountingTermStructure( |
| 197 | const Handle<YieldTermStructure>& d) { |
| 198 | bool includeSettlementDateFlows = false; |
| 199 | engine_ = ext::shared_ptr<PricingEngine>(new |
| 200 | DiscountingSwapEngine(d, includeSettlementDateFlows)); |
| 201 | return *this; |
| 202 | } |
| 203 | |
| 204 | MakeOIS& MakeOIS::withPricingEngine( |
| 205 | const ext::shared_ptr<PricingEngine>& engine) { |
| 206 | engine_ = engine; |
| 207 | return *this; |
| 208 | } |
| 209 | |
| 210 | MakeOIS& MakeOIS::withFixedLegDayCount(const DayCounter& dc) { |
| 211 | fixedDayCount_ = dc; |
| 212 | return *this; |
| 213 | } |
| 214 | |
| 215 | MakeOIS& MakeOIS::withEndOfMonth(bool flag) { |
| 216 | endOfMonth_ = flag; |
| 217 | isDefaultEOM_ = false; |
| 218 | return *this; |
| 219 | } |
| 220 | |
| 221 | MakeOIS& MakeOIS::withOvernightLegSpread(Spread sp) { |
| 222 | overnightSpread_ = sp; |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | MakeOIS& MakeOIS::withTelescopicValueDates(bool telescopicValueDates) { |
| 227 | telescopicValueDates_ = telescopicValueDates; |
| 228 | return *this; |
| 229 | } |
| 230 | |
| 231 | MakeOIS& MakeOIS::withAveragingMethod(RateAveraging::Type averagingMethod) { |
| 232 | averagingMethod_ = averagingMethod; |
| 233 | return *this; |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | |