File: swaption.i

package info (click to toggle)
quantlib-swig 1.40-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,276 kB
  • sloc: python: 6,024; java: 1,552; cs: 774; makefile: 309; sh: 22
file content (270 lines) | stat: -rw-r--r-- 9,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

/*
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
 Copyright (C) 2016 Peter Caspers
 Copyright (C) 2017, 2018, 2019 Matthias Lungwitz
 Copyright (C) 2020 Marcin Rybacki

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#ifndef quantlib_swaption_i
#define quantlib_swaption_i

%include options.i
%include marketelements.i
%include termstructures.i
%include volatilities.i
%include swap.i
%include old_volatility.i
%include daycounters.i

%{
using QuantLib::Actual365Fixed;
using QuantLib::Swaption;
using QuantLib::NonstandardSwaption;
using QuantLib::Settlement;
using QuantLib::FloatFloatSwaption;
using QuantLib::MakeSwaption;
%}

struct Settlement {
    enum Type { Physical, Cash };
    enum Method { PhysicalOTC, PhysicalCleared, CollateralizedCashPrice, ParYieldCurve };
};
    
%shared_ptr(Swaption)
class Swaption : public Option {
  public:
    enum PriceType { Spot, Forward };

    Swaption(const ext::shared_ptr<FixedVsFloatingSwap>& swap,
             const ext::shared_ptr<Exercise>& exercise,
             Settlement::Type type = Settlement::Physical,
             Settlement::Method settlementMethod = Settlement::PhysicalOTC);
    
    Settlement::Type settlementType() const;       
    Settlement::Method settlementMethod() const;
    VanillaSwap::Type type() const;
    const ext::shared_ptr<FixedVsFloatingSwap>& underlying() const;
    
    #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
    %feature("kwargs") impliedVolatility;
    #endif
    Volatility impliedVolatility(
                          Real price,
                          const Handle<YieldTermStructure>& discountCurve,
                          Volatility guess,
                          Real accuracy = 1.0e-4,
                          Natural maxEvaluations = 100,
                          Volatility minVol = 1.0e-7,
                          Volatility maxVol = 4.0,
                          VolatilityType type = ShiftedLognormal,
                          Real displacement = 0.0,
                          PriceType priceType = Spot) const;
    %extend {
        Real vega() {
            return self->result<Real>("vega");
        }

        Real delta() {
            return self->result<Real>("delta");
        }

        Real annuity() {
            return self->result<Real>("annuity");
        }

        Real forwardPrice() {
            return self->result<Real>("forwardPrice");
        }
    }
};

%{
using QuantLib::BasketGeneratingEngine;
%}

%shared_ptr(NonstandardSwaption)
class NonstandardSwaption : public Instrument {
  public:
    NonstandardSwaption(const ext::shared_ptr<NonstandardSwap>& swap,
                const ext::shared_ptr<Exercise>& exercise,
                Settlement::Type type = Settlement::Physical,
                Settlement::Method settlementMethod = Settlement::PhysicalOTC);
                
    const ext::shared_ptr<NonstandardSwap> &underlyingSwap() const;

    %extend {                
        std::vector<ext::shared_ptr<BlackCalibrationHelper> > calibrationBasket(
            ext::shared_ptr<SwapIndex> swapIndex,
            ext::shared_ptr<SwaptionVolatilityStructure> swaptionVolatility,
            std::string typeStr) {

            BasketGeneratingEngine::CalibrationBasketType type;
            if(typeStr == "Naive")
                type = BasketGeneratingEngine::Naive;
            else if(typeStr == "MaturityStrikeByDeltaGamma")
                type = BasketGeneratingEngine::MaturityStrikeByDeltaGamma;
            else
                QL_FAIL("type " << typeStr << "unknown.");

            std::vector<ext::shared_ptr<BlackCalibrationHelper> > hs =
                self->calibrationBasket(swapIndex, swaptionVolatility, type);
            std::vector<ext::shared_ptr<BlackCalibrationHelper> > helpers(hs.size());
            for (Size i=0; i<hs.size(); ++i)
                helpers[i] = hs[i];
            return helpers;
        }


        std::vector<Real> probabilities() {
            return self->result<std::vector<Real> >("probabilities");
        }
    }
};

%shared_ptr(FloatFloatSwaption)
class FloatFloatSwaption : public Instrument {
public:
    FloatFloatSwaption(const ext::shared_ptr<FloatFloatSwap>& swap,
                const ext::shared_ptr<Exercise>& exercise,
                Settlement::Type delivery = Settlement::Physical,
                Settlement::Method settlementMethod = Settlement::PhysicalOTC);

    const ext::shared_ptr<FloatFloatSwap> &underlyingSwap();
    
    %extend {

        std::vector<ext::shared_ptr<BlackCalibrationHelper> > calibrationBasket(
        ext::shared_ptr<SwapIndex> swapIndex,
        ext::shared_ptr<SwaptionVolatilityStructure> swaptionVolatility,
        std::string typeStr) {

        BasketGeneratingEngine::CalibrationBasketType type;
        if(typeStr == "Naive")
            type = BasketGeneratingEngine::Naive;
        else if(typeStr == "MaturityStrikeByDeltaGamma")
            type = BasketGeneratingEngine::MaturityStrikeByDeltaGamma;
        else
            QL_FAIL("type " << typeStr << "unknown.");

        std::vector<ext::shared_ptr<BlackCalibrationHelper> > hs =
            self->calibrationBasket(swapIndex, swaptionVolatility, type);
        std::vector<ext::shared_ptr<BlackCalibrationHelper> > helpers(hs.size());
        for (Size i=0; i<hs.size(); ++i)
            helpers[i] = hs[i];
        return helpers;
        }

        Real underlyingValue() {
            return self->result<Real>("underlyingValue");
        }

        std::vector<Real> probabilities() {
            return self->result<std::vector<Real> >("probabilities");
        }
    }
};

// pricing engines

%{
using QuantLib::BlackSwaptionEngine;
using QuantLib::BachelierSwaptionEngine;
%}

%shared_ptr(BlackSwaptionEngine)
class BlackSwaptionEngine : public PricingEngine {
  public:
    enum CashAnnuityModel { SwapRate, DiscountCurve };
    BlackSwaptionEngine(const Handle<YieldTermStructure> & discountCurve,
                        const Handle<Quote>& vol,
                        const DayCounter& dc = Actual365Fixed(),
                        Real displacement = 0.0,
                        CashAnnuityModel model = DiscountCurve);
    BlackSwaptionEngine(const Handle<YieldTermStructure> & discountCurve,
                        const Handle<SwaptionVolatilityStructure>& v,
                        CashAnnuityModel model = DiscountCurve);
};

%shared_ptr(BachelierSwaptionEngine)
class BachelierSwaptionEngine : public PricingEngine {
  public:
    enum CashAnnuityModel { SwapRate, DiscountCurve };
    BachelierSwaptionEngine(const Handle<YieldTermStructure> & discountCurve,
                            const Handle<Quote>& vol,
                            const DayCounter& dc = Actual365Fixed(),
                            CashAnnuityModel model = DiscountCurve);
    BachelierSwaptionEngine(const Handle<YieldTermStructure> & discountCurve,
                            const Handle<SwaptionVolatilityStructure>& v,
                            CashAnnuityModel model = DiscountCurve);
};

#if defined(SWIGPYTHON)
%rename (_MakeSwaption) MakeSwaption;
#endif
class MakeSwaption {
    public:
    MakeSwaption& withNominal(Real n);
    MakeSwaption& withSettlementType(Settlement::Type delivery);
    MakeSwaption& withSettlementMethod(Settlement::Method settlementMethod);
    MakeSwaption& withOptionConvention(BusinessDayConvention bdc);
    MakeSwaption& withExerciseDate(const Date&);
    MakeSwaption& withUnderlyingType(Swap::Type type);

    MakeSwaption& withIndexedCoupons(bool flag = true);
    MakeSwaption& withAtParCoupons(bool flag = true);

    MakeSwaption& withPricingEngine(
                            const ext::shared_ptr<PricingEngine>& engine);

    MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
                const Period& optionTenor,
                doubleOrNull strike = Null<Rate>());

    MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
                const Date& fixingDate,
                doubleOrNull strike = Null<Rate>());
        
    %extend {
        ext::shared_ptr<Swaption> makeSwaption() {
            return (ext::shared_ptr<Swaption>)(* $self);
        }
    }
};

#if defined(SWIGPYTHON)
%pythoncode {
_MAKESWAPTION_METHODS = {
    "nominal": "withNominal",
    "settlementType": "withSettlementType",
    "settlementMethod": "withSettlementMethod",
    "optionConvention": "withOptionConvention",
    "exerciseDate": "withExerciseDate",
    "underlyingType": "withUndelyingType",
    "pricingEngine": "withPricingEngine",
    "indexedCoupons": "withIndexedCoupons",
    "atParCoupons": "withAtParCoupons",
}

def MakeSwaption(swapIndex, fixingDate, strike=None, **kwargs):
    mv = _MakeSwaption(swapIndex, fixingDate, strike)
    _apply_kwargs("MakeSwaption", _MAKESWAPTION_METHODS, mv, kwargs)
    return mv.makeSwaption()
}
#endif

#endif