20#include <boost/test/unit_test.hpp>
21#include <ql/currencies/america.hpp>
22#include <ql/settings.hpp>
23#include <ql/time/calendars/nullcalendar.hpp>
28using namespace boost::unit_test_framework;
36 QuantLib::ext::shared_ptr<CommodityIndex> index;
38 Position::Type position;
48 currency(USDCurrency()), position(Position::Long), quantity(100), maturity(19, Feb, 2019), strike(50.0) {}
54BOOST_AUTO_TEST_SUITE(CommodityForwardTests)
58 BOOST_TEST_MESSAGE(
"Testing commodity forward constructor");
62 CommodityForward forward(td.index, td.currency, td.position, td.quantity, td.maturity, td.strike);
64 BOOST_CHECK_EQUAL(forward.
index()->name(), td.index->name());
65 BOOST_CHECK_EQUAL(forward.
currency(), td.currency);
66 BOOST_CHECK_EQUAL(forward.
position(), td.position);
67 BOOST_CHECK_EQUAL(forward.
quantity(), td.quantity);
69 BOOST_CHECK_EQUAL(forward.
strike(), td.strike);
74 BOOST_TEST_MESSAGE(
"Testing commodity forward expiry logic");
78 CommodityForward forward(td.index, td.currency, td.position, td.quantity, td.maturity, td.strike);
80 Settings::instance().evaluationDate() = td.maturity - 1 * Days;
81 Settings::instance().includeReferenceDateEvents() =
true;
82 BOOST_CHECK_EQUAL(forward.
isExpired(),
false);
84 Settings::instance().evaluationDate() = td.maturity;
85 BOOST_CHECK_EQUAL(forward.
isExpired(),
false);
87 Settings::instance().includeReferenceDateEvents() =
false;
88 BOOST_CHECK_EQUAL(forward.
isExpired(),
true);
93 BOOST_TEST_MESSAGE(
"Testing commodity forward expiry logic for cash-settled forward" <<
94 " with payment equal to maturity");
98 CommodityForward forward(td.index, td.currency, td.position, td.quantity, td.maturity, td.strike,
false);
100 Settings::instance().evaluationDate() = td.maturity - 1 * Days;
101 Settings::instance().includeReferenceDateEvents() =
true;
102 BOOST_CHECK_EQUAL(forward.
isExpired(),
false);
104 Settings::instance().evaluationDate() = td.maturity;
105 BOOST_CHECK_EQUAL(forward.
isExpired(),
false);
107 Settings::instance().includeReferenceDateEvents() =
false;
108 BOOST_CHECK_EQUAL(forward.
isExpired(),
true);
113 BOOST_TEST_MESSAGE(
"Testing commodity forward expiry logic for cash-settled forward" <<
114 " with payment date strictly greater than maturity date.");
118 Date payment(21, Feb, 2019);
119 CommodityForward forward(td.index, td.currency, td.position, td.quantity, td.maturity, td.strike,
123 Settings::instance().includeReferenceDateEvents() =
true;
124 Date tmpDate = td.maturity - 1 * Days;
125 while (tmpDate <= payment) {
126 Settings::instance().evaluationDate() = tmpDate;
127 BOOST_CHECK_EQUAL(forward.
isExpired(),
false);
132 Settings::instance().includeReferenceDateEvents() =
false;
133 BOOST_CHECK_EQUAL(forward.
isExpired(),
true);
136 Settings::instance().evaluationDate() = payment + 1 * Days;
137 BOOST_CHECK_EQUAL(forward.
isExpired(),
true);
138 Settings::instance().includeReferenceDateEvents() =
true;
139 BOOST_CHECK_EQUAL(forward.
isExpired(),
true);
145 BOOST_TEST_MESSAGE(
"Test that using a negative quantity in the constructor causes an exception");
149 BOOST_CHECK_THROW(
CommodityForward(td.index, td.currency, td.position, -10.0, td.maturity, td.strike),
155 BOOST_TEST_MESSAGE(
"Test that using a negative strike in the constructor causes an exception");
159 BOOST_CHECK_THROW(
CommodityForward(td.index, td.currency, td.position, td.quantity, td.maturity, -50.0),
165 BOOST_TEST_MESSAGE(
"Test that using a payment date less than maturity for cash settled causes an exception");
169 BOOST_CHECK_THROW(
CommodityForward(td.index, td.currency, td.position, td.quantity, td.maturity, -50.0,
170 false, td.maturity - 1 * Days), QuantLib::Error);
175 BOOST_TEST_MESSAGE(
"Test that using a payment date for physically settled causes an exception");
179 BOOST_CHECK_THROW(
CommodityForward(td.index, td.currency, td.position, td.quantity, td.maturity, -50.0,
180 true, td.maturity + 2 * Days), QuantLib::Error);
183BOOST_AUTO_TEST_SUITE_END()
185BOOST_AUTO_TEST_SUITE_END()
bool isExpired() const override
const QuantLib::ext::shared_ptr< CommodityIndex > & index() const
const QuantLib::Date & maturityDate() const
QuantLib::Position::Type position() const
const QuantLib::Currency & currency() const
QuantLib::Real quantity() const
QuantLib::Real strike() const
Instrument representing a commodity forward contract.
BOOST_AUTO_TEST_CASE(testConstructor)
Fixture that can be used at top level.