Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
makeoiscapfloor.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/cashflows/cashflows.hpp>
22
23namespace QuantExt {
24
25MakeOISCapFloor::MakeOISCapFloor(CapFloor::Type type, const Period& tenor, const ext::shared_ptr<OvernightIndex>& index,
26 const Period& rateComputationPeriod, Rate strike,
27 const QuantLib::Handle<QuantLib::YieldTermStructure>& discountCurve)
28 : type_(type), tenor_(tenor), index_(index), rateComputationPeriod_(rateComputationPeriod), strike_(strike),
29 nominal_(1.0), settlementDays_(2), calendar_(index->fixingCalendar()), convention_(ModifiedFollowing),
30 rule_(DateGeneration::Backward), dayCounter_(index->dayCounter()), telescopicValueDates_(false),
31 discountCurve_(discountCurve) {}
32
33MakeOISCapFloor::operator Leg() const {
34 Calendar calendar = index_->fixingCalendar();
35
36 Date startDate;
37 if (effectiveDate_ != Date()) {
38 startDate = effectiveDate_;
39 } else {
40 Date refDate = Settings::instance().evaluationDate();
41 startDate = calendar.advance(calendar.adjust(refDate), settlementDays_ * Days);
42 }
43
44 Date endDate = calendar.adjust(startDate + tenor_, ModifiedFollowing);
45
46 Schedule schedule(startDate, endDate, rateComputationPeriod_, calendar, ModifiedFollowing, ModifiedFollowing,
47 DateGeneration::Backward, false);
48
49 // determine atm strike if required
50 Real effectiveStrike = strike_;
51 if (effectiveStrike == Null<Real>()) {
52 Leg leg = OvernightLeg(schedule, index_)
53 .withNotionals(nominal_)
54 .withPaymentDayCounter(dayCounter_)
55 .withPaymentAdjustment(convention_)
56 .withTelescopicValueDates(telescopicValueDates_);
57 effectiveStrike =
58 CashFlows::atmRate(leg, discountCurve_.empty() ? **index_->forwardingTermStructure() : **discountCurve_,
59 false, index_->forwardingTermStructure()->referenceDate());
60 }
61
62 Real cap = Null<Real>(), floor = Null<Real>();
63 if (type_ == CapFloor::Cap)
64 cap = effectiveStrike;
65 else if (type_ == CapFloor::Floor)
66 floor = effectiveStrike;
67 else {
68 QL_FAIL("MakeOISCapFloor: expected type Cap or Floor");
69 }
70
71 Leg leg = OvernightLeg(schedule, index_)
72 .withNotionals(nominal_)
73 .withPaymentDayCounter(dayCounter_)
74 .withPaymentAdjustment(convention_)
75 .withCaps(cap)
76 .withFloors(floor)
77 .withNakedOption(true)
78 .withTelescopicValueDates(telescopicValueDates_);
79
80 if (pricer_) {
81 for (auto &c : leg) {
82 auto f = QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(c);
83 if (f) {
84 f->setPricer(pricer_);
85 }
86 }
87 }
88
89 return leg;
90}
91
93 nominal_ = n;
94 return *this;
95}
96
98 effectiveDate_ = effectiveDate;
99 return *this;
100}
101
103 settlementDays_ = settlementDays;
104 return *this;
105}
106
108 calendar_ = cal;
109 return *this;
110}
111
113 convention_ = bdc;
114 return *this;
115}
116
118 rule_ = r;
119 return *this;
120}
121
123 dayCounter_ = dc;
124 return *this;
125}
126
129 return *this;
130}
131
133MakeOISCapFloor::withCouponPricer(const ext::shared_ptr<CappedFlooredOvernightIndexedCouponPricer>& pricer) {
134 pricer_ = pricer;
135 return *this;
136}
137
138Leg getOisCapFloorUnderlying(const Leg& oisCapFloor) {
139 Leg underlying;
140 for (auto const& c : oisCapFloor) {
141 auto cfon = QuantLib::ext::dynamic_pointer_cast<CappedFlooredOvernightIndexedCoupon>(c);
142 QL_REQUIRE(cfon, "getOisCapFloorUnderlying(): expected CappedFlooredOvernightIndexedCoupon");
143 underlying.push_back(cfon->underlying());
144 }
145 return underlying;
146}
147
148std::vector<std::pair<Real, Real>> getOisCapFloorStrikes(const Leg& oisCapFloor) {
149 std::vector<std::pair<Real, Real>> result;
150 for (auto const& c : oisCapFloor) {
151 auto cfon = QuantLib::ext::dynamic_pointer_cast<CappedFlooredOvernightIndexedCoupon>(c);
152 QL_REQUIRE(cfon, "getOisCapFloorUnderlying(): expected CappedFlooredOvernightIndexedCoupon");
153 result.push_back(std::make_pair(cfon->cap(), cfon->floor()));
154 }
155 return result;
156}
157
158} // namespace QuantExt
ext::shared_ptr< CappedFlooredOvernightIndexedCouponPricer > pricer_
MakeOISCapFloor & withEffectiveDate(const Date &effectiveDate)
MakeOISCapFloor & withSettlementDays(Natural settlementDays)
MakeOISCapFloor & withDayCount(const DayCounter &dc)
MakeOISCapFloor & withCouponPricer(const ext::shared_ptr< CappedFlooredOvernightIndexedCouponPricer > &pricer)
MakeOISCapFloor & withRule(DateGeneration::Rule r)
MakeOISCapFloor & withTelescopicValueDates(bool telescopicValueDates)
MakeOISCapFloor(CapFloor::Type type, const Period &tenor, const ext::shared_ptr< OvernightIndex > &index, const Period &rateComputationPeriod, Rate strike, const QuantLib::Handle< QuantLib::YieldTermStructure > &discountCurve=Handle< YieldTermStructure >())
BusinessDayConvention convention_
DateGeneration::Rule rule_
MakeOISCapFloor & withCalendar(const Calendar &cal)
MakeOISCapFloor & withConvention(BusinessDayConvention bdc)
MakeOISCapFloor & withNominal(Real n)
helper class building a sequence of overnight coupons
OvernightLeg & withFloors(Rate floor)
OvernightLeg & withTelescopicValueDates(bool telescopicValueDates)
OvernightLeg & withPaymentAdjustment(BusinessDayConvention)
OvernightLeg & withNakedOption(const bool nakedOption)
OvernightLeg & withNotionals(Real notional)
OvernightLeg & withCaps(Rate cap)
OvernightLeg & withPaymentDayCounter(const DayCounter &)
const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > pricer_
helper class to instantiate standard market OIS cap / floors
Leg getOisCapFloorUnderlying(const Leg &oisCapFloor)
get the underlying ON coupons from an OIS cf
std::vector< std::pair< Real, Real > > getOisCapFloorStrikes(const Leg &oisCapFloor)
get the (cap, floor) - strikes from an OIS cf