Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
durationadjustedcmslegbuilder.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
23
25
28
29#include <ql/experimental/coupons/strippedcapflooredcoupon.hpp>
30
31using namespace QuantExt;
32using namespace QuantLib;
33
34namespace ore {
35namespace data {
36
37Leg DurationAdjustedCmsLegBuilder::buildLeg(const LegData& data, const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory,
38 RequiredFixings& requiredFixings, const string& configuration,
39 const QuantLib::Date& openEndDateReplacement,
40 const bool useXbsCurves) const {
41
42 auto cmsData = QuantLib::ext::dynamic_pointer_cast<DurationAdjustedCmsLegData>(data.concreteLegData());
43 QL_REQUIRE(cmsData, "Wrong LegType, expected CMS");
44
45 string indexName = cmsData->swapIndex();
46 auto index = *engineFactory->market()->swapIndex(indexName, configuration);
47 Schedule schedule = makeSchedule(data.schedule(), openEndDateReplacement);
48 DayCounter dc = parseDayCounter(data.dayCounter());
49 BusinessDayConvention bdc = parseBusinessDayConvention(data.paymentConvention());
50
51 vector<double> spreads =
52 ore::data::buildScheduledVectorNormalised(cmsData->spreads(), cmsData->spreadDates(), schedule, 0.0);
53 vector<double> gearings =
54 ore::data::buildScheduledVectorNormalised(cmsData->gearings(), cmsData->gearingDates(), schedule, 1.0);
55 vector<double> notionals = buildScheduledVector(data.notionals(), data.notionalDates(), schedule);
56 Size fixingDays = cmsData->fixingDays() == Null<Size>() ? index->fixingDays() : cmsData->fixingDays();
57
58 applyAmortization(notionals, data, schedule, false);
59
60 PaymentLag paymentLag = parsePaymentLag(data.paymentLag());
61 DurationAdjustedCmsLeg leg = DurationAdjustedCmsLeg(schedule, index, cmsData->duration())
62 .withNotionals(notionals)
63 .withSpreads(spreads)
64 .withGearings(gearings)
67 .withPaymentLag(boost::apply_visitor(PaymentLagInteger(), paymentLag))
68 .withFixingDays(fixingDays)
69 .inArrears(cmsData->isInArrears());
70
71 if (!data.paymentCalendar().empty())
72 leg.withPaymentCalendar(parseCalendar(data.paymentCalendar()));
73
74 if (cmsData->caps().size() > 0)
75 leg.withCaps(buildScheduledVector(cmsData->caps(), cmsData->capDates(), schedule));
76
77 if (cmsData->floors().size() > 0)
78 leg.withFloors(buildScheduledVector(cmsData->floors(), cmsData->floorDates(), schedule));
79
80 // Get a coupon pricer for the leg
81 auto builder = QuantLib::ext::dynamic_pointer_cast<DurationAdjustedCmsCouponPricerBuilder>(
82 engineFactory->builder("DurationAdjustedCMS"));
83 QL_REQUIRE(builder != nullptr, "No builder found for DurationAdjustedCmsLeg");
84 auto couponPricer = builder->engine(IndexNameTranslator::instance().oreName(index->iborIndex()->name()));
85
86 // Loop over the coupons in the leg and set pricer
87 Leg result = leg;
88 for (auto& c : result) {
89 auto f = QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(c);
90 QL_REQUIRE(f != nullptr,
91 "DurationAdjustedCmsLegBuilder::buildLeg(): internal error, expected FloatingRateCoupon");
92 f->setPricer(couponPricer);
93 }
94
95 // build naked option leg if required
96 if (cmsData->nakedOption()) {
97 result = StrippedCappedFlooredCouponLeg(result);
98 }
99
100 applyIndexing(result, data, engineFactory, requiredFixings, openEndDateReplacement, useXbsCurves);
101 addToRequiredFixings(result, QuantLib::ext::make_shared<FixingDateGetter>(requiredFixings));
102 return result;
103}
104} // namespace data
105} // namespace ore
DurationAdjustedCmsLeg & withPaymentCalendar(const Calendar &)
DurationAdjustedCmsLeg & withPaymentAdjustment(BusinessDayConvention)
DurationAdjustedCmsLeg & withSpreads(Spread spread)
DurationAdjustedCmsLeg & withFixingDays(Natural fixingDays)
DurationAdjustedCmsLeg & withPaymentLag(Natural lag)
DurationAdjustedCmsLeg & withFloors(Rate floor)
DurationAdjustedCmsLeg & withNotionals(Real notional)
DurationAdjustedCmsLeg & withGearings(Real gearing)
DurationAdjustedCmsLeg & withPaymentDayCounter(const DayCounter &)
DurationAdjustedCmsLeg & inArrears(bool flag=true)
DurationAdjustedCmsLeg & withCaps(Rate cap)
QuantLib::Leg buildLeg(const ore::data::LegData &data, const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, ore::data::RequiredFixings &requiredFixings, const std::string &configuration, const QuantLib::Date &openEndDateReplacement=Null< Date >(), const bool useXbsCurves=false) const override
Serializable object holding leg data.
Definition: legdata.hpp:844
coupon pricer builder for duration adjusted cms coupons
leg builder for duration adjusted cms coupon legs
leg data for duration adjusted cms
Logic for calculating required fixing dates on legs.
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
PaymentLag parsePaymentLag(const string &s)
Convert text to PaymentLag.
Definition: parsers.cpp:628
DayCounter parseDayCounter(const string &s)
Convert text to QuantLib::DayCounter.
Definition: parsers.cpp:209
translates between QuantLib::Index::name() and ORE names
leg data model and serialization
@ data
Definition: log.hpp:77
vector< T > buildScheduledVectorNormalised(const vector< T > &values, const vector< string > &dates, const Schedule &schedule, const T &defaultValue, const bool checkAllValuesAppearInResult=false)
Definition: legdata.hpp:1139
void applyAmortization(std::vector< Real > &notionals, const LegData &data, const Schedule &schedule, const bool annuityAllowed, const std::vector< Real > &rates)
Definition: legdata.cpp:2593
void applyIndexing(Leg &leg, const LegData &data, const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, RequiredFixings &requiredFixings, const QuantLib::Date &openEndDateReplacement, const bool useXbsCurves)
Definition: legdata.cpp:2633
void addToRequiredFixings(const QuantLib::Leg &leg, const QuantLib::ext::shared_ptr< FixingDateGetter > &fixingDateGetter)
vector< T > buildScheduledVector(const vector< T > &values, const vector< string > &dates, const Schedule &schedule, const bool checkAllValuesAppearInResult=false)
Definition: legdata.hpp:1061
boost::variant< QuantLib::Period, QuantLib::Natural > PaymentLag
Definition: types.hpp:32
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
Serializable Credit Default Swap.
Definition: namespaces.docs:23