Skip to content

Commit

Permalink
optionally initialize iborIndex, discountingCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulXiCao committed Apr 12, 2024
1 parent 4da4569 commit 3119b30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
50 changes: 23 additions & 27 deletions ql/termstructures/yield/ratehelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,17 @@ namespace QuantLib {
std::move(discount), Null<Natural>(), pillarChoice, customPillarDate, endOfMonth,
useIndexedCoupons) {}

SwapRateHelper::Handles::Handles(const ext::shared_ptr<IborIndex>& iborIndex,
Handle<YieldTermStructure>&& discount)
: discount_(std::move(discount)) {
// take fixing into account
iborIndex_ = iborIndex->clone(termStructure_);
// We want to be notified of changes of fixings, but we don't
// want notifications from termStructureHandle_ (they would
// interfere with bootstrapping.)
iborIndex_->unregisterWith(termStructure_);
}

SwapRateHelper::SwapRateHelper(const Handle<Quote>& rate,
const Period& tenor,
Calendar calendar,
Expand All @@ -548,20 +559,13 @@ namespace QuantLib {
bool endOfMonth,
const ext::optional<bool>& useIndexedCoupons)
: RelativeDateRateHelper(rate), pillarChoice_(pillarChoice), spread_(std::move(spread)),
forwardStart_(fwdStart), discountHandles_(DiscountHandles(std::move(discount))) {
// take fixing into account
iborIndex_ = iborIndex->clone(termStructureHandle_);
// We want to be notified of changes of fixings, but we don't
// want notifications from termStructureHandle_ (they would
// interfere with bootstrapping.)
iborIndex_->unregisterWith(termStructureHandle_);

registerWith(iborIndex_);
forwardStart_(fwdStart), handles_(Handles(iborIndex, std::move(discount))) {
registerWith(handles_->iborIndex_);
registerWith(spread_);
registerWith(discountHandles_->handle_);
registerWith(handles_->discount_);

pillarDate_ = customPillarDate;
swap_ = MakeVanillaSwap(tenor, iborIndex_, 0.0, fwdStart)
swap_ = MakeVanillaSwap(tenor, handles_->iborIndex_, 0.0, fwdStart)
.withSettlementDays(settlementDays)
.withFixedLegDayCount(fixedDayCount)
.withFixedLegTenor(Period(fixedFrequency))
Expand All @@ -572,7 +576,7 @@ namespace QuantLib {
.withFloatingLegCalendar(calendar)
.withFloatingLegEndOfMonth(endOfMonth)
.withIndexedCoupons(useIndexedCoupons)
.withDiscountingTermStructure(discountHandles_->relinkableHandle_);
.withDiscountingTermStructure(handles_->relinkableDiscount_);
SwapRateHelper::initializeDates();
}

Expand Down Expand Up @@ -614,18 +618,10 @@ namespace QuantLib {
Date customPillarDate)
: RelativeDateRateHelper(rate), pillarChoice_(pillar), spread_(std::move(spread)),
forwardStart_(swapBuilder.forwardStart_) {
swap_ = swapBuilder;
// take fixing into account
iborIndex_ = swap_->iborIndex()->clone(termStructureHandle_);
// We want to be notified of changes of fixings, but we don't
// want notifications from termStructureHandle_ (they would
// interfere with bootstrapping.)
iborIndex_->unregisterWith(termStructureHandle_);

registerWith(iborIndex_);
registerWith(spread_);

pillarDate_ = customPillarDate;
swap_ = swapBuilder;
SwapRateHelper::initializeDates();
}

Expand Down Expand Up @@ -680,13 +676,13 @@ namespace QuantLib {
// force recalculation when needed
bool observer = false;

ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
termStructureHandle_.linkTo(temp, observer);
if (handles_) {
ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
handles_->termStructure_.linkTo(temp, observer);

if (discountHandles_) {
Handle<YieldTermStructure>& h = discountHandles_->handle_;
RelinkableHandle<YieldTermStructure>& rh = discountHandles_->relinkableHandle_;
rh.linkTo((h.empty() ? temp : *h), observer);
Handle<YieldTermStructure>& dh = handles_->discount_;
RelinkableHandle<YieldTermStructure>& rdh = handles_->relinkableDiscount_;
rdh.linkTo((dh.empty() ? temp : *dh), observer);
}

RelativeDateRateHelper::setTermStructure(t);
Expand Down
16 changes: 9 additions & 7 deletions ql/termstructures/yield/ratehelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,20 @@ namespace QuantLib {
void initializeDates() override;

Pillar::Choice pillarChoice_;
ext::shared_ptr<IborIndex> iborIndex_;
ext::shared_ptr<VanillaSwap> swap_;
RelinkableHandle<YieldTermStructure> termStructureHandle_;
Handle<Quote> spread_;
Period forwardStart_;

struct DiscountHandles {
Handle<YieldTermStructure> handle_;
RelinkableHandle<YieldTermStructure> relinkableHandle_;
DiscountHandles(Handle<YieldTermStructure>&& h) : handle_(std::move(h)) {}
struct Handles {
ext::shared_ptr<IborIndex> iborIndex_;
RelinkableHandle<YieldTermStructure> termStructure_;
Handle<YieldTermStructure> discount_;
RelinkableHandle<YieldTermStructure> relinkableDiscount_;

Handles(const ext::shared_ptr<IborIndex>& iborIndex, Handle<YieldTermStructure>&& discountingCurve);
};
ext::optional<DiscountHandles> discountHandles_;
// only set if iborIndex explicitly given in ctor
ext::optional<Handles> handles_;
};


Expand Down

0 comments on commit 3119b30

Please sign in to comment.