QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
makecapfloor.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 Ferdinando Ametrano
5 Copyright (C) 2007 StatPro Italia srl
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
24
25namespace QuantLib {
26
28 const Period& tenor,
29 const ext::shared_ptr<IborIndex>& iborIndex,
30 Rate strike,
31 const Period& forwardStart)
32 : capFloorType_(capFloorType), strike_(strike), firstCapletExcluded_(forwardStart == 0 * Days),
33 // setting the fixed leg tenor avoids that MakeVanillaSwap throws
34 // because of an unknown fixed leg default tenor for a currency,
35 // notice that only the floating leg of the swap is used anyway
36 makeVanillaSwap_(MakeVanillaSwap(tenor, iborIndex, 0.0, forwardStart)
37 .withFixedLegTenor(1 * Years)
38 .withFixedLegDayCount(Actual365Fixed())) {}
39
40 MakeCapFloor::operator CapFloor() const {
41 ext::shared_ptr<CapFloor> capfloor = *this;
42 return *capfloor;
43 }
44
45 MakeCapFloor::operator ext::shared_ptr<CapFloor>() const {
46
47 VanillaSwap swap = makeVanillaSwap_;
48
49 Leg leg = swap.floatingLeg();
50 if (firstCapletExcluded_)
51 leg.erase(leg.begin());
52
53 // only leaves the last coupon
54 if (asOptionlet_ && leg.size() > 1) {
55 auto end = leg.end(); // Sun Studio needs an lvalue
56 leg.erase(leg.begin(), --end);
57 }
58
59 std::vector<Rate> strikeVector(1, strike_);
60 if (strike_ == Null<Rate>()) {
61
62 // temporary patch...
63 // should be fixed for every CapFloor::Engine
64 ext::shared_ptr<BlackCapFloorEngine> temp =
65 ext::dynamic_pointer_cast<BlackCapFloorEngine>(engine_);
66 QL_REQUIRE(temp,
67 "cannot calculate ATM without a BlackCapFloorEngine");
68 Handle<YieldTermStructure> discountCurve = temp->termStructure();
69 strikeVector[0] = CashFlows::atmRate(leg,
70 **discountCurve,
71 false,
72 discountCurve->referenceDate());
73 }
74
75 ext::shared_ptr<CapFloor> capFloor(new
76 CapFloor(capFloorType_, leg, strikeVector));
77 capFloor->setPricingEngine(engine_);
78 return capFloor;
79 }
80
83 return *this;
84 }
85
87 bool firstCapletExcluded) {
89 firstCapletExcluded_ = firstCapletExcluded;
90 return *this;
91 }
92
95 return *this;
96 }
97
98
101 return *this;
102 }
103
104
107 return *this;
108 }
109
110
114 return *this;
115 }
116
117
120 return *this;
121 }
122
125 return *this;
126 }
127
128
131 return *this;
132 }
133
136 return *this;
137 }
138
141 return *this;
142 }
143
145 asOptionlet_ = b;
146 return *this;
147 }
148
150 const ext::shared_ptr<PricingEngine>& engine) {
151 engine_ = engine;
152 return *this;
153 }
154
155}
156
Black-formula cap/floor engine.
Cash-flow analysis functions.
ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:60
Actual/365 (Fixed) day count convention.
calendar class
Definition: calendar.hpp:61
Base class for cap-like instruments.
Definition: capfloor.hpp:55
static Rate atmRate(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date(), Real npv=Null< Real >())
At-the-money rate of the cash flows.
Definition: cashflows.cpp:510
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
MakeCapFloor & withTenor(const Period &t)
MakeCapFloor & withEffectiveDate(const Date &effectiveDate, bool firstCapletExcluded)
MakeCapFloor & withNominal(Real n)
MakeCapFloor & withCalendar(const Calendar &cal)
MakeCapFloor & withDayCount(const DayCounter &dc)
MakeCapFloor & asOptionlet(bool b=true)
only get last coupon
MakeVanillaSwap makeVanillaSwap_
MakeCapFloor & withFirstDate(const Date &d)
MakeCapFloor & withEndOfMonth(bool flag=true)
ext::shared_ptr< PricingEngine > engine_
MakeCapFloor & withPricingEngine(const ext::shared_ptr< PricingEngine > &engine)
MakeCapFloor(CapFloor::Type capFloorType, const Period &capFloorTenor, const ext::shared_ptr< IborIndex > &iborIndex, Rate strike=Null< Rate >(), const Period &forwardStart=0 *Days)
MakeCapFloor & withRule(DateGeneration::Rule r)
MakeCapFloor & withConvention(BusinessDayConvention bdc)
MakeCapFloor & withTerminationDateConvention(BusinessDayConvention bdc)
MakeCapFloor & withNextToLastDate(const Date &d)
MakeVanillaSwap & withEffectiveDate(const Date &)
MakeVanillaSwap & withFloatingLegFirstDate(const Date &d)
MakeVanillaSwap & withFloatingLegTerminationDateConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withFloatingLegDayCount(const DayCounter &dc)
MakeVanillaSwap & withNominal(Real n)
MakeVanillaSwap & withFloatingLegCalendar(const Calendar &cal)
MakeVanillaSwap & withFloatingLegRule(DateGeneration::Rule r)
MakeVanillaSwap & withFloatingLegTenor(const Period &t)
MakeVanillaSwap & withFloatingLegNextToLastDate(const Date &d)
MakeVanillaSwap & withFloatingLegEndOfMonth(bool flag=true)
MakeVanillaSwap & withFloatingLegConvention(BusinessDayConvention bdc)
template class providing a null value for a given type.
Definition: null.hpp:76
Plain-vanilla swap: fix vs ibor leg.
Definition: vanillaswap.hpp:65
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
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 standard market cap/floor.
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
ext::shared_ptr< YieldTermStructure > r