Coverage Report

Created: 2026-06-23 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/swaption.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5
 Copyright (C) 2006 Cristina Duminuco
6
 Copyright (C) 2006 Marco Bianchetti
7
 Copyright (C) 2007 StatPro Italia srl
8
 Copyright (C) 2014 Ferdinando Ametrano
9
 Copyright (C) 2016, 2018 Peter Caspers
10
11
 This file is part of QuantLib, a free-software/open-source library
12
 for financial quantitative analysts and developers - http://quantlib.org/
13
14
 QuantLib is free software: you can redistribute it and/or modify it
15
 under the terms of the QuantLib license.  You should have received a
16
 copy of the license along with this program; if not, please email
17
 <quantlib-dev@lists.sf.net>. The license is also available online at
18
 <https://www.quantlib.org/license.shtml>.
19
20
 This program is distributed in the hope that it will be useful, but WITHOUT
21
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22
 FOR A PARTICULAR PURPOSE.  See the license for more details.
23
*/
24
25
/*! \file swaption.hpp
26
    \brief Swaption class
27
*/
28
29
#ifndef quantlib_instruments_swaption_hpp
30
#define quantlib_instruments_swaption_hpp
31
32
#include <ql/option.hpp>
33
#include <ql/instruments/fixedvsfloatingswap.hpp>
34
#include <ql/instruments/vanillaswap.hpp>
35
#include <ql/termstructures/yieldtermstructure.hpp>
36
#include <ql/termstructures/volatility/volatilitytype.hpp>
37
38
namespace QuantLib {
39
40
    //! %settlement information
41
    struct Settlement {
42
        enum Type { Physical, Cash };
43
        enum Method {
44
            PhysicalOTC,
45
            PhysicalCleared,
46
            CollateralizedCashPrice,
47
            ParYieldCurve
48
        };
49
        //! check consistency of settlement type and method
50
        static void checkTypeAndMethodConsistency(Settlement::Type,
51
                                                  Settlement::Method);
52
    };
53
54
    std::ostream& operator<<(std::ostream& out,
55
                             Settlement::Type type);
56
57
    std::ostream& operator<<(std::ostream& out,
58
                             Settlement::Method method);
59
60
    //! %Swaption class
61
    /*! \ingroup instruments
62
63
        \warning it's possible to pass an overnight-indexed swap to
64
                 the constructor, but most engines will treat it as a
65
                 vanilla swap, which is at best a decent proxy.
66
                 Engines that fully support OIS underlyings are
67
                 BlackSwaptionEngine, FdHullWhiteSwaptionEngine,
68
                 and FdG2SwaptionEngine.
69
70
        \test
71
        - the correctness of the returned value is tested by checking
72
          that the price of a payer (resp. receiver) swaption
73
          decreases (resp. increases) with the strike.
74
        - the correctness of the returned value is tested by checking
75
          that the price of a payer (resp. receiver) swaption
76
          increases (resp. decreases) with the spread.
77
        - the correctness of the returned value is tested by checking
78
          it against that of a swaption on a swap with no spread and a
79
          correspondingly adjusted fixed rate.
80
        - the correctness of the returned value is tested by checking
81
          it against a known good value.
82
        - the correctness of the returned value of cash settled swaptions
83
          is tested by checking the modified annuity against a value
84
          calculated without using the Swaption class.
85
86
87
        \todo add greeks and explicit exercise lag
88
    */
89
    class Swaption : public Option {
90
      public:
91
        enum PriceType { Spot, Forward };
92
        class arguments;
93
        class engine;
94
        Swaption(ext::shared_ptr<FixedVsFloatingSwap> swap,
95
                 const ext::shared_ptr<Exercise>& exercise,
96
                 Settlement::Type delivery = Settlement::Physical,
97
                 Settlement::Method settlementMethod = Settlement::PhysicalOTC);
98
        //! \name Observer interface
99
        //@{
100
        void deepUpdate() override;
101
        //@}
102
        //! \name Instrument interface
103
        //@{
104
        bool isExpired() const override;
105
        void setupArguments(PricingEngine::arguments*) const override;
106
        //@}
107
        //! \name Inspectors
108
        //@{
109
0
        Settlement::Type settlementType() const { return settlementType_; }
110
0
        Settlement::Method settlementMethod() const {
111
0
            return settlementMethod_;
112
0
        }
113
0
        Swap::Type type() const { return swap_->type(); }
114
0
        const ext::shared_ptr<FixedVsFloatingSwap>& underlying() const {
115
0
            return swap_;
116
0
        }
117
        //@}
118
        //! implied volatility
119
        Volatility impliedVolatility(
120
                              Real price,
121
                              const Handle<YieldTermStructure>& discountCurve,
122
                              Volatility guess,
123
                              Real accuracy = 1.0e-4,
124
                              Natural maxEvaluations = 100,
125
                              Volatility minVol = 1.0e-7,
126
                              Volatility maxVol = 4.0,
127
                              VolatilityType type = ShiftedLognormal,
128
                              Real displacement = 0.0,
129
                              PriceType priceType = Spot) const;
130
      private:
131
        // arguments
132
        ext::shared_ptr<FixedVsFloatingSwap> swap_;
133
        //Handle<YieldTermStructure> termStructure_;
134
        Settlement::Type settlementType_;
135
        Settlement::Method settlementMethod_;
136
        // until we remove underlyingSwap();
137
        ext::shared_ptr<VanillaSwap> vanilla_;
138
    };
139
140
    //! %Arguments for swaption calculation
141
    class Swaption::arguments : public FixedVsFloatingSwap::arguments,
142
                                public Option::arguments {
143
      public:
144
0
        arguments() = default;
145
        ext::shared_ptr<FixedVsFloatingSwap> swap;
146
        Settlement::Type settlementType = Settlement::Physical;
147
        Settlement::Method settlementMethod;
148
        void validate() const override;
149
    };
150
151
    //! base class for swaption engines
152
    class Swaption::engine
153
        : public GenericEngine<Swaption::arguments, Swaption::results> {};
154
155
}
156
157
#endif