19#include <boost/test/unit_test.hpp>
20#include <oret/toplevelfixture.hpp>
22#include <boost/make_shared.hpp>
24#include <ql/currencies/america.hpp>
25#include <ql/math/interpolations/linearinterpolation.hpp>
26#include <ql/termstructures/yield/flatforward.hpp>
36using namespace boost::unit_test_framework;
44Real testTolerance = 1e-10;
50 asof_ = Date(19, Feb, 2018);
51 Actual365Fixed dayCounter;
54 Handle<YieldTermStructure> discount(QuantLib::ext::make_shared<FlatForward>(asof_, 0.0, dayCounter));
58 vector<Date> dates = {
asof_, Date(19, Feb, 2019)};
59 vector<Real> prices = {1346.0, 1348.0};
60 Handle<PriceTermStructure> priceCurve(
62 Handle<CommodityIndex> commIdx(QuantLib::ext::make_shared<CommoditySpotIndex>(
"GOLD_USD", NullCalendar(), priceCurve));
69BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
71BOOST_AUTO_TEST_SUITE(CommodityForwardTests)
75 BOOST_TEST_MESSAGE(
"Testing commodity forward trade building");
78 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
79 Settings::instance().evaluationDate() = market->asofDate();
82 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
83 engineData->model(
"CommodityForward") =
"DiscountedCashflows";
84 engineData->engine(
"CommodityForward") =
"DiscountingCommodityForwardEngine";
85 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
88 string position =
"Long";
89 string commodityName =
"GOLD_USD";
90 string currency =
"USD";
91 Real quantity = 100.0;
97 QuantLib::ext::shared_ptr<ore::data::CommodityForward> forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(
98 envelope, position, commodityName, currency, quantity,
maturity, strike);
99 BOOST_CHECK_NO_THROW(forward->build(engineFactory));
102 QuantLib::ext::shared_ptr<Instrument> qlInstrument = forward->instrument()->qlInstrument();
103 QuantLib::ext::shared_ptr<QuantExt::CommodityForward> commodityForward =
104 QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityForward>(qlInstrument);
105 BOOST_CHECK(commodityForward);
106 BOOST_CHECK_EQUAL(commodityForward->position(), Position::Type::Long);
107 BOOST_CHECK_EQUAL(commodityForward->index()->name(),
"COMM-GOLD_USD");
108 BOOST_CHECK_EQUAL(commodityForward->currency(), USDCurrency());
109 BOOST_CHECK_CLOSE(commodityForward->quantity(), 100.0, testTolerance);
110 BOOST_CHECK_EQUAL(commodityForward->maturityDate(), Date(19, Feb, 2019));
111 BOOST_CHECK_CLOSE(commodityForward->strike(), 1340.0, testTolerance);
114 BOOST_CHECK_CLOSE(commodityForward->NPV(), 800.0, testTolerance);
117 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope,
"Short", commodityName, currency, quantity,
119 BOOST_CHECK_NO_THROW(forward->build(engineFactory));
120 qlInstrument = forward->instrument()->qlInstrument();
121 commodityForward = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityForward>(qlInstrument);
122 BOOST_CHECK(commodityForward);
123 BOOST_CHECK_EQUAL(commodityForward->position(), Position::Type::Short);
124 BOOST_CHECK_CLOSE(commodityForward->NPV(), -800.0, testTolerance);
127 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, -quantity,
129 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
132 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, quantity,
134 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
137 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position,
"GOLD", currency, quantity,
maturity,
139 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
144 BOOST_TEST_MESSAGE(
"Testing parsing of commodity forward trade from XML");
148 tradeXml.append(
"<Portfolio>");
149 tradeXml.append(
" <Trade id=\"CommodityForward_WTI_Oct_21\">");
150 tradeXml.append(
" <TradeType>CommodityForward</TradeType>");
151 tradeXml.append(
" <Envelope>");
152 tradeXml.append(
" <CounterParty>CPTY_A</CounterParty>");
153 tradeXml.append(
" <NettingSetId>CPTY_A</NettingSetId>");
154 tradeXml.append(
" <AdditionalFields/>");
155 tradeXml.append(
" </Envelope>");
156 tradeXml.append(
" <CommodityForwardData>");
157 tradeXml.append(
" <Position>Short</Position>");
158 tradeXml.append(
" <Maturity>2021-10-31</Maturity>");
159 tradeXml.append(
" <Name>COMDTY_WTI_USD</Name>");
160 tradeXml.append(
" <Currency>USD</Currency>");
161 tradeXml.append(
" <Strike>49.75</Strike>");
162 tradeXml.append(
" <Quantity>500000</Quantity>");
163 tradeXml.append(
" </CommodityForwardData>");
164 tradeXml.append(
" </Trade>");
165 tradeXml.append(
"</Portfolio>");
172 QuantLib::ext::shared_ptr<Trade> trade = portfolio.
trades().begin()->second;
173 QuantLib::ext::shared_ptr<ore::data::CommodityForward> commodityForward =
174 QuantLib::ext::dynamic_pointer_cast<ore::data::CommodityForward>(trade);
177 BOOST_CHECK(commodityForward);
178 BOOST_CHECK_EQUAL(commodityForward->tradeType(),
"CommodityForward");
179 BOOST_CHECK_EQUAL(commodityForward->id(),
"CommodityForward_WTI_Oct_21");
180 BOOST_CHECK_EQUAL(commodityForward->position(),
"Short");
181 BOOST_CHECK_EQUAL(commodityForward->maturityDate(),
"2021-10-31");
182 BOOST_CHECK_EQUAL(commodityForward->commodityName(),
"COMDTY_WTI_USD");
183 BOOST_CHECK_EQUAL(commodityForward->currency(),
"USD");
184 BOOST_CHECK_CLOSE(commodityForward->strike(), 49.75, testTolerance);
185 BOOST_CHECK_CLOSE(commodityForward->quantity(), 500000.0, testTolerance);
188BOOST_AUTO_TEST_SUITE_END()
190BOOST_AUTO_TEST_SUITE_END()
Engine builder for commodity forward.
Serializable object holding generic trade data, reporting dimensions.
static const string defaultConfiguration
Default configuration label.
map< tuple< string, YieldCurveType, string >, Handle< YieldTermStructure > > yieldCurves_
map< pair< string, string >, QuantLib::Handle< QuantExt::CommodityIndex > > commodityIndices_
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
void fromXMLString(const std::string &xml)
Parse from XML string.
Commodity forward representation.
An implementation of the Market class that stores the required objects in maps.
BOOST_AUTO_TEST_CASE(testCommodityForwardTradeBuilding)