QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
makevanillaswap.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) 2006, 2007, 2010, 2014, 2015 Ferdinando Ametrano
5 Copyright (C) 2006 Katiuscia Manzoni
6 Copyright (C) 2006 StatPro Italia srl
7 Copyright (C) 2015 Paolo Mazzocchi
8 Copyright (C) 2018 Matthias Groncki
9
10 This file is part of QuantLib, a free-software/open-source library
11 for financial quantitative analysts and developers - http://quantlib.org/
12
13 QuantLib is free software: you can redistribute it and/or modify it
14 under the terms of the QuantLib license. You should have received a
15 copy of the license along with this program; if not, please email
16 <quantlib-dev@lists.sf.net>. The license is also available online at
17 <http://quantlib.org/license.shtml>.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 FOR A PARTICULAR PURPOSE. See the license for more details.
22*/
23
30#include <ql/time/schedule.hpp>
35#include <ql/utilities/null.hpp>
36#include <ql/optional.hpp>
37
38namespace QuantLib {
39
41 const ext::shared_ptr<IborIndex>& index,
42 Rate fixedRate,
43 const Period& forwardStart)
44 : swapTenor_(swapTenor), iborIndex_(index), fixedRate_(fixedRate), forwardStart_(forwardStart),
45 settlementDays_(Null<Natural>()), fixedCalendar_(index->fixingCalendar()),
46 floatCalendar_(index->fixingCalendar()),
47
48 floatTenor_(index->tenor()),
49
50 floatConvention_(index->businessDayConvention()),
51 floatTerminationDateConvention_(index->businessDayConvention()),
52
53 floatDayCount_(index->dayCounter()) {}
54
55 MakeVanillaSwap::operator VanillaSwap() const {
56 ext::shared_ptr<VanillaSwap> swap = *this;
57 return *swap;
58 }
59
60 MakeVanillaSwap::operator ext::shared_ptr<VanillaSwap>() const {
61
62 Date startDate;
63 if (effectiveDate_ != Date())
64 startDate = effectiveDate_;
65 else {
67 // if the evaluation date is not a business day
68 // then move to the next business day
69 refDate = floatCalendar_.adjust(refDate);
70 // use index valueDate interface wherever possible to estimate spot date.
71 // Unless we pass an explicit settlementDays_ which does not match the index-defined number of fixing days.
72 Date spotDate;
73 if (settlementDays_ == Null<Natural>())
74 spotDate = iborIndex_->valueDate(refDate);
75 else
76 spotDate = floatCalendar_.advance(refDate, settlementDays_ * Days);
77 startDate = spotDate+forwardStart_;
78 if (forwardStart_.length()<0)
79 startDate = floatCalendar_.adjust(startDate,
80 Preceding);
81 else if (forwardStart_.length()>0)
82 startDate = floatCalendar_.adjust(startDate,
83 Following);
84 // no explicit date adjustment needed for forwardStart_.length()==0 (already handled by spotDate arithmetic above)
85 }
86
87 Date endDate = terminationDate_;
88 if (endDate == Date()) {
89 if (floatEndOfMonth_)
90 endDate = floatCalendar_.advance(startDate,
91 swapTenor_,
93 floatEndOfMonth_);
94 else
95 endDate = startDate + swapTenor_;
96 }
97
98 const Currency& curr = iborIndex_->currency();
99 Period fixedTenor;
100 if (fixedTenor_ != Period())
101 fixedTenor = fixedTenor_;
102 else {
103 if ((curr == EURCurrency()) ||
104 (curr == USDCurrency()) ||
105 (curr == CHFCurrency()) ||
106 (curr == SEKCurrency()) ||
107 (curr == GBPCurrency() && swapTenor_ <= 1 * Years))
108 fixedTenor = Period(1, Years);
109 else if ((curr == GBPCurrency() && swapTenor_ > 1 * Years) ||
110 (curr == JPYCurrency()) ||
111 (curr == AUDCurrency() && swapTenor_ >= 4 * Years))
112 fixedTenor = Period(6, Months);
113 else if ((curr == HKDCurrency() ||
114 (curr == AUDCurrency() && swapTenor_ < 4 * Years)))
115 fixedTenor = Period(3, Months);
116 else
117 QL_FAIL("unknown fixed leg default tenor for " << curr);
118 }
119
120 Schedule fixedSchedule(startDate, endDate,
121 fixedTenor, fixedCalendar_,
122 fixedConvention_,
123 fixedTerminationDateConvention_,
124 fixedRule_, fixedEndOfMonth_,
125 fixedFirstDate_, fixedNextToLastDate_);
126
127 Schedule floatSchedule(startDate, endDate,
128 floatTenor_, floatCalendar_,
129 floatConvention_,
130 floatTerminationDateConvention_,
131 floatRule_, floatEndOfMonth_,
132 floatFirstDate_, floatNextToLastDate_);
133
134 DayCounter fixedDayCount;
135 if (fixedDayCount_ != DayCounter())
136 fixedDayCount = fixedDayCount_;
137 else {
138 if (curr == USDCurrency())
139 fixedDayCount = Actual360();
140 else if (curr == EURCurrency() || curr == CHFCurrency() ||
141 curr == SEKCurrency())
142 fixedDayCount = Thirty360(Thirty360::BondBasis);
143 else if (curr == GBPCurrency() || curr == JPYCurrency() ||
144 curr == AUDCurrency() || curr == HKDCurrency() ||
145 curr == THBCurrency())
146 fixedDayCount = Actual365Fixed();
147 else
148 QL_FAIL("unknown fixed leg day counter for " << curr);
149 }
150
151 Rate usedFixedRate = fixedRate_;
152 if (fixedRate_ == Null<Rate>()) {
153 VanillaSwap temp(type_, 100.00, fixedSchedule,
154 0.0, // fixed rate
155 fixedDayCount, floatSchedule, iborIndex_, floatSpread_, floatDayCount_,
156 paymentConvention_, useIndexedCoupons_);
157 if (engine_ == nullptr) {
159 iborIndex_->forwardingTermStructure();
160 QL_REQUIRE(!disc.empty(),
161 "null term structure set to this instance of " <<
162 iborIndex_->name());
163 bool includeSettlementDateFlows = false;
164 ext::shared_ptr<PricingEngine> engine(new
165 DiscountingSwapEngine(disc, includeSettlementDateFlows));
166 temp.setPricingEngine(engine);
167 } else
169
170 usedFixedRate = temp.fairRate();
171 }
172
173 ext::shared_ptr<VanillaSwap> swap(new VanillaSwap(
174 type_, nominal_, fixedSchedule, usedFixedRate, fixedDayCount, floatSchedule, iborIndex_,
175 floatSpread_, floatDayCount_, paymentConvention_, useIndexedCoupons_));
176
177 if (engine_ == nullptr) {
179 iborIndex_->forwardingTermStructure();
180 bool includeSettlementDateFlows = false;
181 ext::shared_ptr<PricingEngine> engine(new
182 DiscountingSwapEngine(disc, includeSettlementDateFlows));
183 swap->setPricingEngine(engine);
184 } else
185 swap->setPricingEngine(engine_);
186
187 return swap;
188 }
189
191 type_ = flag ? Swap::Receiver : Swap::Payer ;
192 return *this;
193 }
194
196 type_ = type;
197 return *this;
198 }
199
201 nominal_ = n;
202 return *this;
203 }
204
206 settlementDays_ = settlementDays;
208 return *this;
209 }
210
213 effectiveDate_ = effectiveDate;
214 return *this;
215 }
216
219 terminationDate_ = terminationDate;
220 if (terminationDate != Date())
221 swapTenor_ = Period();
222 return *this;
223 }
224
226 fixedRule_ = r;
227 floatRule_ = r;
228 return *this;
229 }
230
232 paymentConvention_ = bdc;
233 return *this;
234 }
235
238 bool includeSettlementDateFlows = false;
239 engine_ = ext::shared_ptr<PricingEngine>(new
240 DiscountingSwapEngine(d, includeSettlementDateFlows));
241 return *this;
242 }
243
245 const ext::shared_ptr<PricingEngine>& engine) {
246 engine_ = engine;
247 return *this;
248 }
249
251 fixedTenor_ = t;
252 return *this;
253 }
254
257 fixedCalendar_ = cal;
258 return *this;
259 }
260
263 fixedConvention_ = bdc;
264 return *this;
265 }
266
270 return *this;
271 }
272
274 fixedRule_ = r;
275 return *this;
276 }
277
279 fixedEndOfMonth_ = flag;
280 return *this;
281 }
282
285 return *this;
286 }
287
291 return *this;
292 }
293
296 fixedDayCount_ = dc;
297 return *this;
298 }
299
301 floatTenor_ = t;
302 return *this;
303 }
304
307 floatCalendar_ = cal;
308 return *this;
309 }
310
313 floatConvention_ = bdc;
314 return *this;
315 }
316
320 return *this;
321 }
322
324 floatRule_ = r;
325 return *this;
326 }
327
329 floatEndOfMonth_ = flag;
330 return *this;
331 }
332
336 return *this;
337 }
338
342 return *this;
343 }
344
347 floatDayCount_ = dc;
348 return *this;
349 }
350
352 floatSpread_ = sp;
353 return *this;
354 }
355
358 return *this;
359 }
360
363 return *this;
364 }
365
366}
act/360 day counter
Actual/365 (Fixed) day counter.
American currencies.
Asian currencies.
ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:60
Australian dollar.
Definition: oceania.hpp:45
Actual/360 day count convention.
Definition: actual360.hpp:37
Actual/365 (Fixed) day count convention.
Swiss franc.
Definition: europe.hpp:79
calendar class
Definition: calendar.hpp:61
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
static Date advance(const Date &d, Integer units, TimeUnit)
Definition: date.cpp:139
day counter class
Definition: daycounter.hpp:44
Discounting engine for swaps.
European Euro.
Definition: europe.hpp:123
British pound sterling.
Definition: europe.hpp:134
Hong Kong dollar.
Definition: asia.hpp:68
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:188
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
Japanese yen.
Definition: asia.hpp:134
MakeVanillaSwap & receiveFixed(bool flag=true)
MakeVanillaSwap & withFixedLegFirstDate(const Date &d)
MakeVanillaSwap & withEffectiveDate(const Date &)
MakeVanillaSwap & withFixedLegConvention(BusinessDayConvention bdc)
BusinessDayConvention fixedConvention_
MakeVanillaSwap & withFixedLegTerminationDateConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withFloatingLegFirstDate(const Date &d)
MakeVanillaSwap & withFixedLegCalendar(const Calendar &cal)
MakeVanillaSwap & withFloatingLegTerminationDateConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withTerminationDate(const Date &)
MakeVanillaSwap & withAtParCoupons(bool b=true)
MakeVanillaSwap & withDiscountingTermStructure(const Handle< YieldTermStructure > &discountCurve)
DateGeneration::Rule floatRule_
MakeVanillaSwap & withIndexedCoupons(const ext::optional< bool > &b=true)
MakeVanillaSwap & withPricingEngine(const ext::shared_ptr< PricingEngine > &engine)
MakeVanillaSwap & withSettlementDays(Natural settlementDays)
MakeVanillaSwap(const Period &swapTenor, const ext::shared_ptr< IborIndex > &iborIndex, Rate fixedRate=Null< Rate >(), const Period &forwardStart=0 *Days)
MakeVanillaSwap & withFixedLegDayCount(const DayCounter &dc)
MakeVanillaSwap & withFixedLegEndOfMonth(bool flag=true)
MakeVanillaSwap & withFloatingLegDayCount(const DayCounter &dc)
ext::optional< bool > useIndexedCoupons_
BusinessDayConvention floatTerminationDateConvention_
MakeVanillaSwap & withRule(DateGeneration::Rule r)
MakeVanillaSwap & withFloatingLegSpread(Spread sp)
MakeVanillaSwap & withNominal(Real n)
MakeVanillaSwap & withFloatingLegCalendar(const Calendar &cal)
DateGeneration::Rule fixedRule_
ext::shared_ptr< PricingEngine > engine_
ext::optional< BusinessDayConvention > paymentConvention_
BusinessDayConvention fixedTerminationDateConvention_
MakeVanillaSwap & withFloatingLegRule(DateGeneration::Rule r)
MakeVanillaSwap & withFloatingLegTenor(const Period &t)
MakeVanillaSwap & withFixedLegRule(DateGeneration::Rule r)
MakeVanillaSwap & withType(Swap::Type type)
MakeVanillaSwap & withFloatingLegNextToLastDate(const Date &d)
MakeVanillaSwap & withFloatingLegEndOfMonth(bool flag=true)
MakeVanillaSwap & withFloatingLegConvention(BusinessDayConvention bdc)
BusinessDayConvention floatConvention_
MakeVanillaSwap & withPaymentConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withFixedLegNextToLastDate(const Date &d)
MakeVanillaSwap & withFixedLegTenor(const Period &t)
template class providing a null value for a given type.
Definition: null.hpp:59
Swedish krona.
Definition: europe.hpp:268
Payment schedule.
Definition: schedule.hpp:40
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
Thai baht.
Definition: asia.hpp:227
30/360 day count convention
Definition: thirty360.hpp:76
U.S. dollar.
Definition: america.hpp:162
Plain-vanilla swap: fix vs ibor leg.
Definition: vanillaswap.hpp:65
const DefaultType & t
discounting swap engine
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
European currencies.
Date d
std::function< Real(Real)> b
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
base class for Inter-Bank-Offered-Rate indexes
Helper class to instantiate standard market swaps.
Definition: any.hpp:37
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:891
null values
Oceanian currencies.
Maps optional to either the boost or std implementation.
ext::shared_ptr< YieldTermStructure > r
date schedule
30/360 day counters