QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
swaptionhelper.cpp
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) 2007 StatPro Italia srl
6 Copyright (C) 2015 Peter Caspers
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/indexes/iborindex.hpp>
23#include <ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp>
24#include <ql/pricingengines/swap/discountingswapengine.hpp>
25#include <ql/pricingengines/swaption/blackswaptionengine.hpp>
26#include <ql/pricingengines/swaption/discretizedswaption.hpp>
27#include <ql/quotes/simplequote.hpp>
28#include <ql/time/schedule.hpp>
29#include <utility>
30
31namespace QuantLib {
32
34 const Period& length,
35 const Handle<Quote>& volatility,
36 ext::shared_ptr<IborIndex> index,
37 const Period& fixedLegTenor,
38 DayCounter fixedLegDayCounter,
39 DayCounter floatingLegDayCounter,
40 Handle<YieldTermStructure> termStructure,
42 const Real strike,
43 const Real nominal,
44 const VolatilityType type,
45 const Real shift)
46 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(Null<Date>()),
47 endDate_(Null<Date>()), maturity_(maturity), length_(length), fixedLegTenor_(fixedLegTenor),
48 index_(std::move(index)), termStructure_(std::move(termStructure)),
49 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
50 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal) {
53 }
54
56 const Period& length,
57 const Handle<Quote>& volatility,
58 ext::shared_ptr<IborIndex> index,
59 const Period& fixedLegTenor,
60 DayCounter fixedLegDayCounter,
61 DayCounter floatingLegDayCounter,
62 Handle<YieldTermStructure> termStructure,
64 const Real strike,
65 const Real nominal,
66 const VolatilityType type,
67 const Real shift)
68 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(exerciseDate),
69 endDate_(Null<Date>()), maturity_(0 * Days), length_(length), fixedLegTenor_(fixedLegTenor),
70 index_(std::move(index)), termStructure_(std::move(termStructure)),
71 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
72 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal) {
75 }
76
78 const Date& endDate,
79 const Handle<Quote>& volatility,
80 ext::shared_ptr<IborIndex> index,
81 const Period& fixedLegTenor,
82 DayCounter fixedLegDayCounter,
83 DayCounter floatingLegDayCounter,
84 Handle<YieldTermStructure> termStructure,
86 const Real strike,
87 const Real nominal,
88 const VolatilityType type,
89 const Real shift)
90 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(exerciseDate),
91 endDate_(endDate), maturity_(0 * Days), length_(0 * Days), fixedLegTenor_(fixedLegTenor),
92 index_(std::move(index)), termStructure_(std::move(termStructure)),
93 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
94 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal) {
97 }
98
99
100 void SwaptionHelper::addTimesTo(std::list<Time>& times) const {
101 calculate();
103 swaption_->setupArguments(&args);
104 std::vector<Time> swaptionTimes =
106 termStructure_->referenceDate(),
107 termStructure_->dayCounter()).mandatoryTimes();
108 times.insert(times.end(),
109 swaptionTimes.begin(), swaptionTimes.end());
110 }
111
113 calculate();
114 swaption_->setPricingEngine(engine_);
115 return swaption_->NPV();
116 }
117
119 calculate();
120 Handle<Quote> vol(ext::shared_ptr<Quote>(new SimpleQuote(sigma)));
121 ext::shared_ptr<PricingEngine> engine;
122 switch(volatilityType_) {
123 case ShiftedLognormal:
124 engine = ext::make_shared<BlackSwaptionEngine>(
126 break;
127 case Normal:
128 engine = ext::make_shared<BachelierSwaptionEngine>(
130 break;
131 default:
132 QL_FAIL("can not construct engine: " << volatilityType_);
133 break;
134 }
135 swaption_->setPricingEngine(engine);
136 Real value = swaption_->NPV();
137 swaption_->setPricingEngine(engine_);
138 return value;
139 }
140
142
143 Calendar calendar = index_->fixingCalendar();
144
145 Date exerciseDate = exerciseDate_;
146 if (exerciseDate == Null<Date>())
147 exerciseDate = calendar.advance(termStructure_->referenceDate(),
148 maturity_,
149 index_->businessDayConvention());
150
151 Date startDate = index_->valueDate(index_->fixingCalendar().adjust(exerciseDate));
152
153 Date endDate = endDate_;
154 if (endDate == Null<Date>())
155 endDate = calendar.advance(startDate, length_,
156 index_->businessDayConvention());
157
158 Schedule fixedSchedule(startDate, endDate, fixedLegTenor_, calendar,
159 index_->businessDayConvention(),
160 index_->businessDayConvention(),
162 Schedule floatSchedule(startDate, endDate, index_->tenor(), calendar,
163 index_->businessDayConvention(),
164 index_->businessDayConvention(),
166
167 ext::shared_ptr<PricingEngine> swapEngine(
169
171
173 fixedSchedule, 0.0, fixedLegDayCounter_,
174 floatSchedule, index_, 0.0, floatingLegDayCounter_);
175 temp.setPricingEngine(swapEngine);
176 Real forward = temp.fairRate();
177 if(strike_ == Null<Real>()) {
178 exerciseRate_ = forward;
179 }
180 else {
182 type = strike_ <= forward ? Swap::Receiver : Swap::Payer;
183 // ensure that calibration instrument is out of the money
184 }
185 swap_ = ext::make_shared<VanillaSwap>(
186 type, nominal_,
187 fixedSchedule, exerciseRate_, fixedLegDayCounter_,
188 floatSchedule, index_, 0.0, floatingLegDayCounter_);
189 swap_->setPricingEngine(swapEngine);
190
191 ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));
192
193 swaption_ = ext::make_shared<Swaption>(swap_, exercise);
194
196
197 }
198
199}
Actual/365 (Fixed) day count convention.
liquid Black76 market instrument used during calibration
void performCalculations() const override
ext::shared_ptr< PricingEngine > engine_
calendar class
Definition: calendar.hpp:61
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
std::vector< Time > mandatoryTimes() const override
European exercise.
Definition: exercise.hpp:96
Shared handle to an observable.
Definition: handle.hpp:41
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Payment schedule.
Definition: schedule.hpp:40
market element returning a stored value
Definition: simplequote.hpp:33
Arguments for swaption calculation
Definition: swaption.hpp:130
ext::shared_ptr< Swaption > swaption_
void performCalculations() const override
ext::shared_ptr< VanillaSwap > swap_
const ext::shared_ptr< IborIndex > index_
Real blackPrice(Volatility volatility) const override
Black or Bachelier price given a volatility.
Real modelValue() const override
returns the price of the instrument according to the model
SwaptionHelper(const Period &maturity, const Period &length, const Handle< Quote > &volatility, ext::shared_ptr< IborIndex > index, const Period &fixedLegTenor, DayCounter fixedLegDayCounter, DayCounter floatingLegDayCounter, Handle< YieldTermStructure > termStructure, BlackCalibrationHelper::CalibrationErrorType errorType=BlackCalibrationHelper::RelativePriceError, Real strike=Null< Real >(), Real nominal=1.0, VolatilityType type=ShiftedLognormal, Real shift=0.0)
void addTimesTo(std::list< Time > &times) const override
const DayCounter floatingLegDayCounter_
const Handle< YieldTermStructure > termStructure_
const DayCounter fixedLegDayCounter_
Plain-vanilla swap: fix vs ibor leg.
Definition: vanillaswap.hpp:65
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Definition: any.hpp:35
STL namespace.