Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
subperiodsswaphelper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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
19#include <ql/cashflows/iborcoupon.hpp>
20#include <ql/pricingengines/swap/discountingswapengine.hpp>
21#include <ql/utilities/null_deleter.hpp>
23
24namespace QuantExt {
25
26SubPeriodsSwapHelper::SubPeriodsSwapHelper(Handle<Quote> spread, const Period& swapTenor, const Period& fixedTenor,
27 const Calendar& fixedCalendar, const DayCounter& fixedDayCount,
28 BusinessDayConvention fixedConvention, const Period& floatPayTenor,
29 const QuantLib::ext::shared_ptr<IborIndex>& iborIndex,
30 const DayCounter& floatDayCount,
31 const Handle<YieldTermStructure>& discountingCurve,
33 : RelativeDateRateHelper(spread), iborIndex_(iborIndex), swapTenor_(swapTenor), fixedTenor_(fixedTenor),
34 fixedCalendar_(fixedCalendar), fixedDayCount_(fixedDayCount), fixedConvention_(fixedConvention),
35 floatPayTenor_(floatPayTenor), floatDayCount_(floatDayCount), type_(type), discountHandle_(discountingCurve) {
36
38 iborIndex_->unregisterWith(termStructureHandle_);
39
40 registerWith(iborIndex_);
41 registerWith(spread);
42 registerWith(discountHandle_);
43
45}
46
48
49 // build swap
50 Date valuationDate = Settings::instance().evaluationDate();
51 Calendar spotCalendar = iborIndex_->fixingCalendar();
52 Natural spotDays = iborIndex_->fixingDays();
53 // move val date forward in case it is a holiday
54 valuationDate = spotCalendar.adjust(valuationDate);
55 Date effectiveDate = spotCalendar.advance(valuationDate, spotDays * Days);
56
57 swap_ = QuantLib::ext::shared_ptr<SubPeriodsSwap>(new SubPeriodsSwap(
58 effectiveDate, 1.0, swapTenor_, true, fixedTenor_, 0.0, fixedCalendar_, fixedDayCount_, fixedConvention_,
59 floatPayTenor_, iborIndex_, floatDayCount_, DateGeneration::Backward, type_));
60
61 QuantLib::ext::shared_ptr<PricingEngine> engine(new DiscountingSwapEngine(discountRelinkableHandle_));
62 swap_->setPricingEngine(engine);
63
64 // set earliest and latest
65 earliestDate_ = swap_->startDate();
66 latestDate_ = swap_->maturityDate();
67
68 QuantLib::ext::shared_ptr<FloatingRateCoupon> lastFloating =
69 QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(swap_->floatLeg().back());
70 if (IborCoupon::Settings::instance().usingAtParCoupons()) {
71 /* Subperiods coupons do not have a par approximation either... */
72 if (QuantLib::ext::dynamic_pointer_cast<QuantExt::SubPeriodsCoupon1>(lastFloating)) {
73 Date fixingValueDate = iborIndex_->valueDate(lastFloating->fixingDate());
74 Date endValueDate = iborIndex_->maturityDate(fixingValueDate);
75 latestDate_ = std::max(latestDate_, endValueDate);
76 }
77 } else {
78 /* May need to adjust latestDate_ if you are projecting libor based
79 on tenor length rather than from accrual date to accrual date. */
80 Date fixingValueDate = iborIndex_->valueDate(lastFloating->fixingDate());
81 Date endValueDate = iborIndex_->maturityDate(fixingValueDate);
82 latestDate_ = std::max(latestDate_, endValueDate);
83 }
84}
85
86void SubPeriodsSwapHelper::setTermStructure(YieldTermStructure* t) {
87
88 bool observer = false;
89
90 QuantLib::ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
91 termStructureHandle_.linkTo(temp, observer);
92
93 if (discountHandle_.empty())
94 discountRelinkableHandle_.linkTo(temp, observer);
95 else
97
98 RelativeDateRateHelper::setTermStructure(t);
99}
100
102 QL_REQUIRE(termStructure_ != 0, "Termstructure not set");
103 swap_->deepUpdate();
104 return swap_->fairRate();
105}
106
107void SubPeriodsSwapHelper::accept(AcyclicVisitor& v) {
108 Visitor<SubPeriodsSwapHelper>* v1 = dynamic_cast<Visitor<SubPeriodsSwapHelper>*>(&v);
109 if (v1 != 0)
110 v1->visit(*this);
111 else
112 RateHelper::accept(v);
113}
114} // namespace QuantExt
RelinkableHandle< YieldTermStructure > discountRelinkableHandle_
void setTermStructure(YieldTermStructure *) override
BusinessDayConvention fixedConvention_
RelinkableHandle< YieldTermStructure > termStructureHandle_
SubPeriodsSwapHelper(Handle< Quote > spread, const Period &swapTenor, const Period &fixedTenor, const Calendar &fixedCalendar, const DayCounter &fixedDayCount, BusinessDayConvention fixedConvention, const Period &floatPayTenor, const QuantLib::ext::shared_ptr< IborIndex > &iborIndex, const DayCounter &floatDayCount, const Handle< YieldTermStructure > &discountingCurve=Handle< YieldTermStructure >(), QuantExt::SubPeriodsCoupon1::Type type=QuantExt::SubPeriodsCoupon1::Compounding)
Handle< YieldTermStructure > discountHandle_
void accept(AcyclicVisitor &) override
QuantLib::ext::shared_ptr< SubPeriodsSwap > swap_
QuantExt::SubPeriodsCoupon1::Type type_
QuantLib::ext::shared_ptr< IborIndex > iborIndex_
Single currency sub periods swap.
Single currency sub periods swap helper.