| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2013, 2018 Peter Caspers |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/exercise.hpp> |
| 21 | #include <ql/instruments/floatfloatswaption.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | FloatFloatSwaption::FloatFloatSwaption(ext::shared_ptr<FloatFloatSwap> swap, |
| 27 | const ext::shared_ptr<Exercise>& exercise, |
| 28 | Settlement::Type delivery, |
| 29 | Settlement::Method settlementMethod) |
| 30 | : Option(ext::shared_ptr<Payoff>(), exercise), swap_(std::move(swap)), |
| 31 | settlementType_(delivery), settlementMethod_(settlementMethod) { |
| 32 | registerWith(h: swap_); |
| 33 | // When we ask for the NPV of an expired swaption, the |
| 34 | // swap is not recalculated and thus wouldn't forward |
| 35 | // later notifications according to the default behavior of |
| 36 | // LazyObject instances. This means that even if the |
| 37 | // evaluation date changes so that the swaption is no longer |
| 38 | // expired, the instrument wouldn't be notified and thus it |
| 39 | // wouldn't recalculate. To avoid this, we override the |
| 40 | // default behavior of the underlying swap. |
| 41 | swap_->alwaysForwardNotifications(); |
| 42 | } |
| 43 | |
| 44 | bool FloatFloatSwaption::isExpired() const { |
| 45 | return detail::simple_event(exercise_->dates().back()).hasOccurred(); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | FloatFloatSwaption::setupArguments(PricingEngine::arguments *args) const { |
| 50 | |
| 51 | swap_->setupArguments(args); |
| 52 | |
| 53 | auto* arguments = dynamic_cast<FloatFloatSwaption::arguments*>(args); |
| 54 | |
| 55 | QL_REQUIRE(arguments != nullptr, "wrong argument type" ); |
| 56 | |
| 57 | arguments->swap = swap_; |
| 58 | arguments->exercise = exercise_; |
| 59 | arguments->settlementType = settlementType_; |
| 60 | arguments->settlementMethod = settlementMethod_; |
| 61 | } |
| 62 | |
| 63 | void FloatFloatSwaption::arguments::validate() const { |
| 64 | FloatFloatSwap::arguments::validate(); |
| 65 | QL_REQUIRE(swap, "underlying cms swap not set" ); |
| 66 | QL_REQUIRE(exercise, "exercise not set" ); |
| 67 | Settlement::checkTypeAndMethodConsistency(settlementType, |
| 68 | settlementMethod); |
| 69 | } |
| 70 | |
| 71 | std::vector<ext::shared_ptr<BlackCalibrationHelper>> |
| 72 | FloatFloatSwaption::calibrationBasket( |
| 73 | const ext::shared_ptr<SwapIndex>& standardSwapBase, |
| 74 | const ext::shared_ptr<SwaptionVolatilityStructure>& swaptionVolatility, |
| 75 | const BasketGeneratingEngine::CalibrationBasketType basketType) const { |
| 76 | |
| 77 | ext::shared_ptr<BasketGeneratingEngine> engine = |
| 78 | ext::dynamic_pointer_cast<BasketGeneratingEngine>(r: engine_); |
| 79 | QL_REQUIRE(engine, "engine is not a basket generating engine" ); |
| 80 | engine_->reset(); |
| 81 | setupArguments(engine_->getArguments()); |
| 82 | engine_->getArguments()->validate(); |
| 83 | return engine->calibrationBasket(exercise: exercise_, standardSwapBase, |
| 84 | swaptionVolatility, basketType); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |