160 {
161
162 BOOST_TEST_MESSAGE("Testing parsing of commodity option trade from XML");
163
164
165 CommonData td;
166
167
168 string tradeXml;
169 tradeXml.append("<Portfolio>");
170 tradeXml.append(" <Trade id=\"CommodityOption_Gold\">");
171 tradeXml.append(" <TradeType>CommodityOption</TradeType>");
172 tradeXml.append(" <Envelope>");
173 tradeXml.append(" <CounterParty>CPTY_A</CounterParty>");
174 tradeXml.append(" <NettingSetId>CPTY_A</NettingSetId>");
175 tradeXml.append(" <AdditionalFields/>");
176 tradeXml.append(" </Envelope>");
177 tradeXml.append(" <CommodityOptionData>");
178 tradeXml.append(" <OptionData>");
179 tradeXml.append(" <LongShort>Long</LongShort>");
180 tradeXml.append(" <OptionType>Call</OptionType>");
181 tradeXml.append(" <Style>European</Style>");
182 tradeXml.append(" <Settlement>Cash</Settlement>");
183 tradeXml.append(" <PayOffAtExpiry>false</PayOffAtExpiry>");
184 tradeXml.append(" <ExerciseDates>");
185 tradeXml.append(" <ExerciseDate>2019-02-19</ExerciseDate>");
186 tradeXml.append(" </ExerciseDates>");
187 tradeXml.append(" </OptionData>");
188 tradeXml.append(" <Name>GOLD_USD</Name>");
189 tradeXml.append(" <Currency>USD</Currency>");
190 tradeXml.append(" <Strike>1340</Strike>");
191 tradeXml.append(" <Quantity>100</Quantity>");
192 tradeXml.append(" </CommodityOptionData>");
193 tradeXml.append(" </Trade>");
194 tradeXml.append("</Portfolio>");
195
196
199
200
201 QuantLib::ext::shared_ptr<Trade> trade = portfolio.
trades().begin()->second;
202 QuantLib::ext::shared_ptr<CommodityOption> option = QuantLib::ext::dynamic_pointer_cast<ore::data::CommodityOption>(trade);
203
204
205 BOOST_CHECK(option);
206 BOOST_CHECK_EQUAL(option->tradeType(), "CommodityOption");
207 BOOST_CHECK_EQUAL(option->id(), "CommodityOption_Gold");
208 BOOST_CHECK_EQUAL(option->asset(), "GOLD_USD");
209 BOOST_CHECK_EQUAL(option->currency(), "USD");
210 BOOST_CHECK_CLOSE(option->strike().value(), 1340, testTolerance);
211 BOOST_CHECK_CLOSE(option->quantity(), 100, testTolerance);
212 BOOST_CHECK_EQUAL(option->option().longShort(), "Long");
213 BOOST_CHECK_EQUAL(option->option().callPut(), "Call");
214 BOOST_CHECK_EQUAL(option->option().style(), "European");
215 BOOST_CHECK_EQUAL(option->option().exerciseDates().size(), 1);
216 BOOST_CHECK_EQUAL(option->option().exerciseDates()[0], "2019-02-19");
217
218
219 BOOST_CHECK_NO_THROW(option->build(td.engineFactory));
220 BOOST_CHECK_CLOSE(option->instrument()->NPV(), cachedCallPrice, testTolerance);
221
222
223 replace_all(tradeXml, ">Long<", ">Short<");
226 trade = portfolio.
trades().begin()->second;
227 BOOST_CHECK_NO_THROW(trade->build(td.engineFactory));
228 BOOST_CHECK_CLOSE(trade->instrument()->NPV(), -cachedCallPrice, testTolerance);
229
230
231 replace_all(tradeXml, ">Call<", ">Put<");
234 trade = portfolio.
trades().begin()->second;
235 BOOST_CHECK_NO_THROW(trade->build(td.engineFactory));
236 BOOST_CHECK_CLOSE(trade->instrument()->NPV(), -cachedPutPrice, testTolerance);
237
238
239 replace_all(tradeXml, ">Short<", ">Long<");
242 trade = portfolio.
trades().begin()->second;
243 BOOST_CHECK_NO_THROW(trade->build(td.engineFactory));
244 BOOST_CHECK_CLOSE(trade->instrument()->NPV(), cachedPutPrice, testTolerance);
245}
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
void clear()
Clear the portfolio.
void fromXMLString(const std::string &xml)
Parse from XML string.