Skip to content

Commit

Permalink
Add a method to determine whether an IBOR coupon has fixed or not
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhoiscontrary committed Jun 14, 2024
1 parent 8770770 commit 33fcd31
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ql/cashflows/iborcoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,24 @@ namespace QuantLib {
return fixingDate_;
}

Rate IborCoupon::indexFixing() const {
bool IborCoupon::hasFixed() const {
Date today = QuantLib::Settings::instance().evaluationDate();

if (fixingDate_ > today) {
return false;
} else if (fixingDate_ < today) {
return true;
} else {
// fixingDate_ == today
if (QuantLib::Settings::instance().enforcesTodaysHistoricFixings()) {
return true;
} else {
return index_->hasHistoricalFixing(fixingDate_);
}
}
}

Rate IborCoupon::indexFixing() const {
initializeCachedData();

/* instead of just returning index_->fixing(fixingValueDate_)
Expand Down
1 change: 1 addition & 0 deletions ql/cashflows/iborcoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace QuantLib {
//! \name Inspectors
//@{
const ext::shared_ptr<IborIndex>& iborIndex() const { return iborIndex_; }
bool hasFixed() const;
//@}
//! \name FloatingRateCoupon interface
//@{
Expand Down
60 changes: 60 additions & 0 deletions test-suite/cashflows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,66 @@ BOOST_AUTO_TEST_CASE(testFixedIborCouponWithoutForecastCurve) {
}
}

IborCoupon iborCouponForFixingDate(const ext::shared_ptr<IborIndex>& index, Date fixingDate) {
Date startDate = index->valueDate(fixingDate);
Date endDate = index->maturityDate(fixingDate);

IborCoupon coupon(endDate, 100.0, startDate, endDate, index->fixingDays(), index);
coupon.setPricer(ext::make_shared<BlackIborCouponPricer>());

return coupon;
}

BOOST_AUTO_TEST_CASE(testIborCouponKnowsWhenitHasFixed) {
BOOST_TEST_MESSAGE("Testing ibor coupon knowing when it has fixed...");

Date today = Settings::instance().evaluationDate();

auto index = ext::make_shared<Euribor3M>();
index->clearFixings();
auto calendar = index->fixingCalendar();

bool defaultEnforcesTodaysHistoricFixings =
QuantLib::Settings::instance().enforcesTodaysHistoricFixings();

{
IborCoupon coupon = iborCouponForFixingDate(index, calendar.advance(today, -1, Days));
// this should not throw an exception if the fixing is missing
BOOST_CHECK_EQUAL(coupon.hasFixed(), true);
}

{
IborCoupon coupon = iborCouponForFixingDate(index, today);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() = false;
BOOST_CHECK_EQUAL(coupon.hasFixed(), false);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() =
defaultEnforcesTodaysHistoricFixings;
}

{
IborCoupon coupon = iborCouponForFixingDate(index, today);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() = false;
index->addFixing(coupon.fixingDate(), 0.01);
BOOST_CHECK_EQUAL(coupon.hasFixed(), true);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() =
defaultEnforcesTodaysHistoricFixings;
index->addFixing(coupon.fixingDate(), Null<Real>(), true);
}

{
IborCoupon coupon = iborCouponForFixingDate(index, today);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() = true;
BOOST_CHECK_EQUAL(coupon.hasFixed(), true);
QuantLib::Settings::instance().enforcesTodaysHistoricFixings() =
defaultEnforcesTodaysHistoricFixings;
}

{
IborCoupon coupon = iborCouponForFixingDate(index, calendar.advance(today, 1, Days));
BOOST_CHECK_EQUAL(coupon.hasFixed(), false);
}
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 33fcd31

Please sign in to comment.