Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fittedbondcurve.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <boost/make_shared.hpp>
20#include <boost/test/unit_test.hpp>
21#include <oret/datapaths.hpp>
22#include <oret/toplevelfixture.hpp>
23
28
29#include <ql/pricingengines/bond/discountingbondengine.hpp>
30#include <ql/termstructures/yield/bondhelpers.hpp>
31#include <ql/termstructures/yield/nonlinearfittingmethods.hpp>
32
33using namespace QuantLib;
34using namespace ore::data;
35
36BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
37
38BOOST_AUTO_TEST_SUITE(FittedBondCurveTests)
39
40BOOST_AUTO_TEST_CASE(testCurveFromFixedRateBonds) {
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); // 1bp tolerance in absolute price
83 }
84}
85
86BOOST_AUTO_TEST_SUITE_END()
87
88BOOST_AUTO_TEST_SUITE_END()
Pricing Engine Factory.
BOOST_AUTO_TEST_CASE(testCurveFromFixedRateBonds)
A market implementation providing curves for setting up bond rate helpers.
Portfolio class.
base trade data model and serialization