From cef64cc98651cc9f44cf930a2018cb2da0369976 Mon Sep 17 00:00:00 2001 From: Eugene Toder Date: Fri, 9 Feb 2024 15:26:52 -0500 Subject: [PATCH 1/5] Reduce duplication in rate helpers constructors --- ql/handle.hpp | 16 +- ql/quotes/simplequote.hpp | 5 + ql/termstructures/bootstraphelper.hpp | 3 +- ql/termstructures/yield/ratehelpers.cpp | 197 +++++------------------- 4 files changed, 50 insertions(+), 171 deletions(-) diff --git a/ql/handle.hpp b/ql/handle.hpp index 63934035c4d..37b82e1ed17 100644 --- a/ql/handle.hpp +++ b/ql/handle.hpp @@ -42,9 +42,9 @@ namespace QuantLib { protected: class Link : public Observable, public Observer { public: - explicit Link(const ext::shared_ptr& h, + explicit Link(ext::shared_ptr h, bool registerAsObserver); - void linkTo(const ext::shared_ptr&, + void linkTo(ext::shared_ptr, bool registerAsObserver); bool empty() const { return !h_; } const ext::shared_ptr& currentLink() const { return h_; } @@ -75,9 +75,9 @@ namespace QuantLib { //@{ Handle() : Handle(ext::shared_ptr()) {} - explicit Handle(const ext::shared_ptr& p, + explicit Handle(ext::shared_ptr p, bool registerAsObserver = true) - : link_(new Link(p,registerAsObserver)) {} + : link_(new Link(std::move(p), registerAsObserver)) {} //@} //! dereferencing const ext::shared_ptr& currentLink() const; @@ -125,17 +125,17 @@ namespace QuantLib { // inline definitions template - inline Handle::Link::Link(const ext::shared_ptr& h, bool registerAsObserver) { - linkTo(h,registerAsObserver); + inline Handle::Link::Link(ext::shared_ptr h, bool registerAsObserver) { + linkTo(std::move(h), registerAsObserver); } template - inline void Handle::Link::linkTo(const ext::shared_ptr& h, + inline void Handle::Link::linkTo(ext::shared_ptr h, bool registerAsObserver) { if ((h != h_) || (isObserver_ != registerAsObserver)) { if (h_ && isObserver_) unregisterWith(h_); - h_ = h; + h_ = std::move(h); isObserver_ = registerAsObserver; if (h_ && isObserver_) registerWith(h_); diff --git a/ql/quotes/simplequote.hpp b/ql/quotes/simplequote.hpp index d859351d116..df260a5c828 100644 --- a/ql/quotes/simplequote.hpp +++ b/ql/quotes/simplequote.hpp @@ -32,6 +32,7 @@ namespace QuantLib { //! market element returning a stored value class SimpleQuote : public Quote { public: + static Handle handle(Real value = Null()); SimpleQuote(Real value = Null()); //! \name Quote interface //@{ @@ -50,6 +51,10 @@ namespace QuantLib { // inline definitions + inline Handle SimpleQuote::handle(Real value) { + return Handle(ext::make_shared(value)); + } + inline SimpleQuote::SimpleQuote(Real value) : value_(value) {} diff --git a/ql/termstructures/bootstraphelper.hpp b/ql/termstructures/bootstraphelper.hpp index 8fd0b3f66c3..3f28d3bee99 100644 --- a/ql/termstructures/bootstraphelper.hpp +++ b/ql/termstructures/bootstraphelper.hpp @@ -155,8 +155,7 @@ namespace QuantLib { template BootstrapHelper::BootstrapHelper(Real quote) - : quote_(Handle(ext::shared_ptr(new SimpleQuote(quote)))), - termStructure_(nullptr) {} + : quote_(SimpleQuote::handle(quote)), termStructure_(nullptr) {} template void BootstrapHelper::setTermStructure(TS* t) { diff --git a/ql/termstructures/yield/ratehelpers.cpp b/ql/termstructures/yield/ratehelpers.cpp index adb35cc57bb..e56b55dbf17 100644 --- a/ql/termstructures/yield/ratehelpers.cpp +++ b/ql/termstructures/yield/ratehelpers.cpp @@ -94,10 +94,10 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(Handle(ext::make_shared(price)), + : FuturesRateHelper(SimpleQuote::handle(price), iborStartDate, lengthInMonths, calendar, convention, endOfMonth, dayCounter, - Handle(ext::make_shared(convAdj)), type) {} + SimpleQuote::handle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -150,9 +150,9 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(Handle(ext::make_shared(price)), + : FuturesRateHelper(SimpleQuote::handle(price), iborStartDate, iborEndDate, dayCounter, - Handle(ext::make_shared(convAdj)), type) {} + SimpleQuote::handle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -177,9 +177,9 @@ namespace QuantLib { const ext::shared_ptr& index, Rate convAdj, Futures::Type type) - : FuturesRateHelper(Handle(ext::make_shared(price)), + : FuturesRateHelper(SimpleQuote::handle(price), iborStartDate, index, - Handle(ext::make_shared(convAdj)), type) {} + SimpleQuote::handle(convAdj), type) {} Real FuturesRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -227,13 +227,8 @@ namespace QuantLib { BusinessDayConvention convention, bool endOfMonth, const DayCounter& dayCounter) - : RelativeDateRateHelper(rate) { - iborIndex_ = ext::make_shared("no-fix", // never take fixing into account - tenor, fixingDays, - Currency(), calendar, convention, - endOfMonth, dayCounter, termStructureHandle_); - DepositRateHelper::initializeDates(); - } + : DepositRateHelper(SimpleQuote::handle(rate), tenor, fixingDays, calendar, convention, + endOfMonth, dayCounter) {} DepositRateHelper::DepositRateHelper(const Handle& rate, const ext::shared_ptr& i) @@ -244,10 +239,7 @@ namespace QuantLib { DepositRateHelper::DepositRateHelper(Rate rate, const ext::shared_ptr& i) - : RelativeDateRateHelper(rate) { - iborIndex_ = i->clone(termStructureHandle_); - DepositRateHelper::initializeDates(); - } + : DepositRateHelper(SimpleQuote::handle(rate), i) {} Real DepositRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -298,21 +290,12 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(monthsToStart*Months), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { + : FraRateHelper(rate, monthsToStart*Months, monthsToEnd-monthsToStart, fixingDays, calendar, + convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) { QL_REQUIRE(monthsToEnd>monthsToStart, "monthsToEnd (" << monthsToEnd << ") must be grater than monthsToStart (" << monthsToStart << ")"); - // no way to take fixing into account, - // even if we would like to for FRA over today - iborIndex_ = ext::make_shared("no-fix", // correct family name would be needed - (monthsToEnd-monthsToStart)*Months, - fixingDays, - Currency(), calendar, convention, - endOfMonth, dayCounter, termStructureHandle_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); } FraRateHelper::FraRateHelper(Rate rate, @@ -326,22 +309,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(monthsToStart*Months), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - QL_REQUIRE(monthsToEnd>monthsToStart, - "monthsToEnd (" << monthsToEnd << - ") must be grater than monthsToStart (" << monthsToStart << - ")"); - // no way to take fixing into account, - // even if we would like to for FRA over today - iborIndex_ = ext::make_shared("no-fix", // correct family name would be needed - (monthsToEnd-monthsToStart)*Months, - fixingDays, - Currency(), calendar, convention, - endOfMonth, dayCounter, termStructureHandle_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(SimpleQuote::handle(rate), monthsToStart, monthsToEnd, fixingDays, calendar, + convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Natural monthsToStart, @@ -349,18 +318,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(monthsToStart*Months), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - // take fixing into account - iborIndex_ = i->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_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(rate, monthsToStart*Months, i, pillarChoice, customPillarDate, useIndexedCoupon) + {} FraRateHelper::FraRateHelper(Rate rate, Natural monthsToStart, @@ -368,16 +327,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(monthsToStart*Months), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - // take fixing into account - iborIndex_ = i->clone(termStructureHandle_); - // see above - iborIndex_->unregisterWith(termStructureHandle_); - registerWith(iborIndex_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(SimpleQuote::handle(rate), monthsToStart, i, pillarChoice, customPillarDate, + useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Period periodToStart, @@ -414,18 +365,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(periodToStart), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - // no way to take fixing into account, - // even if we would like to for FRA over today - iborIndex_ = ext::make_shared("no-fix", // correct family name would be needed - lengthInMonths*Months, - fixingDays, - Currency(), calendar, convention, - endOfMonth, dayCounter, termStructureHandle_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(SimpleQuote::handle(rate), periodToStart, lengthInMonths, fixingDays, calendar, + convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Period periodToStart, @@ -437,7 +378,9 @@ namespace QuantLib { pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { // take fixing into account iborIndex_ = i->clone(termStructureHandle_); - // see above + // 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_); pillarDate_ = customPillarDate; @@ -450,16 +393,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), periodToStart_(periodToStart), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - // take fixing into account - iborIndex_ = i->clone(termStructureHandle_); - // see above - iborIndex_->unregisterWith(termStructureHandle_); - registerWith(iborIndex_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(SimpleQuote::handle(rate), periodToStart, i, pillarChoice, customPillarDate, + useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Natural immOffsetStart, @@ -486,16 +421,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : RelativeDateRateHelper(rate), immOffsetStart_(immOffsetStart), immOffsetEnd_(immOffsetEnd), - pillarChoice_(pillarChoice), useIndexedCoupon_(useIndexedCoupon) { - // take fixing into account - iborIndex_ = i->clone(termStructureHandle_); - // see above - iborIndex_->unregisterWith(termStructureHandle_); - registerWith(iborIndex_); - pillarDate_ = customPillarDate; - FraRateHelper::initializeDates(); - } + : FraRateHelper(SimpleQuote::handle(rate), immOffsetStart, immOffsetEnd, i, pillarChoice, + customPillarDate, useIndexedCoupon) {} Real FraRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -605,26 +532,11 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : RelativeDateRateHelper(rate), settlementDays_(Null()), tenor_(swapIndex->tenor()), - pillarChoice_(pillarChoice), calendar_(swapIndex->fixingCalendar()), - fixedConvention_(swapIndex->fixedLegConvention()), - fixedFrequency_(swapIndex->fixedLegTenor().frequency()), - fixedDayCount_(swapIndex->dayCounter()), spread_(std::move(spread)), endOfMonth_(endOfMonth), - fwdStart_(fwdStart), discountHandle_(std::move(discount)), useIndexedCoupons_(useIndexedCoupons) { - // take fixing into account - iborIndex_ = swapIndex->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_); - registerWith(discountHandle_); - - pillarDate_ = customPillarDate; - SwapRateHelper::initializeDates(); - } + : SwapRateHelper(rate, swapIndex->tenor(), swapIndex->fixingCalendar(), + swapIndex->fixedLegTenor().frequency(), swapIndex->fixedLegConvention(), + swapIndex->dayCounter(), swapIndex->iborIndex(), std::move(spread), fwdStart, + std::move(discount), Null(), pillarChoice, customPillarDate, endOfMonth, + useIndexedCoupons) {} SwapRateHelper::SwapRateHelper(const Handle& rate, const Period& tenor, @@ -672,27 +584,8 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : RelativeDateRateHelper(rate), settlementDays_(Null()), tenor_(swapIndex->tenor()), - pillarChoice_(pillarChoice), calendar_(swapIndex->fixingCalendar()), - fixedConvention_(swapIndex->fixedLegConvention()), - fixedFrequency_(swapIndex->fixedLegTenor().frequency()), - fixedDayCount_(swapIndex->dayCounter()), spread_(std::move(spread)), endOfMonth_(endOfMonth), - fwdStart_(fwdStart), discountHandle_(std::move(discount)), - useIndexedCoupons_(useIndexedCoupons) { - // take fixing into account - iborIndex_ = swapIndex->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_); - registerWith(discountHandle_); - - pillarDate_ = customPillarDate; - SwapRateHelper::initializeDates(); - } + : SwapRateHelper(SimpleQuote::handle(rate), swapIndex, std::move(spread), fwdStart, + std::move(discount), pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} SwapRateHelper::SwapRateHelper(Rate rate, const Period& tenor, @@ -709,27 +602,9 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : RelativeDateRateHelper(rate), settlementDays_(settlementDays), tenor_(tenor), - pillarChoice_(pillarChoice), calendar_(std::move(calendar)), - fixedConvention_(fixedConvention), fixedFrequency_(fixedFrequency), - fixedDayCount_(std::move(fixedDayCount)), spread_(std::move(spread)), endOfMonth_(endOfMonth), - fwdStart_(fwdStart), discountHandle_(std::move(discount)), - useIndexedCoupons_(useIndexedCoupons) { - - // 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_); - registerWith(spread_); - registerWith(discountHandle_); - - pillarDate_ = customPillarDate; - SwapRateHelper::initializeDates(); - } + : SwapRateHelper(SimpleQuote::handle(rate), tenor, calendar, fixedFrequency, fixedConvention, + fixedDayCount, iborIndex, std::move(spread), fwdStart, std::move(discount), settlementDays, + pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} void SwapRateHelper::initializeDates() { From 9ca941007ef0bf4f926312583e23abf1793da749 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Fri, 16 Feb 2024 10:22:35 +0100 Subject: [PATCH 2/5] Extract static method to function for shorter calls --- ql/quotes/simplequote.hpp | 6 ++- ql/termstructures/bootstraphelper.hpp | 2 +- ql/termstructures/yield/ratehelpers.cpp | 49 +++++++++++-------------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/ql/quotes/simplequote.hpp b/ql/quotes/simplequote.hpp index df260a5c828..efde7cecaa1 100644 --- a/ql/quotes/simplequote.hpp +++ b/ql/quotes/simplequote.hpp @@ -32,7 +32,6 @@ namespace QuantLib { //! market element returning a stored value class SimpleQuote : public Quote { public: - static Handle handle(Real value = Null()); SimpleQuote(Real value = Null()); //! \name Quote interface //@{ @@ -49,9 +48,12 @@ namespace QuantLib { Real value_; }; + Handle handle(Real value); + + // inline definitions - inline Handle SimpleQuote::handle(Real value) { + inline Handle handle(Real value) { return Handle(ext::make_shared(value)); } diff --git a/ql/termstructures/bootstraphelper.hpp b/ql/termstructures/bootstraphelper.hpp index 3f28d3bee99..ea892b0144f 100644 --- a/ql/termstructures/bootstraphelper.hpp +++ b/ql/termstructures/bootstraphelper.hpp @@ -155,7 +155,7 @@ namespace QuantLib { template BootstrapHelper::BootstrapHelper(Real quote) - : quote_(SimpleQuote::handle(quote)), termStructure_(nullptr) {} + : quote_(handle(quote)), termStructure_(nullptr) {} template void BootstrapHelper::setTermStructure(TS* t) { diff --git a/ql/termstructures/yield/ratehelpers.cpp b/ql/termstructures/yield/ratehelpers.cpp index e56b55dbf17..70bf3aeb749 100644 --- a/ql/termstructures/yield/ratehelpers.cpp +++ b/ql/termstructures/yield/ratehelpers.cpp @@ -94,10 +94,8 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(SimpleQuote::handle(price), - iborStartDate, lengthInMonths, calendar, - convention, endOfMonth, dayCounter, - SimpleQuote::handle(convAdj), type) {} + : FuturesRateHelper(handle(price), iborStartDate, lengthInMonths, calendar, + convention, endOfMonth, dayCounter, handle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -150,9 +148,8 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(SimpleQuote::handle(price), - iborStartDate, iborEndDate, dayCounter, - SimpleQuote::handle(convAdj), type) {} + : FuturesRateHelper(handle(price), iborStartDate, iborEndDate, dayCounter, + handle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -177,9 +174,7 @@ namespace QuantLib { const ext::shared_ptr& index, Rate convAdj, Futures::Type type) - : FuturesRateHelper(SimpleQuote::handle(price), - iborStartDate, index, - SimpleQuote::handle(convAdj), type) {} + : FuturesRateHelper(handle(price), iborStartDate, index, handle(convAdj), type) {} Real FuturesRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -227,8 +222,8 @@ namespace QuantLib { BusinessDayConvention convention, bool endOfMonth, const DayCounter& dayCounter) - : DepositRateHelper(SimpleQuote::handle(rate), tenor, fixingDays, calendar, convention, - endOfMonth, dayCounter) {} + : DepositRateHelper(handle(rate), tenor, fixingDays, calendar, convention, + endOfMonth, dayCounter) {} DepositRateHelper::DepositRateHelper(const Handle& rate, const ext::shared_ptr& i) @@ -239,7 +234,7 @@ namespace QuantLib { DepositRateHelper::DepositRateHelper(Rate rate, const ext::shared_ptr& i) - : DepositRateHelper(SimpleQuote::handle(rate), i) {} + : DepositRateHelper(handle(rate), i) {} Real DepositRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -309,8 +304,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(SimpleQuote::handle(rate), monthsToStart, monthsToEnd, fixingDays, calendar, - convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} + : FraRateHelper(handle(rate), monthsToStart, monthsToEnd, fixingDays, calendar, + convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Natural monthsToStart, @@ -327,8 +322,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(SimpleQuote::handle(rate), monthsToStart, i, pillarChoice, customPillarDate, - useIndexedCoupon) {} + : FraRateHelper(handle(rate), monthsToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Period periodToStart, @@ -365,8 +359,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(SimpleQuote::handle(rate), periodToStart, lengthInMonths, fixingDays, calendar, - convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} + : FraRateHelper(handle(rate), periodToStart, lengthInMonths, fixingDays, calendar, + convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Period periodToStart, @@ -393,8 +387,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(SimpleQuote::handle(rate), periodToStart, i, pillarChoice, customPillarDate, - useIndexedCoupon) {} + : FraRateHelper(handle(rate), periodToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Natural immOffsetStart, @@ -421,8 +414,8 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(SimpleQuote::handle(rate), immOffsetStart, immOffsetEnd, i, pillarChoice, - customPillarDate, useIndexedCoupon) {} + : FraRateHelper(handle(rate), immOffsetStart, immOffsetEnd, i, pillarChoice, + customPillarDate, useIndexedCoupon) {} Real FraRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -584,8 +577,8 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : SwapRateHelper(SimpleQuote::handle(rate), swapIndex, std::move(spread), fwdStart, - std::move(discount), pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} + : SwapRateHelper(handle(rate), swapIndex, std::move(spread), fwdStart, + std::move(discount), pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} SwapRateHelper::SwapRateHelper(Rate rate, const Period& tenor, @@ -602,9 +595,9 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : SwapRateHelper(SimpleQuote::handle(rate), tenor, calendar, fixedFrequency, fixedConvention, - fixedDayCount, iborIndex, std::move(spread), fwdStart, std::move(discount), settlementDays, - pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} + : SwapRateHelper(handle(rate), tenor, calendar, fixedFrequency, fixedConvention, + fixedDayCount, iborIndex, std::move(spread), fwdStart, std::move(discount), settlementDays, + pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} void SwapRateHelper::initializeDates() { From f1db18fa543973945c8ff67d9034127cdb3453a6 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Fri, 16 Feb 2024 11:48:36 +0100 Subject: [PATCH 3/5] Use convenience function in examples --- Examples/CDS/CDS.cpp | 2 +- Examples/CVAIRS/CVAIRS.cpp | 2 +- Examples/ConvertibleBonds/ConvertibleBonds.cpp | 4 ++-- Examples/DiscreteHedging/DiscreteHedging.cpp | 3 +-- Examples/EquityOption/EquityOption.cpp | 2 +- Examples/Gaussian1dModels/Gaussian1dModels.cpp | 10 ++++------ 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Examples/CDS/CDS.cpp b/Examples/CDS/CDS.cpp index 2cfb998cdc6..040df2cde0f 100644 --- a/Examples/CDS/CDS.cpp +++ b/Examples/CDS/CDS.cpp @@ -98,7 +98,7 @@ void example01() { std::vector> instruments; for (Size i = 0; i < 4; i++) { instruments.push_back(ext::make_shared( - Handle(ext::make_shared(quoted_spreads[i])), + handle(quoted_spreads[i]), tenors[i], settlementDays, calendar, Quarterly, Following, DateGeneration::TwentiethIMM, Actual365Fixed(), recovery_rate, tsCurve)); diff --git a/Examples/CVAIRS/CVAIRS.cpp b/Examples/CVAIRS/CVAIRS.cpp index 9ef26737cd7..3d44f7558ef 100644 --- a/Examples/CVAIRS/CVAIRS.cpp +++ b/Examples/CVAIRS/CVAIRS.cpp @@ -69,7 +69,7 @@ int main(int, char* []) { vector> swapHelpers; for(Size i=0; i( - Handle(ext::make_shared(ratesSwapmkt[i])), + handle(ratesSwapmkt[i]), tenorsSwapMkt[i] * Years, TARGET(), Quarterly, diff --git a/Examples/ConvertibleBonds/ConvertibleBonds.cpp b/Examples/ConvertibleBonds/ConvertibleBonds.cpp index 320599ca36e..69b7f310301 100644 --- a/Examples/ConvertibleBonds/ConvertibleBonds.cpp +++ b/Examples/ConvertibleBonds/ConvertibleBonds.cpp @@ -144,7 +144,7 @@ int main(int, char* []) { auto exercise = ext::make_shared(exerciseDate); auto amExercise = ext::make_shared(settlementDate, exerciseDate); - Handle underlyingH(ext::make_shared(underlying)); + auto underlyingH = handle(underlying); Handle flatTermStructure( ext::make_shared(settlementDate, riskFreeRate, dayCounter)); @@ -160,7 +160,7 @@ int main(int, char* []) { Size timeSteps = 801; - Handle creditSpread(ext::make_shared(spreadRate)); + auto creditSpread = handle(spreadRate); auto rate = ext::make_shared(riskFreeRate); diff --git a/Examples/DiscreteHedging/DiscreteHedging.cpp b/Examples/DiscreteHedging/DiscreteHedging.cpp index 8ebe9d4fb8c..7df493e17e0 100644 --- a/Examples/DiscreteHedging/DiscreteHedging.cpp +++ b/Examples/DiscreteHedging/DiscreteHedging.cpp @@ -300,8 +300,7 @@ void ReplicationError::compute(Size nTimeSteps, Size nSamples) Calendar calendar = TARGET(); Date today = Date::todaysDate(); DayCounter dayCount = Actual365Fixed(); - Handle stateVariable( - ext::make_shared(s0_)); + auto stateVariable = handle(s0_); Handle riskFreeRate( ext::make_shared(today, r_, dayCount)); Handle dividendYield( diff --git a/Examples/EquityOption/EquityOption.cpp b/Examples/EquityOption/EquityOption.cpp index 2de0f67f759..014a2940c9f 100644 --- a/Examples/EquityOption/EquityOption.cpp +++ b/Examples/EquityOption/EquityOption.cpp @@ -97,7 +97,7 @@ int main(int, char* []) { auto americanExercise = ext::make_shared(settlementDate, maturity); - Handle underlyingH(ext::make_shared(underlying)); + auto underlyingH = handle(underlying); // bootstrap the yield/dividend/vol curves Handle flatTermStructure( diff --git a/Examples/Gaussian1dModels/Gaussian1dModels.cpp b/Examples/Gaussian1dModels/Gaussian1dModels.cpp index 651af1dd0ee..bbe445eeb96 100644 --- a/Examples/Gaussian1dModels/Gaussian1dModels.cpp +++ b/Examples/Gaussian1dModels/Gaussian1dModels.cpp @@ -137,9 +137,8 @@ int main(int argc, char *argv[]) { Real forward6mLevel = 0.025; Real oisLevel = 0.02; - Handle forward6mQuote( - ext::make_shared(forward6mLevel)); - Handle oisQuote(ext::make_shared(oisLevel)); + auto forward6mQuote = handle(forward6mLevel); + auto oisQuote = handle(oisLevel); Handle yts6m(ext::make_shared( 0, TARGET(), forward6mQuote, Actual365Fixed())); @@ -156,7 +155,7 @@ int main(int argc, char *argv[]) { << "\nat a level of " << forward6mLevel << std::endl; Real volLevel = 0.20; - Handle volQuote(ext::make_shared(volLevel)); + auto volQuote = handle(volLevel); Handle swaptionVol( ext::make_shared( 0, TARGET(), ModifiedFollowing, volQuote, Actual365Fixed())); @@ -483,8 +482,7 @@ int main(int argc, char *argv[]) { "\npricing this using the LinearTsrPricer for CMS coupon " "estimation" << std::endl; - Handle reversionQuote( - ext::make_shared(reversion)); + auto reversionQuote = handle(reversion); const Leg &leg0 = underlying4->leg(0); const Leg &leg1 = underlying4->leg(1); From 22dc9118b8082a3b77e04ad0cb6d293b0a61921d Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Fri, 16 Feb 2024 12:32:32 +0100 Subject: [PATCH 4/5] Create relinkable handle in convenience function --- ql/quotes/simplequote.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ql/quotes/simplequote.hpp b/ql/quotes/simplequote.hpp index efde7cecaa1..d38b93f1d27 100644 --- a/ql/quotes/simplequote.hpp +++ b/ql/quotes/simplequote.hpp @@ -48,13 +48,13 @@ namespace QuantLib { Real value_; }; - Handle handle(Real value); + RelinkableHandle handle(Real value); // inline definitions - inline Handle handle(Real value) { - return Handle(ext::make_shared(value)); + inline RelinkableHandle handle(Real value) { + return RelinkableHandle(ext::make_shared(value)); } inline SimpleQuote::SimpleQuote(Real value) From 30d6eb5be8e9637572258ee57455887b92f9aa42 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Mon, 19 Feb 2024 12:17:18 +0100 Subject: [PATCH 5/5] Rename convenience function --- Examples/CDS/CDS.cpp | 2 +- Examples/CVAIRS/CVAIRS.cpp | 2 +- .../ConvertibleBonds/ConvertibleBonds.cpp | 4 +-- Examples/DiscreteHedging/DiscreteHedging.cpp | 2 +- Examples/EquityOption/EquityOption.cpp | 2 +- .../Gaussian1dModels/Gaussian1dModels.cpp | 10 +++---- ql/quotes/simplequote.hpp | 4 +-- ql/termstructures/bootstraphelper.hpp | 2 +- ql/termstructures/yield/ratehelpers.cpp | 28 +++++++++---------- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Examples/CDS/CDS.cpp b/Examples/CDS/CDS.cpp index 040df2cde0f..3b7fe5e05c1 100644 --- a/Examples/CDS/CDS.cpp +++ b/Examples/CDS/CDS.cpp @@ -98,7 +98,7 @@ void example01() { std::vector> instruments; for (Size i = 0; i < 4; i++) { instruments.push_back(ext::make_shared( - handle(quoted_spreads[i]), + makeQuoteHandle(quoted_spreads[i]), tenors[i], settlementDays, calendar, Quarterly, Following, DateGeneration::TwentiethIMM, Actual365Fixed(), recovery_rate, tsCurve)); diff --git a/Examples/CVAIRS/CVAIRS.cpp b/Examples/CVAIRS/CVAIRS.cpp index 3d44f7558ef..e992f3c1110 100644 --- a/Examples/CVAIRS/CVAIRS.cpp +++ b/Examples/CVAIRS/CVAIRS.cpp @@ -69,7 +69,7 @@ int main(int, char* []) { vector> swapHelpers; for(Size i=0; i( - handle(ratesSwapmkt[i]), + makeQuoteHandle(ratesSwapmkt[i]), tenorsSwapMkt[i] * Years, TARGET(), Quarterly, diff --git a/Examples/ConvertibleBonds/ConvertibleBonds.cpp b/Examples/ConvertibleBonds/ConvertibleBonds.cpp index 69b7f310301..81553205578 100644 --- a/Examples/ConvertibleBonds/ConvertibleBonds.cpp +++ b/Examples/ConvertibleBonds/ConvertibleBonds.cpp @@ -144,7 +144,7 @@ int main(int, char* []) { auto exercise = ext::make_shared(exerciseDate); auto amExercise = ext::make_shared(settlementDate, exerciseDate); - auto underlyingH = handle(underlying); + auto underlyingH = makeQuoteHandle(underlying); Handle flatTermStructure( ext::make_shared(settlementDate, riskFreeRate, dayCounter)); @@ -160,7 +160,7 @@ int main(int, char* []) { Size timeSteps = 801; - auto creditSpread = handle(spreadRate); + auto creditSpread = makeQuoteHandle(spreadRate); auto rate = ext::make_shared(riskFreeRate); diff --git a/Examples/DiscreteHedging/DiscreteHedging.cpp b/Examples/DiscreteHedging/DiscreteHedging.cpp index 7df493e17e0..3248b31a332 100644 --- a/Examples/DiscreteHedging/DiscreteHedging.cpp +++ b/Examples/DiscreteHedging/DiscreteHedging.cpp @@ -300,7 +300,7 @@ void ReplicationError::compute(Size nTimeSteps, Size nSamples) Calendar calendar = TARGET(); Date today = Date::todaysDate(); DayCounter dayCount = Actual365Fixed(); - auto stateVariable = handle(s0_); + auto stateVariable = makeQuoteHandle(s0_); Handle riskFreeRate( ext::make_shared(today, r_, dayCount)); Handle dividendYield( diff --git a/Examples/EquityOption/EquityOption.cpp b/Examples/EquityOption/EquityOption.cpp index 014a2940c9f..2a0c4bd1881 100644 --- a/Examples/EquityOption/EquityOption.cpp +++ b/Examples/EquityOption/EquityOption.cpp @@ -97,7 +97,7 @@ int main(int, char* []) { auto americanExercise = ext::make_shared(settlementDate, maturity); - auto underlyingH = handle(underlying); + auto underlyingH = makeQuoteHandle(underlying); // bootstrap the yield/dividend/vol curves Handle flatTermStructure( diff --git a/Examples/Gaussian1dModels/Gaussian1dModels.cpp b/Examples/Gaussian1dModels/Gaussian1dModels.cpp index bbe445eeb96..e411dad8cb9 100644 --- a/Examples/Gaussian1dModels/Gaussian1dModels.cpp +++ b/Examples/Gaussian1dModels/Gaussian1dModels.cpp @@ -118,7 +118,7 @@ void printModelCalibration( // here the main part of the code starts -int main(int argc, char *argv[]) { +int main(int, char *[]) { try { @@ -137,8 +137,8 @@ int main(int argc, char *argv[]) { Real forward6mLevel = 0.025; Real oisLevel = 0.02; - auto forward6mQuote = handle(forward6mLevel); - auto oisQuote = handle(oisLevel); + auto forward6mQuote = makeQuoteHandle(forward6mLevel); + auto oisQuote = makeQuoteHandle(oisLevel); Handle yts6m(ext::make_shared( 0, TARGET(), forward6mQuote, Actual365Fixed())); @@ -155,7 +155,7 @@ int main(int argc, char *argv[]) { << "\nat a level of " << forward6mLevel << std::endl; Real volLevel = 0.20; - auto volQuote = handle(volLevel); + auto volQuote = makeQuoteHandle(volLevel); Handle swaptionVol( ext::make_shared( 0, TARGET(), ModifiedFollowing, volQuote, Actual365Fixed())); @@ -482,7 +482,7 @@ int main(int argc, char *argv[]) { "\npricing this using the LinearTsrPricer for CMS coupon " "estimation" << std::endl; - auto reversionQuote = handle(reversion); + auto reversionQuote = makeQuoteHandle(reversion); const Leg &leg0 = underlying4->leg(0); const Leg &leg1 = underlying4->leg(1); diff --git a/ql/quotes/simplequote.hpp b/ql/quotes/simplequote.hpp index d38b93f1d27..900c3eb05d6 100644 --- a/ql/quotes/simplequote.hpp +++ b/ql/quotes/simplequote.hpp @@ -48,12 +48,12 @@ namespace QuantLib { Real value_; }; - RelinkableHandle handle(Real value); + RelinkableHandle makeQuoteHandle(Real value); // inline definitions - inline RelinkableHandle handle(Real value) { + inline RelinkableHandle makeQuoteHandle(Real value) { return RelinkableHandle(ext::make_shared(value)); } diff --git a/ql/termstructures/bootstraphelper.hpp b/ql/termstructures/bootstraphelper.hpp index ea892b0144f..66b071b42ce 100644 --- a/ql/termstructures/bootstraphelper.hpp +++ b/ql/termstructures/bootstraphelper.hpp @@ -155,7 +155,7 @@ namespace QuantLib { template BootstrapHelper::BootstrapHelper(Real quote) - : quote_(handle(quote)), termStructure_(nullptr) {} + : quote_(makeQuoteHandle(quote)), termStructure_(nullptr) {} template void BootstrapHelper::setTermStructure(TS* t) { diff --git a/ql/termstructures/yield/ratehelpers.cpp b/ql/termstructures/yield/ratehelpers.cpp index 70bf3aeb749..cdd72dcffe4 100644 --- a/ql/termstructures/yield/ratehelpers.cpp +++ b/ql/termstructures/yield/ratehelpers.cpp @@ -94,8 +94,8 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(handle(price), iborStartDate, lengthInMonths, calendar, - convention, endOfMonth, dayCounter, handle(convAdj), type) {} + : FuturesRateHelper(makeQuoteHandle(price), iborStartDate, lengthInMonths, calendar, + convention, endOfMonth, dayCounter, makeQuoteHandle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -148,8 +148,8 @@ namespace QuantLib { const DayCounter& dayCounter, Rate convAdj, Futures::Type type) - : FuturesRateHelper(handle(price), iborStartDate, iborEndDate, dayCounter, - handle(convAdj), type) {} + : FuturesRateHelper(makeQuoteHandle(price), iborStartDate, iborEndDate, dayCounter, + makeQuoteHandle(convAdj), type) {} FuturesRateHelper::FuturesRateHelper(const Handle& price, const Date& iborStartDate, @@ -174,7 +174,7 @@ namespace QuantLib { const ext::shared_ptr& index, Rate convAdj, Futures::Type type) - : FuturesRateHelper(handle(price), iborStartDate, index, handle(convAdj), type) {} + : FuturesRateHelper(makeQuoteHandle(price), iborStartDate, index, makeQuoteHandle(convAdj), type) {} Real FuturesRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -222,7 +222,7 @@ namespace QuantLib { BusinessDayConvention convention, bool endOfMonth, const DayCounter& dayCounter) - : DepositRateHelper(handle(rate), tenor, fixingDays, calendar, convention, + : DepositRateHelper(makeQuoteHandle(rate), tenor, fixingDays, calendar, convention, endOfMonth, dayCounter) {} DepositRateHelper::DepositRateHelper(const Handle& rate, @@ -234,7 +234,7 @@ namespace QuantLib { DepositRateHelper::DepositRateHelper(Rate rate, const ext::shared_ptr& i) - : DepositRateHelper(handle(rate), i) {} + : DepositRateHelper(makeQuoteHandle(rate), i) {} Real DepositRateHelper::impliedQuote() const { QL_REQUIRE(termStructure_ != nullptr, "term structure not set"); @@ -304,7 +304,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(handle(rate), monthsToStart, monthsToEnd, fixingDays, calendar, + : FraRateHelper(makeQuoteHandle(rate), monthsToStart, monthsToEnd, fixingDays, calendar, convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, @@ -322,7 +322,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(handle(rate), monthsToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} + : FraRateHelper(makeQuoteHandle(rate), monthsToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Period periodToStart, @@ -359,7 +359,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(handle(rate), periodToStart, lengthInMonths, fixingDays, calendar, + : FraRateHelper(makeQuoteHandle(rate), periodToStart, lengthInMonths, fixingDays, calendar, convention, endOfMonth, dayCounter, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, @@ -387,7 +387,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(handle(rate), periodToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} + : FraRateHelper(makeQuoteHandle(rate), periodToStart, i, pillarChoice, customPillarDate, useIndexedCoupon) {} FraRateHelper::FraRateHelper(const Handle& rate, Natural immOffsetStart, @@ -414,7 +414,7 @@ namespace QuantLib { Pillar::Choice pillarChoice, Date customPillarDate, bool useIndexedCoupon) - : FraRateHelper(handle(rate), immOffsetStart, immOffsetEnd, i, pillarChoice, + : FraRateHelper(makeQuoteHandle(rate), immOffsetStart, immOffsetEnd, i, pillarChoice, customPillarDate, useIndexedCoupon) {} Real FraRateHelper::impliedQuote() const { @@ -577,7 +577,7 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : SwapRateHelper(handle(rate), swapIndex, std::move(spread), fwdStart, + : SwapRateHelper(makeQuoteHandle(rate), swapIndex, std::move(spread), fwdStart, std::move(discount), pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {} SwapRateHelper::SwapRateHelper(Rate rate, @@ -595,7 +595,7 @@ namespace QuantLib { Date customPillarDate, bool endOfMonth, const ext::optional& useIndexedCoupons) - : SwapRateHelper(handle(rate), tenor, calendar, fixedFrequency, fixedConvention, + : SwapRateHelper(makeQuoteHandle(rate), tenor, calendar, fixedFrequency, fixedConvention, fixedDayCount, iborIndex, std::move(spread), fwdStart, std::move(discount), settlementDays, pillarChoice, customPillarDate, endOfMonth, useIndexedCoupons) {}