Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
ored_commodityforward.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <oret/toplevelfixture.hpp>
#include <boost/make_shared.hpp>
#include <ql/currencies/america.hpp>
#include <ql/math/interpolations/linearinterpolation.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <qle/termstructures/pricecurve.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/portfolio/builders/commodityforward.hpp>
#include <ored/portfolio/commodityforward.hpp>
#include <ored/portfolio/portfolio.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testCommodityForwardTradeBuilding)
 
 BOOST_AUTO_TEST_CASE (testCommodityForwardFromXml)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( testCommodityForwardTradeBuilding  )

Definition at line 73 of file ored_commodityforward.cpp.

73 {
74
75 BOOST_TEST_MESSAGE("Testing commodity forward trade building");
76
77 // Create market
78 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
79 Settings::instance().evaluationDate() = market->asofDate();
80
81 // Create engine factory
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 // Base commodity values
88 string position = "Long";
89 string commodityName = "GOLD_USD";
90 string currency = "USD";
91 Real quantity = 100.0;
92 string maturity = "2019-02-19";
93 Real strike = 1340.0;
94
95 // Test the building of a commodity forward doesn't throw
96 Envelope envelope;
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 // Check the instrument was built as expected
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 // Check the price (simple because DF = 1.0, 100 * (1348 - 1340))
114 BOOST_CHECK_CLOSE(commodityForward->NPV(), 800.0, testTolerance);
115
116 // Check short
117 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, "Short", commodityName, currency, quantity,
118 maturity, strike);
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 // Check that negative quantity throws an error
127 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, -quantity,
128 maturity, strike);
129 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
130
131 // Check that negative strike throws an error
132 forward = QuantLib::ext::make_shared<ore::data::CommodityForward>(envelope, position, commodityName, currency, quantity,
133 maturity, -strike);
134 BOOST_CHECK_THROW(forward->build(engineFactory), Error);
135
136 // Check that build fails when commodity name does not match that in the market
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.
Definition: envelope.hpp:51
Time maturity
Definition: utilities.cpp:66

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( testCommodityForwardFromXml  )

Definition at line 142 of file ored_commodityforward.cpp.

142 {
143
144 BOOST_TEST_MESSAGE("Testing parsing of commodity forward trade from XML");
145
146 // Create an XML string representation of the trade
147 string tradeXml;
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>");
166
167 // Load portfolio from XML string
168 Portfolio portfolio;
169 portfolio.fromXMLString(tradeXml);
170
171 // Extract CommodityForward trade from 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);
175
176 // Check fields after checking that the cast was successful
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);
186}
Serializable portfolio.
Definition: portfolio.hpp:43
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Definition: portfolio.cpp:162
void fromXMLString(const std::string &xml)
Parse from XML string.
Definition: xmlutils.cpp:162
+ Here is the call graph for this function: