Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/nonstandardswaption.cpp
Line
Count
Source
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
 <https://www.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/nonstandardswaption.hpp>
22
#include <utility>
23
24
namespace QuantLib {
25
26
    NonstandardSwaption::NonstandardSwaption(const Swaption &fromSwaption)
27
0
        : Option(ext::shared_ptr<Payoff>(),
28
0
                 const_cast<Swaption &>(fromSwaption).exercise()),
29
0
          swap_(ext::make_shared<NonstandardSwap>(
30
0
              *fromSwaption.underlying())),
31
0
          settlementType_(fromSwaption.settlementType()),
32
0
          settlementMethod_(fromSwaption.settlementMethod()) {
33
34
0
        registerWith(swap_);
35
        // When we ask for the NPV of an expired swaption, the
36
        // swap is not recalculated and thus wouldn't forward
37
        // later notifications according to the default behavior of
38
        // LazyObject instances.  This means that even if the
39
        // evaluation date changes so that the swaption is no longer
40
        // expired, the instrument wouldn't be notified and thus it
41
        // wouldn't recalculate.  To avoid this, we override the
42
        // default behavior of the underlying swap.
43
0
        swap_->alwaysForwardNotifications();
44
0
    }
Unexecuted instantiation: QuantLib::NonstandardSwaption::NonstandardSwaption(QuantLib::Swaption const&)
Unexecuted instantiation: QuantLib::NonstandardSwaption::NonstandardSwaption(QuantLib::Swaption const&)
45
46
    NonstandardSwaption::NonstandardSwaption(ext::shared_ptr<NonstandardSwap> swap,
47
                                             const ext::shared_ptr<Exercise>& exercise,
48
                                             Settlement::Type delivery,
49
                                             Settlement::Method settlementMethod)
50
0
    : Option(ext::shared_ptr<Payoff>(), exercise), swap_(std::move(swap)),
51
0
      settlementType_(delivery), settlementMethod_(settlementMethod) {
52
0
        registerWith(swap_);
53
0
        swap_->alwaysForwardNotifications();
54
0
    }
Unexecuted instantiation: QuantLib::NonstandardSwaption::NonstandardSwaption(boost::shared_ptr<QuantLib::NonstandardSwap>, boost::shared_ptr<QuantLib::Exercise> const&, QuantLib::Settlement::Type, QuantLib::Settlement::Method)
Unexecuted instantiation: QuantLib::NonstandardSwaption::NonstandardSwaption(boost::shared_ptr<QuantLib::NonstandardSwap>, boost::shared_ptr<QuantLib::Exercise> const&, QuantLib::Settlement::Type, QuantLib::Settlement::Method)
55
56
0
    bool NonstandardSwaption::isExpired() const {
57
58
0
        return detail::simple_event(exercise_->dates().back()).hasOccurred();
59
0
    }
60
61
    void
62
0
    NonstandardSwaption::setupArguments(PricingEngine::arguments *args) const {
63
64
0
        swap_->setupArguments(args);
65
66
0
        auto* arguments = dynamic_cast<NonstandardSwaption::arguments*>(args);
67
68
0
        QL_REQUIRE(arguments != nullptr, "argument types do not match");
69
70
0
        arguments->swap = swap_;
71
0
        arguments->exercise = exercise_;
72
0
        arguments->settlementType = settlementType_;
73
0
        arguments->settlementMethod = settlementMethod_;
74
0
    }
75
76
0
    void NonstandardSwaption::arguments::validate() const {
77
78
0
        NonstandardSwap::arguments::validate();
79
0
        QL_REQUIRE(swap, "underlying non standard swap not set");
80
0
        QL_REQUIRE(exercise, "exercise not set");
81
0
        Settlement::checkTypeAndMethodConsistency(settlementType,
82
0
                                                  settlementMethod);
83
0
    }
84
85
    std::vector<ext::shared_ptr<BlackCalibrationHelper>>
86
    NonstandardSwaption::calibrationBasket(
87
        const ext::shared_ptr<SwapIndex>& standardSwapBase,
88
        const ext::shared_ptr<SwaptionVolatilityStructure>& swaptionVolatility,
89
0
        const BasketGeneratingEngine::CalibrationBasketType basketType) const {
90
91
0
        ext::shared_ptr<BasketGeneratingEngine> engine =
92
0
            ext::dynamic_pointer_cast<BasketGeneratingEngine>(engine_);
93
0
        QL_REQUIRE(engine, "engine is not a basket generating engine");
94
0
        engine_->reset();
95
0
        setupArguments(engine_->getArguments());
96
0
        engine_->getArguments()->validate();
97
0
        return engine->calibrationBasket(exercise_, standardSwapBase,
98
0
                                         swaptionVolatility, basketType);
99
0
    }
100
}