73 {
74
75 BOOST_TEST_MESSAGE("Testing commodity forward trade building");
76
77
78 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
79 Settings::instance().evaluationDate() = market->asofDate();
80
81
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);
86
87
88 string position = "Long";
89 string commodityName = "GOLD_USD";
90 string currency = "USD";
91 Real quantity = 100.0;
93 Real strike = 1340.0;
94
95
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));
100
101
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);
112
113
114 BOOST_CHECK_CLOSE(commodityForward->NPV(), 800.0, testTolerance);
115
116
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);
125
126
127 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, -quantity,
129 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
130
131
132 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, quantity,
134 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
135
136
137 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position,
"GOLD", currency, quantity,
maturity,
138 strike);
139 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
140}
Serializable object holding generic trade data, reporting dimensions.