| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007, 2008, 2014 Ferdinando Ametrano |
| 5 | Copyright (C) 2007 Giorgio Facchinetti |
| 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/cashflows.hpp> |
| 22 | #include <ql/exercise.hpp> |
| 23 | #include <ql/indexes/swapindex.hpp> |
| 24 | #include <ql/instruments/makeswaption.hpp> |
| 25 | #include <ql/instruments/makevanillaswap.hpp> |
| 26 | #include <ql/pricingengines/swap/discountingswapengine.hpp> |
| 27 | #include <ql/optional.hpp> |
| 28 | #include <ql/settings.hpp> |
| 29 | #include <utility> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex, |
| 34 | const Period& optionTenor, |
| 35 | Rate strike) |
| 36 | : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical), |
| 37 | settlementMethod_(Settlement::PhysicalOTC), optionTenor_(optionTenor), |
| 38 | optionConvention_(ModifiedFollowing), fixingDate_(Null<Date>()), strike_(strike), |
| 39 | underlyingType_(Swap::Payer), nominal_(1.0) {} |
| 40 | |
| 41 | MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex, |
| 42 | const Date& fixingDate, |
| 43 | Rate strike) |
| 44 | : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical), |
| 45 | settlementMethod_(Settlement::PhysicalOTC), optionConvention_(ModifiedFollowing), |
| 46 | fixingDate_(fixingDate), strike_(strike), underlyingType_(Swap::Payer) {} |
| 47 | |
| 48 | MakeSwaption::operator Swaption() const { |
| 49 | ext::shared_ptr<Swaption> swaption = *this; |
| 50 | return *swaption; |
| 51 | } |
| 52 | |
| 53 | MakeSwaption::operator ext::shared_ptr<Swaption>() const { |
| 54 | |
| 55 | const Calendar& fixingCalendar = swapIndex_->fixingCalendar(); |
| 56 | Date refDate = Settings::instance().evaluationDate(); |
| 57 | // if the evaluation date is not a business day |
| 58 | // then move to the next business day |
| 59 | refDate = fixingCalendar.adjust(refDate); |
| 60 | if (fixingDate_ == Null<Date>()) |
| 61 | fixingDate_ = fixingCalendar.advance(date: refDate, period: optionTenor_, |
| 62 | convention: optionConvention_); |
| 63 | if (exerciseDate_ == Null<Date>()) { |
| 64 | exercise_ = ext::shared_ptr<Exercise>(new |
| 65 | EuropeanExercise(fixingDate_)); |
| 66 | } else { |
| 67 | QL_REQUIRE(exerciseDate_ <= fixingDate_, |
| 68 | "exercise date (" << exerciseDate_ << ") must be less " |
| 69 | "than or equal to fixing date (" << fixingDate_ << ")" ); |
| 70 | exercise_ = ext::shared_ptr<Exercise>(new |
| 71 | EuropeanExercise(exerciseDate_)); |
| 72 | } |
| 73 | |
| 74 | Rate usedStrike = strike_; |
| 75 | if (strike_ == Null<Rate>()) { |
| 76 | // ATM on curve(s) attached to index |
| 77 | QL_REQUIRE(!swapIndex_->forwardingTermStructure().empty(), |
| 78 | "null term structure set to this instance of " << |
| 79 | swapIndex_->name()); |
| 80 | ext::shared_ptr<VanillaSwap> temp = |
| 81 | swapIndex_->underlyingSwap(fixingDate: fixingDate_); |
| 82 | temp->setPricingEngine( |
| 83 | ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine( |
| 84 | swapIndex_->exogenousDiscount() |
| 85 | ? swapIndex_->discountingTermStructure() |
| 86 | : swapIndex_->forwardingTermStructure(), |
| 87 | false))); |
| 88 | usedStrike = temp->fairRate(); |
| 89 | } |
| 90 | |
| 91 | BusinessDayConvention bdc = swapIndex_->fixedLegConvention(); |
| 92 | underlyingSwap_ = |
| 93 | MakeVanillaSwap(swapIndex_->tenor(), |
| 94 | swapIndex_->iborIndex(), usedStrike) |
| 95 | .withEffectiveDate(swapIndex_->valueDate(fixingDate: fixingDate_)) |
| 96 | .withFixedLegCalendar(cal: swapIndex_->fixingCalendar()) |
| 97 | .withFixedLegDayCount(dc: swapIndex_->dayCounter()) |
| 98 | .withFixedLegTenor(t: swapIndex_->fixedLegTenor()) |
| 99 | .withFixedLegConvention(bdc) |
| 100 | .withFixedLegTerminationDateConvention(bdc) |
| 101 | .withType(type: underlyingType_) |
| 102 | .withNominal(n: nominal_) |
| 103 | .withIndexedCoupons(b: useIndexedCoupons_); |
| 104 | |
| 105 | ext::shared_ptr<Swaption> swaption(new Swaption( |
| 106 | underlyingSwap_, exercise_, delivery_, settlementMethod_)); |
| 107 | swaption->setPricingEngine(engine_); |
| 108 | return swaption; |
| 109 | } |
| 110 | |
| 111 | MakeSwaption& MakeSwaption::withSettlementType(Settlement::Type delivery) { |
| 112 | delivery_ = delivery; |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | MakeSwaption& MakeSwaption::withSettlementMethod( |
| 117 | Settlement::Method settlementMethod) { |
| 118 | settlementMethod_ = settlementMethod; |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | MakeSwaption& |
| 123 | MakeSwaption::withOptionConvention(BusinessDayConvention bdc) { |
| 124 | optionConvention_ = bdc; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | MakeSwaption& MakeSwaption::withExerciseDate(const Date& date) { |
| 129 | exerciseDate_ = date; |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | MakeSwaption& MakeSwaption::withUnderlyingType(const Swap::Type type) { |
| 134 | underlyingType_ = type; |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | MakeSwaption& MakeSwaption::withPricingEngine( |
| 139 | const ext::shared_ptr<PricingEngine>& engine) { |
| 140 | engine_ = engine; |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | MakeSwaption& MakeSwaption::withNominal(Real n) { |
| 145 | nominal_ = n; |
| 146 | return *this; |
| 147 | } |
| 148 | |
| 149 | MakeSwaption& MakeSwaption::withIndexedCoupons(const ext::optional<bool>& b) { |
| 150 | useIndexedCoupons_ = b; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | MakeSwaption& MakeSwaption::withAtParCoupons(bool b) { |
| 155 | useIndexedCoupons_ = !b; |
| 156 | return *this; |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |