QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
makeswaption.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) 2007, 2008, 2014 Ferdinando Ametrano
5 Copyright (C) 2007 Giorgio Facchinetti
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
22#include <ql/exercise.hpp>
28#include <ql/optional.hpp>
29#include <ql/settings.hpp>
30#include <utility>
31
32namespace QuantLib {
33
34 MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
35 const Period& optionTenor,
36 Rate strike)
37 : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical),
38 settlementMethod_(Settlement::PhysicalOTC), optionTenor_(optionTenor),
39 optionConvention_(ModifiedFollowing), fixingDate_(Null<Date>()), strike_(strike),
40 underlyingType_(Swap::Payer), nominal_(1.0) {}
41
42 MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
43 const Date& fixingDate,
44 Rate strike)
45 : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical),
46 settlementMethod_(Settlement::PhysicalOTC), optionConvention_(ModifiedFollowing),
47 fixingDate_(fixingDate), strike_(strike), underlyingType_(Swap::Payer) {}
48
49 MakeSwaption::operator Swaption() const {
50 ext::shared_ptr<Swaption> swaption = *this;
51 return *swaption;
52 }
53
54 MakeSwaption::operator ext::shared_ptr<Swaption>() const {
55
56 const Calendar& fixingCalendar = swapIndex_->fixingCalendar();
58 // if the evaluation date is not a business day
59 // then move to the next business day
60 refDate = fixingCalendar.adjust(refDate);
61 if (fixingDate_ == Null<Date>())
62 fixingDate_ = fixingCalendar.advance(refDate, optionTenor_,
63 optionConvention_);
64 if (exerciseDate_ == Null<Date>()) {
65 exercise_ = ext::shared_ptr<Exercise>(new
66 EuropeanExercise(fixingDate_));
67 } else {
68 QL_REQUIRE(exerciseDate_ <= fixingDate_,
69 "exercise date (" << exerciseDate_ << ") must be less "
70 "than or equal to fixing date (" << fixingDate_ << ")");
71 exercise_ = ext::shared_ptr<Exercise>(new
72 EuropeanExercise(exerciseDate_));
73 }
74
75 Rate usedStrike;
76 ext::shared_ptr<OvernightIndexedSwapIndex> OIswap_index = ext::dynamic_pointer_cast<OvernightIndexedSwapIndex>(swapIndex_);
77 if (strike_ == Null<Rate>()) {
78 // ATM on curve(s) attached to index
79 QL_REQUIRE(!swapIndex_->forwardingTermStructure().empty(),
80 "null term structure set to this instance of " <<
81 swapIndex_->name());
82 if (OIswap_index) {
83 auto temp = OIswap_index->underlyingSwap(fixingDate_);
84 temp->setPricingEngine(
85 ext::make_shared<DiscountingSwapEngine>(
86 swapIndex_->exogenousDiscount()
87 ? swapIndex_->discountingTermStructure()
88 : swapIndex_->forwardingTermStructure(),
89 false
90 )
91 );
92 usedStrike = temp->fairRate();
93 } else {
94 auto temp = swapIndex_->underlyingSwap(fixingDate_);
95 temp->setPricingEngine(
96 ext::make_shared<DiscountingSwapEngine>(
97 swapIndex_->exogenousDiscount()
98 ? swapIndex_->discountingTermStructure()
99 : swapIndex_->forwardingTermStructure(),
100 false
101 )
102 );
103 usedStrike = temp->fairRate();
104 }
105 } else {
106 usedStrike = strike_;
107 }
108
109 BusinessDayConvention bdc = swapIndex_->fixedLegConvention();
110 if (OIswap_index) {
111 underlyingSwap_ =
112 (ext::shared_ptr<OvernightIndexedSwap>)(
113 MakeOIS(swapIndex_->tenor(),
114 OIswap_index->overnightIndex(), usedStrike)
115 .withEffectiveDate(swapIndex_->valueDate(fixingDate_))
116 .withPaymentCalendar(swapIndex_->fixingCalendar())
117 .withFixedLegDayCount(swapIndex_->dayCounter())
121 .withType(underlyingType_)
122 .withNominal(nominal_)
123 );
124 } else {
125 underlyingSwap_ =
126 (ext::shared_ptr<VanillaSwap>)(
127 MakeVanillaSwap(swapIndex_->tenor(),
128 swapIndex_->iborIndex(), usedStrike)
129 .withEffectiveDate(swapIndex_->valueDate(fixingDate_))
130 .withFixedLegCalendar(swapIndex_->fixingCalendar())
131 .withFixedLegDayCount(swapIndex_->dayCounter())
132 .withFixedLegTenor(swapIndex_->fixedLegTenor())
133 .withFixedLegConvention(bdc)
135 .withType(underlyingType_)
136 .withNominal(nominal_)
137 .withIndexedCoupons(useIndexedCoupons_)
138 );
139 }
140 ext::shared_ptr<Swaption> swaption = ext::make_shared<Swaption>(
141 underlyingSwap_, exercise_, delivery_, settlementMethod_);
142 swaption->setPricingEngine(engine_);
143 return swaption;
144 }
145
147 delivery_ = delivery;
148 return *this;
149 }
150
152 Settlement::Method settlementMethod) {
153 settlementMethod_ = settlementMethod;
154 return *this;
155 }
156
159 optionConvention_ = bdc;
160 return *this;
161 }
162
164 exerciseDate_ = date;
165 return *this;
166 }
167
169 underlyingType_ = type;
170 return *this;
171 }
172
174 const ext::shared_ptr<PricingEngine>& engine) {
175 engine_ = engine;
176 return *this;
177 }
178
180 nominal_ = n;
181 return *this;
182 }
183
184 MakeSwaption& MakeSwaption::withIndexedCoupons(const ext::optional<bool>& b) {
186 return *this;
187 }
188
191 return *this;
192 }
193
194}
Cash-flow analysis functions.
ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:60
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
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
European exercise.
Definition: exercise.hpp:96
helper class
Definition: makeois.hpp:39
MakeOIS & withPaymentAdjustment(BusinessDayConvention convention)
Definition: makeois.cpp:212
MakeOIS & withType(Swap::Type type)
Definition: makeois.cpp:171
MakeOIS & withFixedLegDayCount(const DayCounter &dc)
Definition: makeois.cpp:269
MakeOIS & withFixedLegConvention(BusinessDayConvention bdc)
Definition: makeois.cpp:278
MakeOIS & withFixedLegTerminationDateConvention(BusinessDayConvention bdc)
Definition: makeois.cpp:293
MakeOIS & withPaymentCalendar(const Calendar &cal)
Definition: makeois.cpp:222
MakeOIS & withEffectiveDate(const Date &)
Definition: makeois.cpp:187
MakeOIS & withNominal(Real n)
Definition: makeois.cpp:176
BusinessDayConvention optionConvention_
MakeSwaption & withOptionConvention(BusinessDayConvention bdc)
MakeSwaption & withExerciseDate(const Date &)
ext::optional< bool > useIndexedCoupons_
MakeSwaption & withAtParCoupons(bool b=true)
MakeSwaption & withIndexedCoupons(const ext::optional< bool > &b=true)
MakeSwaption & withNominal(Real n)
Settlement::Type delivery_
ext::shared_ptr< PricingEngine > engine_
MakeSwaption & withSettlementMethod(Settlement::Method settlementMethod)
MakeSwaption & withPricingEngine(const ext::shared_ptr< PricingEngine > &engine)
Settlement::Method settlementMethod_
MakeSwaption & withSettlementType(Settlement::Type delivery)
MakeSwaption(ext::shared_ptr< SwapIndex > swapIndex, const Period &optionTenor, Rate strike=Null< Rate >())
MakeSwaption & withUnderlyingType(Swap::Type type)
MakeVanillaSwap & withFixedLegTerminationDateConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withIndexedCoupons(const ext::optional< bool > &b=true)
MakeVanillaSwap & withNominal(Real n)
MakeVanillaSwap & withType(Swap::Type type)
template class providing a null value for a given type.
Definition: null.hpp:76
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Interest rate swap.
Definition: swap.hpp:41
Swaption class
Definition: swaption.hpp:88
discounting swap engine
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
ext::function< Real(Real)> b
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
Helper class to instantiate overnight indexed swaps.
Helper class to instantiate standard market swaption.
Helper class to instantiate standard market swaps.
Definition: any.hpp:35
STL namespace.
Maps optional to either the boost or std implementation.
const ParametricExercise & exercise_
global repository for run-time library settings
settlement information
Definition: swaption.hpp:41
swap-rate indexes