QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
makearithmeticaverageois.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2016 Stefano Fondi
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/experimental/averageois/makearithmeticaverageois.hpp>
21#include <ql/pricingengines/swap/discountingswapengine.hpp>
22#include <ql/indexes/iborindex.hpp>
23#include <ql/time/schedule.hpp>
24
25namespace QuantLib {
26
28 const Period& swapTenor,
29 const ext::shared_ptr<OvernightIndex>& overnightIndex,
30 Rate fixedRate,
31 const Period& forwardStart)
32 : swapTenor_(swapTenor), overnightIndex_(overnightIndex), fixedRate_(fixedRate),
33 forwardStart_(forwardStart),
34
35 calendar_(overnightIndex->fixingCalendar()),
36
37 fixedDayCount_(overnightIndex->dayCounter()) {}
38
39 MakeArithmeticAverageOIS::operator ArithmeticAverageOIS() const {
40 ext::shared_ptr<ArithmeticAverageOIS> ois = *this;
41 return *ois;
42 }
43
44 MakeArithmeticAverageOIS::operator ext::shared_ptr<ArithmeticAverageOIS>() const {
45
46 Date startDate;
47 if (effectiveDate_ != Date())
48 startDate = effectiveDate_;
49 else {
51 // if the evaluation date is not a business day
52 // then move to the next business day
53 refDate = calendar_.adjust(refDate);
54 Date spotDate = calendar_.advance(refDate,
55 settlementDays_*Days);
56 startDate = spotDate+forwardStart_;
57 if (forwardStart_.length()<0)
58 startDate = calendar_.adjust(startDate, Preceding);
59 else
60 startDate = calendar_.adjust(startDate, Following);
61 }
62
63 // OIS end of month default
64 bool usedEndOfMonth =
65 isDefaultEOM_ ? calendar_.isEndOfMonth(startDate) : endOfMonth_;
66
67 Date endDate = terminationDate_;
68 if (endDate == Date()) {
69 if (usedEndOfMonth)
70 endDate = calendar_.advance(startDate,
71 swapTenor_,
73 usedEndOfMonth);
74 else
75 endDate = startDate + swapTenor_;
76 }
77
78 Schedule fixedLegSchedule(startDate, endDate,
79 Period(fixedLegPaymentFrequency_),
80 calendar_,
83 rule_,
84 usedEndOfMonth);
85
86 Schedule overnightLegSchedule(startDate, endDate,
87 Period(overnightLegPaymentFrequency_),
88 calendar_,
91 rule_,
92 usedEndOfMonth);
93
94 Rate usedFixedRate = fixedRate_;
95 if (fixedRate_ == Null<Rate>()) {
96 ArithmeticAverageOIS temp(type_, nominal_,
97 fixedLegSchedule,
98 0.0, // fixed rate
99 fixedDayCount_,
100 overnightIndex_,
101 overnightLegSchedule,
102 overnightSpread_,
103 mrs_, vol_, byApprox_);
104 if (engine_ == nullptr) {
106 overnightIndex_->forwardingTermStructure();
107 QL_REQUIRE(!disc.empty(),
108 "null term structure set to this instance of " <<
109 overnightIndex_->name());
110 bool includeSettlementDateFlows = false;
111 ext::shared_ptr<PricingEngine> engine(new
112 DiscountingSwapEngine(disc, includeSettlementDateFlows));
113 temp.setPricingEngine(engine);
114 } else
115 temp.setPricingEngine(engine_);
116
117 usedFixedRate = temp.fairRate();
118 }
119
120 ext::shared_ptr<ArithmeticAverageOIS> ois(new
121 ArithmeticAverageOIS(type_, nominal_,
122 fixedLegSchedule,
123 usedFixedRate, fixedDayCount_,
124 overnightIndex_,
125 overnightLegSchedule,
126 overnightSpread_,
127 mrs_, vol_, byApprox_));
128
129 if (engine_ == nullptr) {
131 overnightIndex_->forwardingTermStructure();
132 bool includeSettlementDateFlows = false;
133 ext::shared_ptr<PricingEngine> engine(new
134 DiscountingSwapEngine(disc, includeSettlementDateFlows));
135 ois->setPricingEngine(engine);
136 } else
137 ois->setPricingEngine(engine_);
138
139 return ois;
140 }
141
144 return *this;
145 }
146
148 type_ = type;
149 return *this;
150 }
151
153 nominal_ = n;
154 return *this;
155 }
156
158 settlementDays_ = settlementDays;
160 return *this;
161 }
162
164 effectiveDate_ = effectiveDate;
165 return *this;
166 }
167
169 terminationDate_ = terminationDate;
170 swapTenor_ = Period();
171 return *this;
172 }
173
178 return *this;
179 }
180
185 return *this;
186 }
187
189 rule_ = r;
190 if (r==DateGeneration::Zero) {
193 }
194 return *this;
195 }
196
199 bool includeSettlementDateFlows = false;
200 engine_ = ext::shared_ptr<PricingEngine>(new
201 DiscountingSwapEngine(d, includeSettlementDateFlows));
202 return *this;
203 }
204
206 const ext::shared_ptr<PricingEngine>& engine) {
207 engine_ = engine;
208 return *this;
209 }
210
212 fixedDayCount_ = dc;
213 return *this;
214 }
215
217 endOfMonth_ = flag;
218 isDefaultEOM_ = false;
219 return *this;
220 }
221
223 overnightSpread_ = sp;
224 return *this;
225 }
226
228 Real meanReversionSpeed,
229 Real volatility,
230 bool byApprox) {
231 mrs_ = meanReversionSpeed;
232 vol_ = volatility;
233 byApprox_ = byApprox;
234 return *this;
235 }
236
237}
Arithemtic Average OIS: fix vs arithmetic average of overnight rate.
Concrete date class.
Definition: date.hpp:125
static bool isEndOfMonth(const Date &d)
whether a date is the last day of its month
Definition: date.hpp:434
static Date advance(const Date &d, Integer units, TimeUnit)
Definition: date.cpp:139
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
MakeArithmeticAverageOIS & withOvernightLegSpread(Spread sp)
MakeArithmeticAverageOIS & withType(Swap::Type type)
MakeArithmeticAverageOIS & withEndOfMonth(bool flag=true)
MakeArithmeticAverageOIS & withFixedLegPaymentFrequency(Frequency f)
MakeArithmeticAverageOIS & withEffectiveDate(const Date &)
MakeArithmeticAverageOIS & withArithmeticAverage(Real meanReversionSpeed=0.03, Real volatility=0.00, bool byApprox=false)
MakeArithmeticAverageOIS & withDiscountingTermStructure(const Handle< YieldTermStructure > &discountingTermStructure)
MakeArithmeticAverageOIS & withSettlementDays(Natural settlementDays)
MakeArithmeticAverageOIS & withFixedLegDayCount(const DayCounter &dc)
MakeArithmeticAverageOIS & receiveFixed(bool flag=true)
MakeArithmeticAverageOIS & withOvernightLegPaymentFrequency(Frequency f)
ext::shared_ptr< PricingEngine > engine_
MakeArithmeticAverageOIS & withPricingEngine(const ext::shared_ptr< PricingEngine > &engine)
MakeArithmeticAverageOIS & withNominal(Real n)
MakeArithmeticAverageOIS(const Period &swapTenor, const ext::shared_ptr< OvernightIndex > &overnightIndex, Rate fixedRate=Null< Rate >(), const Period &fwdStart=0 *Days)
MakeArithmeticAverageOIS & withRule(DateGeneration::Rule r)
MakeArithmeticAverageOIS & withTerminationDate(const Date &)
template class providing a null value for a given type.
Definition: null.hpp:76
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
Frequency
Frequency of events.
Definition: frequency.hpp:37
@ Once
only once, e.g., a zero-coupon
Definition: frequency.hpp:38
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
Definition: any.hpp:35