Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
compositetrade.cpp File Reference
#include <ored/portfolio/compositetrade.hpp>
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <oret/datapaths.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/portfolio/builders/equityforward.hpp>
#include <ored/portfolio/builders/equityoption.hpp>
#include <ored/portfolio/equityforward.hpp>
#include <ored/portfolio/equityoption.hpp>
#include <oret/toplevelfixture.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <qle/indexes/equityindex.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testSyntheticForward)
 
 BOOST_AUTO_TEST_CASE (testMultiCcyComposite)
 
 BOOST_AUTO_TEST_CASE (testCompositeReferenceData)
 
 BOOST_AUTO_TEST_CASE (testConstructionWithCompositeTradeReferenceData)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( testSyntheticForward  )

Definition at line 108 of file compositetrade.cpp.

108 {
109 BOOST_TEST_MESSAGE("Testing SyntheticForwardTrade...");
110
111 SavedSettings backup;
112
113 InstrumentConventions::instance().setConventions(QuantLib::ext::make_shared<Conventions>());
114
115 // build market
116 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
117 Settings::instance().evaluationDate() = market->asofDate();
118 Date expiry = market->asofDate() + 6 * Months + 1 * Days;
119 ostringstream o;
120 o << QuantLib::io::iso_date(expiry);
121 string exp_str = o.str();
122
123 // build EquityOption - expiry in 1 Year
124 OptionData callData("Long", "Call", "European", true, vector<string>(1, exp_str));
125 OptionData putData("Short", "Put", "European", true, vector<string>(1, exp_str));
126 Envelope env("CP1");
127 TradeStrike tradeStrike(95.0, "EUR");
128 QuantLib::ext::shared_ptr<Trade> eqCall =
129 QuantLib::ext::make_shared<EquityOption>(env, callData, EquityUnderlying("eurCorp"), "EUR", 1.0, tradeStrike);
130 eqCall->id() = "Long Call";
131 QuantLib::ext::shared_ptr<Trade> eqPut =
132 QuantLib::ext::make_shared<EquityOption>(env, putData, EquityUnderlying("eurCorp"), "EUR", 1.0, tradeStrike);
133 eqPut->id() = "Short Put";
134 CompositeTrade syntheticForward("EUR", {eqCall, eqPut}, "Mean", 0.0, env);
135 syntheticForward.id() = "Synthetic Forward Test";
136 ore::data::EquityForward eqFwd(env, "Long", EquityUnderlying("eurCorp"), "EUR", 1.0, exp_str, 95.0);
137
138 // Build and price
139 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
140 engineData->model("EquityOption") = "BlackScholesMerton";
141 engineData->engine("EquityOption") = "AnalyticEuropeanEngine";
142 engineData->model("EquityForward") = "DiscountedCashflows";
143 engineData->engine("EquityForward") = "DiscountingEquityForwardEngine";
144 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
145
146 syntheticForward.build(engineFactory);
147 eqFwd.build(engineFactory);
148
149 Real npv_composite = syntheticForward.instrument()->NPV();
150 Real npv_fwd = eqFwd.instrument()->NPV();
151
152 BOOST_CHECK_CLOSE(npv_composite, npv_fwd, 0.01);
153 BOOST_CHECK_CLOSE(syntheticForward.notional(), eqFwd.notional(), 0.01);
154}
Composite Trade class.
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding option data.
Definition: optiondata.hpp:42
string & id()
Set the trade id.
Definition: trade.hpp:118
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( testMultiCcyComposite  )

Definition at line 157 of file compositetrade.cpp.

