diff --git a/ql/instruments/overnightindexedswap.cpp b/ql/instruments/overnightindexedswap.cpp index 1554d4bf47..949fe31846 100644 --- a/ql/instruments/overnightindexedswap.cpp +++ b/ql/instruments/overnightindexedswap.cpp @@ -136,6 +136,8 @@ namespace QuantLib { floatingSchedule().calendar() : paymentCalendar) .withAveragingMethod(averagingMethod_); + for (const auto& c : legs_[1]) + registerWith(c); } void OvernightIndexedSwap::setupFloatingArguments(arguments* args) const { diff --git a/ql/instruments/vanillaswap.cpp b/ql/instruments/vanillaswap.cpp index 6bddb48b9f..f7ee29eb21 100644 --- a/ql/instruments/vanillaswap.cpp +++ b/ql/instruments/vanillaswap.cpp @@ -48,8 +48,8 @@ namespace QuantLib { .withPaymentAdjustment(this->paymentConvention()) .withSpreads(this->spread()) .withIndexedCoupons(useIndexedCoupons); - for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i) - registerWith(*i); + for (const auto& c : legs_[1]) + registerWith(c); } void VanillaSwap::setupFloatingArguments(arguments* args) const { diff --git a/test-suite/overnightindexedswap.cpp b/test-suite/overnightindexedswap.cpp index d27d77c58a..9f8cb26af7 100644 --- a/test-suite/overnightindexedswap.cpp +++ b/test-suite/overnightindexedswap.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -631,6 +632,47 @@ BOOST_AUTO_TEST_CASE(testConstructorsAndNominals) { BOOST_CHECK_EQUAL(ois_4.overnightNominals()[3], nominal/2); } +BOOST_AUTO_TEST_CASE(testNotifications) { + BOOST_TEST_MESSAGE("Testing cash-flow notifications for OIS..."); + + CommonVars vars; + + Date spot = vars.calendar.advance(vars.today, 2*Days); + Real nominal = 100000.0; + + Schedule schedule = MakeSchedule() + .from(spot) + .to(vars.calendar.advance(spot, 2*Years)) + .withCalendar(vars.calendar) + .withFrequency(Annual); + + RelinkableHandle forecast_handle; + forecast_handle.linkTo(flatRate(0.02, Actual360())); + + RelinkableHandle discount_handle; + discount_handle.linkTo(flatRate(0.02, Actual360())); + + auto index = ext::make_shared(forecast_handle); + + auto ois = ext::make_shared(Swap::Payer, + nominal, + schedule, + 0.03, + Actual360(), + index); + ois->setPricingEngine(ext::make_shared(discount_handle)); + ois->NPV(); + + Flag flag; + flag.registerWith(ois); + flag.lower(); + + forecast_handle.linkTo(flatRate(0.03, Actual360())); + + if (!flag.isUp()) + BOOST_FAIL("OIS was not notified of curve change"); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/test-suite/swap.cpp b/test-suite/swap.cpp index b46259a346..e9b6ae9077 100644 --- a/test-suite/swap.cpp +++ b/test-suite/swap.cpp @@ -326,6 +326,50 @@ BOOST_AUTO_TEST_CASE(testThirdWednesdayAdjustment) { } } +BOOST_AUTO_TEST_CASE(testNotifications) { + BOOST_TEST_MESSAGE("Testing cash-flow notifications for vanilla swap..."); + + CommonVars vars; + + Date spot = vars.calendar.advance(vars.today, 2*Days); + Real nominal = 100000.0; + + Schedule schedule = MakeSchedule() + .from(spot) + .to(vars.calendar.advance(spot, 2*Years)) + .withCalendar(vars.calendar) + .withFrequency(Semiannual); + + RelinkableHandle forecast_handle; + forecast_handle.linkTo(flatRate(0.02, Actual365Fixed())); + + RelinkableHandle discount_handle; + discount_handle.linkTo(flatRate(0.02, Actual365Fixed())); + + auto index = ext::make_shared(forecast_handle); + + auto swap = ext::make_shared(Swap::Payer, + nominal, + schedule, + 0.03, + Actual365Fixed(), + schedule, + index, + 0.0, + Actual365Fixed()); + swap->setPricingEngine(ext::make_shared(discount_handle)); + swap->NPV(); + + Flag flag; + flag.registerWith(swap); + flag.lower(); + + forecast_handle.linkTo(flatRate(0.03, Actual365Fixed())); + + if (!flag.isUp()) + BOOST_FAIL("swap was not notified of curve change"); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()