Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/instruments/swaption.hpp
Line
Count
Source (jump to first uncovered line)
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
 <http://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 the only engine to fully support
65
                 it is BlackSwaptionEngine; other engines will treat
66
                 it as a vanilla swap.  This is at best a decent
67
                 proxy, at worst simply wrong.  Use with caution.
68
69
        \test
70
        - the correctness of the returned value is tested by checking
71
          that the price of a payer (resp. receiver) swaption
72
          decreases (resp. increases) with the strike.
73
        - the correctness of the returned value is tested by checking
74
          that the price of a payer (resp. receiver) swaption
75
          increases (resp. decreases) with the spread.
76
        - the correctness of the returned value is tested by checking
77
          it against that of a swaption on a swap with no spread and a
78
          correspondingly adjusted fixed rate.
79
        - the correctness of the returned value is tested by checking
80
          it against a known good value.
81
        - the correctness of the returned value of cash settled swaptions
82
          is tested by checking the modified annuity against a value
83
          calculated without using the Swaption class.
84
85
86
        \todo add greeks and explicit exercise lag
87
    */
88
    class Swaption : public Option {
89
      public:
90
        enum PriceType { Spot, Forward };
91
        class arguments;
92
        class engine;
93
        Swaption(ext::shared_ptr<FixedVsFloatingSwap> swap,
94
                 const ext::shared_ptr<Exercise>& exercise,
95
                 Settlement::Type delivery = Settlement::Physical,
96
                 Settlement::Method settlementMethod = Settlement::PhysicalOTC);
97
        //! \name Observer interface
98
        //@{
99
        void deepUpdate() override;
100
        //@}
101
        //! \name Instrument interface
102
        //@{
103
        bool isExpired() const override;
104
        void setupArguments(PricingEngine::arguments*) const override;
105
        //@}
106
        //! \name Inspectors
107
        //@{
108
0
        Settlement::Type settlementType() const { return settlementType_; }
109
0
        Settlement::Method settlementMethod() const {
110
0
            return settlementMethod_;
111
0
        }
112
0
        Swap::Type type() const { return swap_->type(); }
113
0
        const ext::shared_ptr<FixedVsFloatingSwap>& underlying() const {
114
0
            return swap_;
115
0
        }
116
        //@}
117
        //! implied volatility
118
        Volatility impliedVolatility(
119
                              Real price,
120
                              const Handle<YieldTermStructure>& discountCurve,
121
                              Volatility guess,
122
                              Real accuracy = 1.0e-4,
123
                              Natural maxEvaluations = 100,
124
                              Volatility minVol = 1.0e-7,
125
                              Volatility maxVol = 4.0,
126
                              VolatilityType type = ShiftedLognormal,
127
                              Real displacement = 0.0,
128
                              PriceType priceType = Spot) const;
129
      private:
130
        // arguments
131
        ext::shared_ptr<FixedVsFloatingSwap> swap_;
132
        //Handle<YieldTermStructure> termStructure_;
133
        Settlement::Type settlementType_;
134
        Settlement::Method settlementMethod_;
135
        // until we remove underlyingSwap();
136
        ext::shared_ptr<VanillaSwap> vanilla_;
137
    };
138
139
    //! %Arguments for swaption calculation
140
    class Swaption::arguments : public FixedVsFloatingSwap::arguments,
141
                                public Option::arguments {
142
      public:
143
0
        arguments() = default;
144
        ext::shared_ptr<FixedVsFloatingSwap> swap;
145
        Settlement::Type settlementType = Settlement::Physical;
146
        Settlement::Method settlementMethod;
147
        void validate() const override;
148
    };
149
150
    //! base class for swaption engines
151
    class Swaption::engine
152
        : public GenericEngine<Swaption::arguments, Swaption::results> {};
153
154
}
155
156
#endif