157 {
158 BOOST_TEST_MESSAGE("Testing SyntheticForwardTrade...");
159
160 SavedSettings backup;
161
162 InstrumentConventions::instance().setConventions(QuantLib::ext::make_shared<Conventions>());
163
164 // build market
165 QuantLib::ext::shared_ptr<SimpleQuote> eurusdRate(QuantLib::ext::make_shared<SimpleQuote>(1.2));
166 map<string, Handle<Quote>> fxRates = {{"EURUSD", Handle<Quote>(eurusdRate)}};
167 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>(fxRates);
168 Settings::instance().evaluationDate() = market->asofDate();
169 Date expiry = market->asofDate() + 6 * Months + 1 * Days;
170 ostringstream o;
171 o << QuantLib::io::iso_date(expiry);
172 string exp_str = o.str();
173
174 // build EquityOption - expiry in 1 Year
175 OptionData callData("Long", "Call", "European", true, vector<string>(1, exp_str));
176 Envelope env("CP1");
177 TradeStrike tradeStrike_EUR(95.0, "EUR");
178 QuantLib::ext::shared_ptr<Trade> eurCall =
179 QuantLib::ext::make_shared<EquityOption>(env, callData, EquityUnderlying("eurCorp"), "EUR", 1.0, tradeStrike_EUR);
180 eurCall->id() = "EUR Call";
181 TradeStrike tradeStrike_USD(95.0, "USD");
182 QuantLib::ext::shared_ptr<Trade> usdCall =
183 QuantLib::ext::make_shared<EquityOption>(env, callData, EquityUnderlying("usdCorp"), "USD", 1.0, tradeStrike_USD);
184 usdCall->id() = "USD Call";
185 CompositeTrade eurComp("EUR", {eurCall, usdCall}, "Sum", 0.0, env);
186 CompositeTrade usdComp("USD", {eurCall, usdCall}, "Sum", 0.0, env);
187 eurComp.id() = "EUR Combo Call Test";
188 usdComp.id() = "USD Combo Call Test";
189
190 // Build and price
191 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
192 engineData->model("EquityOption") = "BlackScholesMerton";
193 engineData->engine("EquityOption") = "AnalyticEuropeanEngine";
194 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
195
196 eurComp.build(engineFactory);
197 usdComp.build(engineFactory);
198
199 Real npv_eur_composite = eurComp.instrument()->NPV();
200 Real npv_usd_composite = usdComp.instrument()->NPV();
201 Real npv_eurCall = eurCall->instrument()->NPV();
202 Real npv_usdCall = usdCall->instrument()->NPV();
203
204 BOOST_CHECK_CLOSE(npv_eur_composite, npv_eurCall * 1.0 + npv_usdCall / 1.2, 0.01);
205 BOOST_CHECK_CLOSE(npv_usd_composite, npv_eurCall * 1.2 + npv_usdCall / 1.0, 0.01);
206 // Check that the notional is calulated correctly.
207 BOOST_CHECK_CLOSE(usdComp.notional(), eurCall->notional() * 2.2, 0.01);
208
209 // let's bump the fx to check that observation is working
210 auto testMarket = QuantLib::ext::dynamic_pointer_cast<TestMarket>(market);
211 eurusdRate->setValue(1.25);
212 npv_usd_composite = usdComp.instrument()->NPV();
213 BOOST_CHECK_CLOSE(npv_usd_composite, npv_eurCall * 1.25 + npv_usdCall / 1.0, 0.01);
214}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( testCompositeReferenceData  )

Definition at line 216 of file compositetrade.cpp.

216 {
217 BOOST_TEST_MESSAGE("Testing Composite Trade with and w/o reference data...");
218
219 SavedSettings backup;
220
221 InstrumentConventions::instance().setConventions(QuantLib::ext::make_shared<Conventions>());
222
223 // build CompositeTrade with referencedata
224 auto rdm = QuantLib::ext::make_shared<BasicReferenceDataManager>(TEST_INPUT_FILE("reference_data.xml"));
225 auto ptfReferenceDatum = QuantLib::ext::dynamic_pointer_cast<PortfolioBasketReferenceDatum>(rdm->getData("PortfolioBasket", "MSFDSJP"));
226 auto refData = ptfReferenceDatum->getTrades();
227 QuantLib::ext::shared_ptr<Trade> eqRefCall = refData[0];
228 QuantLib::ext::shared_ptr<Trade> eqRefPut = refData[1];
229
230 Envelope env("CP1");
231 CompositeTrade RefData("EUR", {eqRefCall, eqRefPut}, "Mean", 0.0, env);
232 RefData.id() = "Reference Data Test";
233
234 // build market
235 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
236 Settings::instance().evaluationDate() = market->asofDate();
237 Date expiry = market->asofDate() + 6 * Months + 1 * Days;
238 ostringstream o;
239 o << QuantLib::io::iso_date(expiry);
240 string exp_str = o.str();
241
242 // build CompositeTrade without referencedata
243 OptionData callData("Long", "Call", "European", true, vector<string>(1, exp_str));
244 OptionData putData("Short", "Put", "European", true, vector<string>(1, exp_str));
245 //Envelope env("CP1");
246 TradeStrike tradeStrike(95.0, "EUR");
247 QuantLib::ext::shared_ptr<Trade> eqCall =
248 QuantLib::ext::make_shared<EquityOption>(env, callData, EquityUnderlying("eurCorp"), "EUR", 1.0, tradeStrike);
249 eqCall->id() = "Long Call";
250 QuantLib::ext::shared_ptr<Trade> eqPut =
251 QuantLib::ext::make_shared<EquityOption>(env, putData, EquityUnderlying("eurCorp"), "EUR", 1.0, tradeStrike);
252 eqPut->id() = "Short Put";
253 CompositeTrade noRefData("EUR", {eqCall, eqPut}, "Mean", 0.0, env);
254 noRefData.id() = "No Reference Data Test";
255
256 // Build and price
257 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
258 engineData->model("EquityOption") = "BlackScholesMerton";
259 engineData->engine("EquityOption") = "AnalyticEuropeanEngine";
260 QuantLib::ext::shared_ptr<EngineFactory> engineFactory =
261 QuantLib::ext::make_shared<EngineFactory>(engineData, market);
262
263 noRefData.build(engineFactory);
264 RefData.build(engineFactory);
265
266 Real npv_composite_NoRefData = noRefData.instrument()->NPV();
267 Real npv_composite_RefData = RefData.instrument()->NPV();
268
269 BOOST_CHECK_CLOSE(npv_composite_NoRefData, npv_composite_RefData, 0.01);
270 BOOST_CHECK_CLOSE(noRefData.notional(), RefData.notional(), 0.01);
271}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( testConstructionWithCompositeTradeReferenceData  )

