QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
swaptionhelper.cpp
Go to the documentation of this file.
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
30#include <ql/time/schedule.hpp>
31#include <utility>
32
33namespace QuantLib {
34
36 const Period& length,
37 const Handle<Quote>& volatility,
38 ext::shared_ptr<IborIndex> index,
39 const Period& fixedLegTenor,
40 DayCounter fixedLegDayCounter,
41 DayCounter floatingLegDayCounter,
42 Handle<YieldTermStructure> termStructure,
43 CalibrationErrorType errorType,
44 const Real strike,
45 const Real nominal,
46 const VolatilityType type,
47 const Real shift,
48 Natural settlementDays,
49 RateAveraging::Type averagingMethod)
50 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(Null<Date>()),
51 endDate_(Null<Date>()), maturity_(maturity), length_(length), fixedLegTenor_(fixedLegTenor),
52 index_(std::move(index)), termStructure_(std::move(termStructure)),
53 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
54 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal),
55 settlementDays_(settlementDays), averagingMethod_(averagingMethod) {
58 }
59
61 const Period& length,
62 const Handle<Quote>& volatility,
63 ext::shared_ptr<IborIndex> index,
64 const Period& fixedLegTenor,
65 DayCounter fixedLegDayCounter,
66 DayCounter floatingLegDayCounter,
67 Handle<YieldTermStructure> termStructure,
68 CalibrationErrorType errorType,
69 const Real strike,
70 const Real nominal,
71 const VolatilityType type,
72 const Real shift,
73 Natural settlementDays,
74 RateAveraging::Type averagingMethod)
75 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(exerciseDate),
76 endDate_(Null<Date>()), maturity_(0 * Days), length_(length), fixedLegTenor_(fixedLegTenor),
77 index_(std::move(index)), termStructure_(std::move(termStructure)),
78 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
79 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal),
80 settlementDays_(settlementDays), averagingMethod_(averagingMethod) {
83 }
84
86 const Date& endDate,
87 const Handle<Quote>& volatility,
88 ext::shared_ptr<IborIndex> index,
89 const Period& fixedLegTenor,
90 DayCounter fixedLegDayCounter,
91 DayCounter floatingLegDayCounter,
92 Handle<YieldTermStructure> termStructure,
93 CalibrationErrorType errorType,
94 const Real strike,
95 const Real nominal,
96 const VolatilityType type,
97 const Real shift,
98 Natural settlementDays,
99 RateAveraging::Type averagingMethod)
100 : BlackCalibrationHelper(volatility, errorType, type, shift), exerciseDate_(exerciseDate),
101 endDate_(endDate), maturity_(0 * Days), length_(0 * Days), fixedLegTenor_(fixedLegTenor),
102 index_(std::move(index)), termStructure_(std::move(termStructure)),
103 fixedLegDayCounter_(std::move(fixedLegDayCounter)),
104 floatingLegDayCounter_(std::move(floatingLegDayCounter)), strike_(strike), nominal_(nominal),
105 settlementDays_(settlementDays), averagingMethod_(averagingMethod) {
108 }
109
110
111 void SwaptionHelper::addTimesTo(std::list<Time>& times) const {
112 calculate();
114 swaption_->setupArguments(&args);
115 std::vector<Time> swaptionTimes =
117 termStructure_->referenceDate(),
118 termStructure_->dayCounter()).mandatoryTimes();
119 times.insert(times.end(),
120 swaptionTimes.begin(), swaptionTimes.end());
121 }
122
124 calculate();
125 swaption_->setPricingEngine(engine_);
126 return swaption_->NPV();
127 }
128
130 calculate();
131 Handle<Quote> vol(ext::shared_ptr<Quote>(new SimpleQuote(sigma)));
132 ext::shared_ptr<PricingEngine> engine;
133 switch(volatilityType_) {
134 case ShiftedLognormal:
135 engine = ext::make_shared<BlackSwaptionEngine>(
137 break;
138 case Normal:
139 engine = ext::make_shared<BachelierSwaptionEngine>(
141 break;
142 default:
143 QL_FAIL("can not construct engine: " << volatilityType_);
144 break;
145 }
146 swaption_->setPricingEngine(engine);
147 Real value = swaption_->NPV();
148 swaption_->setPricingEngine(engine_);
149 return value;
150 }
151
153
154 Calendar calendar = index_->fixingCalendar();
155 Date exerciseDate = exerciseDate_;
156 if (exerciseDate == Null<Date>())
157 exerciseDate = calendar.advance(termStructure_->referenceDate(),
158 maturity_,
159 index_->businessDayConvention());
160 Date startDate;
161 if (settlementDays_ == Null<Size>()) {
162 startDate = index_->valueDate(index_->fixingCalendar().adjust(exerciseDate));
163 } else {
164 startDate = calendar.advance(exerciseDate,
165 index_->fixingDays(), Days,
166 index_->businessDayConvention());
167 }
168
169 Date endDate = endDate_;
170 if (endDate == Null<Date>())
171 endDate = calendar.advance(startDate, length_,
172 index_->businessDayConvention());
173 Schedule fixedSchedule(startDate, endDate, fixedLegTenor_, calendar,
174 index_->businessDayConvention(),
175 index_->businessDayConvention(),
177 Schedule floatSchedule(startDate, endDate, index_->tenor(), calendar,
178 index_->businessDayConvention(),
179 index_->businessDayConvention(),
181
182 auto swapEngine = ext::make_shared<DiscountingSwapEngine>(termStructure_, false);
183
185 ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));
186 auto temp = makeSwap(fixedSchedule, floatSchedule, 0.0, type);
187 temp->setPricingEngine(swapEngine);
188 Real forward = temp->fairRate();
189 if (strike_ == Null<Real>()) {
190 exerciseRate_ = forward;
191 } else {
193 type = strike_ <= forward ? Swap::Receiver : Swap::Payer;
194 }
195 swap_ = makeSwap(fixedSchedule, floatSchedule, exerciseRate_, type);
196 swap_->setPricingEngine(swapEngine);
197 swaption_ = ext::make_shared<Swaption>(swap_, exercise);
199 }
200
201 ext::shared_ptr<FixedVsFloatingSwap> SwaptionHelper::makeSwap(Schedule fixedSchedule,
202 Schedule floatSchedule,
203 Rate exerciseRate,
204 Swap::Type type) const {
205 auto onIndex = ext::dynamic_pointer_cast<OvernightIndex>(index_);
206 if (onIndex) {
207 return ext::make_shared<OvernightIndexedSwap>(
208 type, nominal_, std::move(fixedSchedule), exerciseRate, fixedLegDayCounter_,
209 std::move(floatSchedule), onIndex, 0.0, 0, Following,
210 Calendar(), true, averagingMethod_);
211 } else {
212 return ext::make_shared<VanillaSwap>(type, nominal_, std::move(fixedSchedule), exerciseRate,
213 fixedLegDayCounter_, std::move(floatSchedule), index_, 0.0,
215 }
216 }
217
218}
Black-formula swaption engine.
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
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:147
ext::shared_ptr< Swaption > swaption_
void performCalculations() const override
ext::shared_ptr< FixedVsFloatingSwap > makeSwap(Schedule fixedSchedule, Schedule floatSchedule, Rate exerciseRate, Swap::Type type) const
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
void addTimesTo(std::list< Time > &times) const override
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, CalibrationErrorType errorType=RelativePriceError, Real strike=Null< Real >(), Real nominal=1.0, VolatilityType type=ShiftedLognormal, Real shift=0.0, Natural settlementDays=Null< Size >(), RateAveraging::Type averagingMethod=RateAveraging::Compound)
ext::shared_ptr< FixedVsFloatingSwap > swap_
const DayCounter floatingLegDayCounter_
const Handle< YieldTermStructure > termStructure_
const DayCounter fixedLegDayCounter_
const RateAveraging::Type averagingMethod_
discounting swap engine
Discretized swaption class.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Real sigma
base class for Inter-Bank-Offered-Rate indexes
Definition: any.hpp:35
STL namespace.
Overnight index swap paying compounded overnight vs. fixed.
date schedule
simple quote class
Swaption calibration helper.
Simple fixed-rate vs Libor swap.