QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
irregularswap.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
6 Copyright (C) 2007 Ferdinando Ametrano
7 Copyright (C) 2010 Andre Miemiec
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/experimental/swaptions/irregularswap.hpp>
24#include <ql/cashflows/fixedratecoupon.hpp>
25#include <ql/cashflows/iborcoupon.hpp>
26#include <ql/cashflows/cashflowvectors.hpp>
27#include <ql/cashflows/cashflows.hpp>
28#include <ql/cashflows/couponpricer.hpp>
29#include <ql/indexes/iborindex.hpp>
30#include <ql/termstructures/yieldtermstructure.hpp>
31
32namespace QuantLib {
33
35 Type type,
36 const Leg& fixLeg,
37 const Leg& floatLeg)
38 : Swap(2), type_(type) {
39
40
41 switch (type_) {
42 case Payer:
43 payer_[0] = -1.0;
44 payer_[1] = +1.0;
45 break;
46 case Receiver:
47 payer_[0] = +1.0;
48 payer_[1] = -1.0;
49 break;
50 default:
51 QL_FAIL("Unknown Irregular-swap type");
52 }
53
54 //Fixed leg
55 legs_[0] = fixLeg;
56
57 for (Leg::const_iterator i = legs_[0].begin(); i < legs_[0].end(); ++i)
58 registerWith(*i);
59
60 //Floating Leg
61 legs_[1] = floatLeg;
62
63 for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i)
64 registerWith(*i);
65
66 }
67
68
70
71 //Debug: to be done
72
74
75 auto* arguments = dynamic_cast<IrregularSwap::arguments*>(args);
76
77 if (arguments == nullptr) // it's a swap engine...
78 return;
79
81 //arguments->nominal = nominal_;
82
83
84 const Leg& fixedCoupons = fixedLeg();
85
86 arguments->fixedResetDates = arguments->fixedPayDates = std::vector<Date>(fixedCoupons.size());
87 arguments->fixedNominals = arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
88
89
90 for (Size i=0; i<fixedCoupons.size(); ++i) {
91 ext::shared_ptr<FixedRateCoupon> coupon =
92 ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
93
94 arguments->fixedPayDates[i] = coupon->date();
95 arguments->fixedResetDates[i] = coupon->accrualStartDate();
96 arguments->fixedCoupons[i] = coupon->amount();
97 arguments->fixedNominals[i] = coupon->nominal();
98 }
99
100
101 const Leg& floatingCoupons = floatingLeg();
102
104 = arguments->floatingFixingDates = std::vector<Date>(floatingCoupons.size());
105 arguments->floatingAccrualTimes = std::vector<Time>(floatingCoupons.size());
106 arguments->floatingSpreads = std::vector<Spread>(floatingCoupons.size());
107 arguments->floatingNominals = arguments->floatingCoupons = std::vector<Real>(floatingCoupons.size());
108
109 for (Size i=0; i<floatingCoupons.size(); ++i) {
110 ext::shared_ptr<IborCoupon> coupon =
111 ext::dynamic_pointer_cast<IborCoupon>(floatingCoupons[i]);
112
113 arguments->floatingResetDates[i] = coupon->accrualStartDate();
114 arguments->floatingPayDates[i] = coupon->date();
115
116 arguments->floatingFixingDates[i] = coupon->fixingDate();
117 arguments->floatingAccrualTimes[i] = coupon->accrualPeriod();
118 arguments->floatingSpreads[i] = coupon->spread();
119 arguments->floatingNominals[i] = coupon->nominal();
120
121 try {
122 arguments->floatingCoupons[i] = coupon->amount();
123 } catch (Error&) {
125 }
126 }
127
128 }
129
130
132 calculate();
133 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
134 return fairRate_;
135 }
136
138 calculate();
139 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
140 return fairSpread_;
141 }
142
144 calculate();
145 QL_REQUIRE(legBPS_[0] != Null<Real>(), "result not available");
146 return legBPS_[0];
147 }
148
150 calculate();
151 QL_REQUIRE(legBPS_[1] != Null<Real>(), "result not available");
152 return legBPS_[1];
153 }
154
156 calculate();
157 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
158 return legNPV_[0];
159 }
160
162 calculate();
163 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
164 return legNPV_[1];
165 }
166
169 legBPS_[0] = legBPS_[1] = 0.0;
172 }
173
176
177 const auto* results = dynamic_cast<const IrregularSwap::results*>(r);
178 if (results != nullptr) { // might be a swap engine, so no error is thrown
181 } else {
184 }
185
186 if (fairRate_ == Null<Rate>()) {
187 // calculate it from other results
188 if (legBPS_[0] != Null<Real>())
189 fairRate_ = 0.0; // Debug: legs_[0]->fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
190 }
191 if (fairSpread_ == Null<Spread>()) {
192 // ditto
193 if (legBPS_[1] != Null<Real>())
194 fairSpread_ = 0.0; //DEBUG: spread_ - NPV_/(legBPS_[1]/basisPoint);
195 }
196 }
197
199
201
202 QL_REQUIRE(fixedResetDates.size() == fixedPayDates.size(),
203 "number of fixed start dates different from "
204 "number of fixed payment dates");
205 QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
206 "number of fixed payment dates different from "
207 "number of fixed coupon amounts");
208 QL_REQUIRE(floatingResetDates.size() == floatingPayDates.size(),
209 "number of floating start dates different from "
210 "number of floating payment dates");
211 QL_REQUIRE(floatingFixingDates.size() == floatingPayDates.size(),
212 "number of floating fixing dates different from "
213 "number of floating payment dates");
214 QL_REQUIRE(floatingAccrualTimes.size() == floatingPayDates.size(),
215 "number of floating accrual Times different from "
216 "number of floating payment dates");
217 QL_REQUIRE(floatingSpreads.size() == floatingPayDates.size(),
218 "number of floating spreads different from "
219 "number of floating payment dates");
220 QL_REQUIRE(floatingPayDates.size() == floatingCoupons.size(),
221 "number of floating payment dates different from "
222 "number of floating coupon amounts");
223 }
224
229 }
230
231}
Base error class.
Definition: errors.hpp:39
void calculate() const override
Definition: instrument.hpp:129
Arguments for irregular-swap calculation
std::vector< Date > floatingResetDates
std::vector< Real > floatingNominals
std::vector< Spread > floatingSpreads
std::vector< Date > floatingFixingDates
std::vector< Time > floatingAccrualTimes
std::vector< Date > floatingPayDates
Results from irregular-swap calculation
const Leg & floatingLeg() const
IrregularSwap(Type type, const Leg &fixLeg, const Leg &floatLeg)
void setupArguments(PricingEngine::arguments *args) const override
const Leg & fixedLeg() const
void setupExpired() const override
void fetchResults(const PricingEngine::results *) const override
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
void validate() const override
Definition: swap.cpp:171
void reset() override
Definition: swap.cpp:176
Interest rate swap.
Definition: swap.hpp:41
void setupArguments(PricingEngine::arguments *) const override
Definition: swap.cpp:87
std::vector< Leg > legs_
Definition: swap.hpp:133
std::vector< Real > legNPV_
Definition: swap.hpp:135
std::vector< Real > legBPS_
Definition: swap.hpp:136
void setupExpired() const override
Definition: swap.cpp:78
void fetchResults(const PricingEngine::results *) const override
Definition: swap.cpp:95
std::vector< Real > payer_
Definition: swap.hpp:134
QL_REAL Real
real number
Definition: types.hpp:50
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78