19#include <boost/make_shared.hpp>
20#include <boost/test/unit_test.hpp>
21#include <oret/datapaths.hpp>
22#include <oret/toplevelfixture.hpp>
29#include <ql/pricingengines/bond/discountingbondengine.hpp>
30#include <ql/termstructures/yield/bondhelpers.hpp>
31#include <ql/termstructures/yield/nonlinearfittingmethods.hpp>
36BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
38BOOST_AUTO_TEST_SUITE(FittedBondCurveTests)
42 Date asof(6, April, 2020);
43 Settings::instance().evaluationDate() = asof;
45 BOOST_TEST_MESSAGE(
"read pricing engine config");
46 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
47 engineData->fromFile(TEST_INPUT_FILE(
"pricingengine.xml"));
49 BOOST_TEST_MESSAGE(
"read portfolio of bonds");
50 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
51 portfolio->fromFile(TEST_INPUT_FILE(
"portfolio1.xml"));
53 BOOST_TEST_MESSAGE(
"build portfolio against FittedBondCurveHelperMarket");
54 QuantLib::ext::shared_ptr < EngineFactory> engineFactory =
55 QuantLib::ext::make_shared<EngineFactory>(engineData, QuantLib::ext::make_shared<FittedBondCurveHelperMarket>());
56 portfolio->build(engineFactory);
58 BOOST_TEST_MESSAGE(
"set up bond helpers");
59 std::vector<QuantLib::ext::shared_ptr<Bond>> bonds;
60 std::vector<QuantLib::ext::shared_ptr<BondHelper>> helpers;
61 for (
auto const& [tradeId, trade] : portfolio->trades()) {
62 QuantLib::ext::shared_ptr<QuantLib::Bond> bond =
63 QuantLib::ext::dynamic_pointer_cast<Bond>(trade->instrument()->qlInstrument());
65 bonds.push_back(bond);
66 helpers.push_back(QuantLib::ext::make_shared<BondHelper>(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(100.0)), bond));
69 BOOST_TEST_MESSAGE(
"build fitted bond curve");
70 Array guess({0.03, 0.03, 0.03, 0.5});
71 NelsonSiegelFitting method((Array(), QuantLib::ext::shared_ptr<OptimizationMethod>(), Array()));
73 QuantLib::ext::make_shared<FittedBondDiscountCurve>(asof, helpers, Actual365Fixed(), method, 1E-10, 10000, guess);
75 BOOST_TEST_MESSAGE(
"cost = " << curve->fitResults().minimumCostValue());
77 auto engine = QuantLib::ext::make_shared<DiscountingBondEngine>(Handle<YieldTermStructure>(curve));
78 for (
auto const& b : bonds) {
79 b->setPricingEngine(engine);
80 BOOST_TEST_MESSAGE(
"bond helper maturity " << b->maturityDate() <<
" has clean price " << b->cleanPrice()
81 <<
" discount factor is " << curve->discount(b->maturityDate()));
82 BOOST_CHECK_CLOSE(b->cleanPrice(), 100.0, 0.01);
86BOOST_AUTO_TEST_SUITE_END()
88BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testCurveFromFixedRateBonds)
A market implementation providing curves for setting up bond rate helpers.
base trade data model and serialization