Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
compositewrapper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <boost/make_shared.hpp>
20#include <boost/test/unit_test.hpp>
41#include <oret/datapaths.hpp>
42#include <oret/toplevelfixture.hpp>
43
44using namespace QuantLib;
45using namespace boost::unit_test_framework;
46using namespace std;
47using namespace ore::data;
48
49BOOST_FIXTURE_TEST_SUITE(OREPlusEquityFXTestSuite, ore::test::TopLevelFixture)
50
51BOOST_AUTO_TEST_SUITE(CompositeWrapperTest)
52
53namespace {
54template <class T> void loadFromXMLString(T& t, const std::string& str) {
56 doc.fromXMLString(str);
57 t.fromXML(doc.getFirstNode(""));
58}
59
60struct CommonVars {
61
62 CommonVars()
63 : asof(Date(5, Feb, 2016)), baseCurrency("EUR"),
64 portfolio(XMLDocument(TEST_INPUT_FILE("portfolio.xml")).toString()),
65 conventions(XMLDocument(TEST_INPUT_FILE("conventions.xml")).toString()),
66 todaysMarketConfig(XMLDocument(TEST_INPUT_FILE("todaysmarket.xml")).toString()),
67 pricingEngineConfig(XMLDocument(TEST_INPUT_FILE("pricingengine.xml")).toString()),
68 curveConfig(XMLDocument(TEST_INPUT_FILE("curveconfig.xml")).toString()),
69 loader(QuantLib::ext::make_shared<CSVLoader>(TEST_INPUT_FILE("market.csv"), TEST_INPUT_FILE("fixings.csv"), "")) {
70
71 Settings::instance().evaluationDate() = asof;
72 }
73
74 Date asof;
75 string baseCurrency;
76 string portfolio;
77 string conventions;
78 string todaysMarketConfig;
79 string pricingEngineConfig;
80 string curveConfig;
81 QuantLib::ext::shared_ptr<Loader> loader;
82 SavedSettings savedSettings;
83};
84
85} // namespace
86
87// Common variables used in the tests below
88
89BOOST_AUTO_TEST_CASE(testCompositeInstrumentWrapperPrice) {
90 CommonVars vars;
91 QuantLib::ext::shared_ptr<CurveConfigurations> curveConfig = QuantLib::ext::make_shared<CurveConfigurations>();
92 QuantLib::ext::shared_ptr<Conventions> conventions = QuantLib::ext::make_shared<Conventions>();
93 QuantLib::ext::shared_ptr<TodaysMarketParameters> todaysMarketConfig = QuantLib::ext::make_shared<TodaysMarketParameters>();
94 QuantLib::ext::shared_ptr<EngineData> pricingEngineConfig = QuantLib::ext::make_shared<EngineData>();
95 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
96
97 loadFromXMLString(*curveConfig, vars.curveConfig);
98 loadFromXMLString(*conventions, vars.conventions);
99 InstrumentConventions::instance().setConventions(conventions);
100
101 loadFromXMLString(*todaysMarketConfig, vars.todaysMarketConfig);
102 loadFromXMLString(*pricingEngineConfig, vars.pricingEngineConfig);
103
104 portfolio->fromXMLString(vars.portfolio);
105
106 QuantLib::ext::shared_ptr<Market> market =
107 QuantLib::ext::make_shared<TodaysMarket>(vars.asof, todaysMarketConfig, vars.loader, curveConfig, true);
108 auto configurations = std::map<MarketContext, string>();
109 QuantLib::ext::shared_ptr<EngineFactory> factory =
110 QuantLib::ext::make_shared<EngineFactory>(pricingEngineConfig, market, configurations);
111
112 BOOST_TEST_MESSAGE("number trades " << portfolio->size());
113 portfolio->build(factory);
114 BOOST_TEST_MESSAGE("number trades " << portfolio->size());
115
116 Handle<Quote> fx = Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
117 vector<QuantLib::ext::shared_ptr<InstrumentWrapper>> iw;
118 std::vector<Handle<Quote>> fxRates;
119
120 Real totalNPV = 0;
121 for (const auto& [tradeId, trade] : portfolio->trades()) {
122 Handle<Quote> fx = Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
123 if (trade->npvCurrency() != "USD")
124 fx = factory->market()->fxRate(trade->npvCurrency() + "USD");
125 fxRates.push_back(fx);
126 iw.push_back(trade->instrument());
127 BOOST_TEST_MESSAGE("NPV " << iw.back()->NPV());
128 BOOST_TEST_MESSAGE("FX " << fxRates.back()->value());
129
130 totalNPV += iw.back()->NPV() * fxRates.back()->value();
131 }
132 QuantLib::ext::shared_ptr<InstrumentWrapper> instrument =
133 QuantLib::ext::shared_ptr<InstrumentWrapper>(new ore::data::CompositeInstrumentWrapper(iw, fxRates, vars.asof));
134
135 BOOST_TEST_MESSAGE(instrument->NPV());
136
137 BOOST_CHECK_CLOSE(instrument->NPV(), totalNPV, 0.01);
138}
139
140BOOST_AUTO_TEST_SUITE_END()
141
142BOOST_AUTO_TEST_SUITE_END()
Utility class for loading market quotes and fixings from a file.
Definition: csvloader.hpp:41
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
void fromXMLString(const string &xmlString)
load a document from a hard-coded string
Definition: xmlutils.cpp:103
XMLNode * getFirstNode(const string &name) const
Definition: xmlutils.cpp:116
used to store multiple trade wrappers
BOOST_AUTO_TEST_CASE(testCompositeInstrumentWrapperPrice)
Currency and instrument specific conventions/defaults.
Market Datum Loader Implementation.
Pricing Engine Factory.
FX Barrier Option data model and serialization.
FX Digital Option data model and serialization.
FX Double Barrier Option data model and serialization.
FX Double One-Touch/No-Touch Option data model and serialization.
FX One-Touch/No-Touch Option data model and serialization.
Portfolio class.
An concrete implementation of the Market class that loads todays market and builds the required curve...
Trade Factory.