40 {
41
42 Date asof(6, April, 2020);
43 Settings::instance().evaluationDate() = asof;
44
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"));
48
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"));
52
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);
57
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());
64 BOOST_REQUIRE(bond);
65 bonds.push_back(bond);
66 helpers.push_back(QuantLib::ext::make_shared<BondHelper>(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(100.0)), bond));
67 }
68
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()));
72 auto curve =
73 QuantLib::ext::make_shared<FittedBondDiscountCurve>(asof, helpers, Actual365Fixed(), method, 1E-10, 10000, guess);
74
75 BOOST_TEST_MESSAGE("cost = " << curve->fitResults().minimumCostValue());
76
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);
83 }
84}