From 279b4d73a04e746d65cf95fc09f785959dbd8d33 Mon Sep 17 00:00:00 2001 From: Peter Caspers Date: Tue, 12 Dec 2023 19:33:20 +0100 Subject: [PATCH] GH-1791 add clarifying comment and unit test to confirm result caching --- .../swaption/blackswaptionengine.hpp | 7 ++--- test-suite/swaption.cpp | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ql/pricingengines/swaption/blackswaptionengine.hpp b/ql/pricingengines/swaption/blackswaptionengine.hpp index 8b8a831b99a..6ac9c175ff4 100644 --- a/ql/pricingengines/swaption/blackswaptionengine.hpp +++ b/ql/pricingengines/swaption/blackswaptionengine.hpp @@ -223,9 +223,10 @@ namespace QuantLib { Date exerciseDate = arguments_.exercise->date(0); - // the part of the swap preceding exerciseDate should be truncated - // to avoid taking into account unwanted cashflows - // for the moment we add a check avoiding this situation + // The part of the swap preceding exerciseDate should be truncated to avoid taking into + // account unwanted cashflows. For the moment we add a check avoiding this situation. + // Furthermore, we take a copy of the underlying swap. This avoids notifying the swaption + // when we set a pricing engine on the swap below. VanillaSwap swap = *arguments_.swap; const Leg& fixedLeg = swap.fixedLeg(); ext::shared_ptr firstCoupon = diff --git a/test-suite/swaption.cpp b/test-suite/swaption.cpp index c5faac74fb0..dbd2a22b2a5 100644 --- a/test-suite/swaption.cpp +++ b/test-suite/swaption.cpp @@ -121,6 +121,32 @@ BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) BOOST_AUTO_TEST_SUITE(SwaptionTest) +BOOST_AUTO_TEST_CASE(testBlackEngineCaching) { + + BOOST_TEST_MESSAGE("Testing swaption result caching in Black engine..."); + + using namespace swaption_test; + + CommonVars vars; + + Date exerciseDate = vars.calendar.advance(vars.today, 1 * Years); + Date startDate = vars.calendar.advance(exerciseDate, vars.settlementDays, Days); + + ext::shared_ptr swap = MakeVanillaSwap(1 * Years, vars.index, 0.03) + .withEffectiveDate(startDate) + .withFixedLegTenor(1 * Years) + .withFixedLegDayCount(vars.fixedDayCount) + .withFloatingLegSpread(0.0) + .withType(Swap::Payer); + ext::shared_ptr swaption = vars.makeSwaption(swap, exerciseDate, 0.12); + + BOOST_CHECK(!swaption->isCalculated()); + + swaption->NPV(); + + BOOST_CHECK(swaption->isCalculated()); +} + BOOST_AUTO_TEST_CASE(testStrikeDependency) { BOOST_TEST_MESSAGE("Testing swaption dependency on strike...");