Definition at line 273 of file compositetrade.cpp.

273 {
274
275 // CompoiteTrade with Reference Data
276 auto rdm = QuantLib::ext::make_shared<BasicReferenceDataManager>(TEST_INPUT_FILE("reference_data.xml"));
277 auto ptfReferenceDatum =
278 QuantLib::ext::dynamic_pointer_cast<PortfolioBasketReferenceDatum>(rdm->getData("PortfolioBasket", "MSFDSJP"));
279
280 string xmlRefData = ptfReferenceDatum->toXMLString();
281 PortfolioBasketReferenceDatum xmlPortfolioBasket("MSFDSJP");
282 xmlPortfolioBasket.fromXMLString(xmlRefData);
283
284 BOOST_CHECK_EQUAL(ptfReferenceDatum->id(), xmlPortfolioBasket.id());
285 BOOST_CHECK_EQUAL(ptfReferenceDatum->getTrades()[0]->notional(), xmlPortfolioBasket.getTrades()[0]->notional());
286 BOOST_CHECK_EQUAL(ptfReferenceDatum->getTrades()[1]->notional(), xmlPortfolioBasket.getTrades()[1]->notional());
287 BOOST_CHECK_EQUAL(ptfReferenceDatum->getTrades()[0]->id(), xmlPortfolioBasket.getTrades()[0]->id());
288 BOOST_CHECK_EQUAL(ptfReferenceDatum->getTrades()[1]->id(), xmlPortfolioBasket.getTrades()[1]->id());
289
290 auto refData = ptfReferenceDatum->getTrades();
291 QuantLib::ext::shared_ptr<Trade> eqRefCall = refData[0];
292 QuantLib::ext::shared_ptr<Trade> eqRefPut = refData[1];
293
294 Envelope env("CP1");
295 CompositeTrade compRefData("EUR", {eqRefCall, eqRefPut}, "Mean", 0.0, env);
296
297 // Use toXml to serialise to string
298 string xmlStr = compRefData.toXMLString();
299 CompositeTrade xmlComposite;
300 xmlComposite.fromXMLString(xmlStr);
301
302 BOOST_CHECK_EQUAL(compRefData.id(), xmlComposite.id());
303 BOOST_CHECK_EQUAL(compRefData.currency(), xmlComposite.currency());
304 BOOST_CHECK_EQUAL(compRefData.notionalCalculation(), xmlComposite.notionalCalculation());
305 BOOST_CHECK_EQUAL(compRefData.trades()[0]->tradeType(), xmlComposite.trades()[0]->tradeType());
306 BOOST_CHECK_EQUAL(compRefData.trades()[0]->notional(), xmlComposite.trades()[0]->notional());
307 BOOST_CHECK_EQUAL(compRefData.trades()[1]->tradeType(), xmlComposite.trades()[1]->tradeType());
308 BOOST_CHECK_EQUAL(compRefData.trades()[1]->notional(), xmlComposite.trades()[1]->notional());
309
310
311}
const string & currency() const
const vector< QuantLib::ext::shared_ptr< Trade > > & trades() const
const string & notionalCalculation() const
std::string toXMLString() const
Parse from XML string.
Definition: xmlutils.cpp:168
void fromXMLString(const std::string &xml)
Parse from XML string.
Definition: xmlutils.cpp:162
+ Here is the call graph for this function: