Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supply MakeVanillaSwap builder to SwapRateHelper #1932

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ql/instruments/makevanillaswap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace QuantLib {
ext::optional<BusinessDayConvention> paymentConvention_;

ext::shared_ptr<PricingEngine> engine_;

friend class SwapRateHelper;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added to enable access within SwapRateHelper::SwapRateHelper(const MakeVanillaSwap& swapBuilder) to swapBuilder.forwardStart_.

};

}
Expand Down
64 changes: 46 additions & 18 deletions ql/termstructures/yield/ratehelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ namespace QuantLib {
fixedConvention_(fixedConvention), fixedFrequency_(fixedFrequency),
fixedDayCount_(std::move(fixedDayCount)), spread_(std::move(spread)), endOfMonth_(endOfMonth),
fwdStart_(fwdStart), discountHandle_(std::move(discount)),
useIndexedCoupons_(useIndexedCoupons) {
useIndexedCoupons_(useIndexedCoupons), swapBuiltViaGivenBuilder(false) {

// take fixing into account
iborIndex_ = iborIndex->clone(termStructureHandle_);
Expand Down Expand Up @@ -600,24 +600,49 @@ namespace QuantLib {
std::move(fixedDayCount), iborIndex, std::move(spread), fwdStart, std::move(discount), settlementDays,
pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {}

void SwapRateHelper::initializeDates() {
SwapRateHelper::SwapRateHelper(const Handle<Quote>& rate,
const MakeVanillaSwap& swapBuilder,
Handle<Quote> spread,
Pillar::Choice pillar,
Date customPillarDate)
: RelativeDateRateHelper(rate), pillarChoice_(pillar), spread_(std::move(spread)),
fwdStart_(swapBuilder.forwardStart_), swapBuiltViaGivenBuilder(true) {
registerWith(spread_);

// 1. do not pass the spread here, as it might be a Quote
// i.e. it can dynamically change
// 2. input discount curve Handle might be empty now but it could
// be assigned a curve later; use a RelinkableHandle here
swap_ = MakeVanillaSwap(tenor_, iborIndex_, 0.0, fwdStart_)
.withSettlementDays(settlementDays_)
.withDiscountingTermStructure(discountRelinkableHandle_)
.withFixedLegDayCount(fixedDayCount_)
.withFixedLegTenor(Period(fixedFrequency_))
.withFixedLegConvention(fixedConvention_)
.withFixedLegTerminationDateConvention(fixedConvention_)
.withFixedLegCalendar(calendar_)
.withFixedLegEndOfMonth(endOfMonth_)
.withFloatingLegCalendar(calendar_)
.withFloatingLegEndOfMonth(endOfMonth_)
.withIndexedCoupons(useIndexedCoupons_);
pillarDate_ = customPillarDate;
swap_ = swapBuilder;
SwapRateHelper::initializeDates();
}

SwapRateHelper::SwapRateHelper(Rate rate,
const MakeVanillaSwap& swapBuilder,
Handle<Quote> spread,
Pillar::Choice pillar,
Date customPillarDate)
: SwapRateHelper(makeQuoteHandle(rate),
swapBuilder,
std::move(spread),
pillar,
customPillarDate) {}

void SwapRateHelper::initializeDates() {
if (!swapBuiltViaGivenBuilder)
// 1. do not pass the spread here, as it might be a Quote
// i.e. it can dynamically change
// 2. input discount curve Handle might be empty now but it could
// be assigned a curve later; use a RelinkableHandle here
swap_ = MakeVanillaSwap(tenor_, iborIndex_, 0.0, fwdStart_)
.withSettlementDays(settlementDays_)
.withDiscountingTermStructure(discountRelinkableHandle_)
.withFixedLegDayCount(fixedDayCount_)
.withFixedLegTenor(Period(fixedFrequency_))
.withFixedLegConvention(fixedConvention_)
.withFixedLegTerminationDateConvention(fixedConvention_)
.withFixedLegCalendar(calendar_)
.withFixedLegEndOfMonth(endOfMonth_)
.withFloatingLegCalendar(calendar_)
.withFloatingLegEndOfMonth(endOfMonth_)
.withIndexedCoupons(useIndexedCoupons_);

simplifyNotificationGraph(*swap_, true);

Expand Down Expand Up @@ -655,6 +680,9 @@ namespace QuantLib {
}

void SwapRateHelper::setTermStructure(YieldTermStructure* t) {
if (swapBuiltViaGivenBuilder)
return;
Comment on lines 682 to +684
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not see a straight forward way to access the term structure when the MakeVanillaSwap builder was given in the ctor. Any ideas on how we should handle this member function then?


// do not set the relinkable handle as an observer -
// force recalculation when needed
bool observer = false;
Expand Down
12 changes: 12 additions & 0 deletions ql/termstructures/yield/ratehelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <ql/termstructures/bootstraphelper.hpp>
#include <ql/instruments/vanillaswap.hpp>
#include <ql/instruments/makevanillaswap.hpp>
#include <ql/instruments/bmaswap.hpp>
#include <ql/instruments/futures.hpp>
#include <ql/time/calendar.hpp>
Expand Down Expand Up @@ -313,6 +314,16 @@
Date customPillarDate = Date(),
bool endOfMonth = false,
const ext::optional<bool>& useIndexedCoupons = ext::nullopt);
SwapRateHelper(const Handle<Quote>& rate,
const MakeVanillaSwap& swapBuilder,
Handle<Quote> spread = {},
Pillar::Choice pillar = Pillar::LastRelevantDate,
Date customPillarDate = Date());
SwapRateHelper(Rate rate,
const MakeVanillaSwap& swapBuilder,
Handle<Quote> spread = {},
Pillar::Choice pillar = Pillar::LastRelevantDate,
Date customPillarDate = Date());
Comment on lines +317 to +326
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple pairs of ctors which differ only in Rate rate vs const Handle<Quote>& rate. Should we try to find a way to reduce this duplication? I could imagine there might be a way to define a macro which produces both declarations in the header. The implementation in the cpp would stay as is.

//! \name RateHelper interface
//@{
Real impliedQuote() const override;
Expand Down Expand Up @@ -347,6 +358,7 @@
Handle<YieldTermStructure> discountHandle_;
RelinkableHandle<YieldTermStructure> discountRelinkableHandle_;
ext::optional<bool> useIndexedCoupons_;
bool swapBuiltViaGivenBuilder;

Check notice on line 361 in ql/termstructures/yield/ratehelpers.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ql/termstructures/yield/ratehelpers.hpp#L361

class member 'SwapRateHelper::swapBuiltViaGivenBuilder' is never used.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this issue "'SwapRateHelper::swapBuiltViaGivenBuilder' is never used" from "Codacy Static Code Analysis" is a false positive. The member is used, e.g. in ratehelpers.cpp:l.681 SwapRateHelper::setTermStructure().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are supporting two different ctor types

  1. one accepting arguments (e.g. Period tenor) to built up the MakeVanillaSwap
  2. another one accepting directly the builder, i.e. MakeVanillaSwap swapBuilder

Thus, different member variables are used. We could represent this in code (e.g. via optional, variant). Here I used a version of this single boolean for minimal code changes.

};


Expand Down
Loading