20#include <boost/make_shared.hpp>
21#include <boost/test/unit_test.hpp>
22#include <ql/currencies/america.hpp>
23#include <ql/settings.hpp>
24#include <ql/termstructures/yield/discountcurve.hpp>
30using namespace boost::unit_test_framework;
36BOOST_AUTO_TEST_SUITE(DiscountingCommodityForwardEngineTest)
40 BOOST_TEST_MESSAGE(
"Testing discounting commodity forward engine pricing");
45 Real tolerance = 1e-10;
48 Date asof(19, Feb, 2018);
49 string name(
"GOLD_USD");
51 Date maturity(19, Feb, 2019);
54 Actual365Fixed dayCounter;
57 vector<Date> dates(2);
58 vector<DiscountFactor> dfs(2);
67 vector<Real> prices(2);
70 Handle<PriceTermStructure> priceCurve(
74 QuantLib::ext::shared_ptr<DiscountingCommodityForwardEngine> engine =
75 QuantLib::ext::make_shared<DiscountingCommodityForwardEngine>(discountCurve);
78 Settings::instance().evaluationDate() = asof;
81 QuantLib::ext::shared_ptr<CommodityIndex> index = QuantLib::ext::make_shared<CommoditySpotIndex>(
82 name, NullCalendar(), priceCurve);
83 Position::Type position = Position::Long;
86 QuantLib::ext::shared_ptr<CommodityForward> forward =
87 QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
88 forward->setPricingEngine(engine);
89 BOOST_CHECK_CLOSE(forward->NPV(), quantity * (prices[1] - strike) * dfs[1], tolerance);
92 position = Position::Short;
93 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
94 forward->setPricingEngine(engine);
95 BOOST_CHECK_CLOSE(forward->NPV(), -quantity * (prices[1] - strike) * dfs[1], tolerance);
99 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
100 forward->setPricingEngine(engine);
101 BOOST_CHECK_CLOSE(forward->NPV(), -quantity * (prices[1] - strike) * dfs[1], tolerance);
105 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
106 forward->setPricingEngine(engine);
107 BOOST_CHECK_CLOSE(forward->NPV(), -quantity * (prices[1] - strike) * dfs[1], tolerance);
110 maturity = Date(19, Aug, 2018);
111 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
112 forward->setPricingEngine(engine);
113 BOOST_CHECK_CLOSE(forward->NPV(),
114 -quantity * (priceCurve->price(maturity) - strike) * discountCurve->discount(maturity),
118 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, asof, strike);
120 forward->setPricingEngine(engine);
121 BOOST_CHECK_CLOSE(forward->NPV(), 0.0, tolerance);
123 Settings::instance().includeReferenceDateEvents() =
true;
124 forward->recalculate();
125 BOOST_CHECK_CLOSE(forward->NPV(), -quantity * (prices[0] - strike), tolerance);
128 engine = QuantLib::ext::make_shared<DiscountingCommodityForwardEngine>(discountCurve,
false);
129 forward->setPricingEngine(engine);
130 BOOST_CHECK_CLOSE(forward->NPV(), 0.0, tolerance);
132 Settings::instance().includeReferenceDateEvents() =
false;
133 engine = QuantLib::ext::make_shared<DiscountingCommodityForwardEngine>(discountCurve,
true);
134 forward->setPricingEngine(engine);
135 BOOST_CHECK_CLOSE(forward->NPV(), 0.0, tolerance);
138 maturity = Date(19, Feb, 2019);
139 forward = QuantLib::ext::make_shared<CommodityForward>(index, currency, position, quantity, maturity, strike);
140 Date npvDate = asof + 2 * Days;
141 engine = QuantLib::ext::make_shared<DiscountingCommodityForwardEngine>(discountCurve, boost::none, npvDate);
142 forward->setPricingEngine(engine);
143 DiscountFactor npvDateDiscount = discountCurve->discount(npvDate);
144 BOOST_CHECK_CLOSE(forward->NPV(), -quantity * (prices[1] - strike) * dfs[1] / npvDateDiscount, tolerance);
147BOOST_AUTO_TEST_SUITE_END()
149BOOST_AUTO_TEST_SUITE_END()
InterpolatedDiscountCurve based on loglinear interpolation of DiscountFactors.
Interpolated price curve.
Engine to value a commodity forward contract.
Interpolated price curve.
BOOST_AUTO_TEST_CASE(testPricing)
Fixture that can be used at top level.