Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fxaccumulator.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// clang-format off
20#include <boost/test/unit_test.hpp>
21#include <boost/test/data/test_case.hpp>
22// clang-format on
23
24#include <boost/make_shared.hpp>
25#include <boost/test/unit_test.hpp>
37#include <oret/datapaths.hpp>
38#include <oret/toplevelfixture.hpp>
39
40#include <ql/time/date.hpp>
41#include <ql/pricingengines/blackformula.hpp>
42
43#include <boost/math/distributions/normal.hpp>
44
45#include <iostream>
46#include <iomanip>
47
48using namespace ore::data;
49using namespace QuantExt;
50using namespace QuantLib;
51using namespace boost::unit_test_framework;
52
53BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
54
55BOOST_AUTO_TEST_SUITE(FxAccumulatorTest)
56
58 BOOST_TEST_MESSAGE("Testing Fx Accumulator...");
59
63
64 Settings::instance().evaluationDate() = Date(31, Dec, 2018);
65 Date asof = Settings::instance().evaluationDate();
66 // Market
67 auto conventions = QuantLib::ext::make_shared<Conventions>();
68 conventions->fromFile(TEST_INPUT_FILE("conventions.xml"));
69 InstrumentConventions::instance().setConventions(conventions);
70
71 auto todaysMarketParams = QuantLib::ext::make_shared<TodaysMarketParameters>();
72 todaysMarketParams->fromFile(TEST_INPUT_FILE("todaysmarket.xml"));
73 auto curveConfigs = QuantLib::ext::make_shared<CurveConfigurations>();
74 curveConfigs->fromFile(TEST_INPUT_FILE("curveconfig.xml"));
75 QuantLib::ext::shared_ptr<Loader> loader =
76 QuantLib::ext::make_shared<CSVLoader>(TEST_INPUT_FILE("market.txt"), TEST_INPUT_FILE("fixings.txt"), false);
77 QuantLib::ext::shared_ptr<TodaysMarket> market =
78 QuantLib::ext::make_shared<TodaysMarket>(asof, todaysMarketParams, loader, curveConfigs, false);
79
80 // Portfolio to test market
81 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
82 engineData->fromFile(TEST_INPUT_FILE("pricingengine.xml"));
83 QuantLib::ext::shared_ptr<EngineFactory> factory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
84
85 // move cleanup to fixture once this code is in ore minus
86 struct cleanup {
87 ~cleanup() { ore::data::ScriptLibraryStorage::instance().clear(); }
88 } cleanup;
90 library.fromFile(TEST_INPUT_FILE("scriptlibrary.xml"));
91 ore::data::ScriptLibraryStorage::instance().set(std::move(library));
92
93 // Read in the trade
94 Portfolio p;
95 string trade = "FX_Accumulator";
96 string portfolioFile = trade + ".xml";
97 p.fromFile(TEST_INPUT_FILE(portfolioFile));
98 BOOST_CHECK_NO_THROW(p.build(factory));
99
100 // Test case 1: An FxAccumulator with no KnockOutBarrier,one fixing and one leverage range should be equivalent to
101 // an FxForward
102 BOOST_CHECK_NO_THROW(p.get("FX_ACCUMULATOR_1")->instrument()->NPV());
103 BOOST_TEST_MESSAGE(p.get("FX_ACCUMULATOR_1")->instrument()->NPV() * 1.1469);
104 BOOST_TEST_MESSAGE(p.get("FX_FORWARD_1")->instrument()->NPV());
105
106 BOOST_CHECK_CLOSE(p.get("FX_ACCUMULATOR_1")->instrument()->NPV() * 1.1469,
107 p.get("FX_FORWARD_1")->instrument()->NPV(), 0.01);
108
109 // Test case 2: An FxAccumulator with no KnockOutBarrier,several fixing dates and one leverage range should be
110 // equivalent to the sum of FxForwards
111 BOOST_CHECK_CLOSE(p.get("FX_ACCUMULATOR_2")->instrument()->NPV() * 1.1469,
112 p.get("FX_FORWARD_2A")->instrument()->NPV() + p.get("FX_FORWARD_2B")->instrument()->NPV() +
113 p.get("FX_FORWARD_2C")->instrument()->NPV(),
114 0.01);
115
116 // Test case 3: The Value of an accumulator with varying leverage ranges should be sum of accumulators with those
117 // individual leverages (and zero for the other ranges)
118 BOOST_CHECK_CLOSE(p.get("FX_ACCUMULATOR_3")->instrument()->NPV() * 1.1469,
119 (p.get("FX_ACCUMULATOR_3A")->instrument()->NPV() +
120 p.get("FX_ACCUMULATOR_3B")->instrument()->NPV() +
121 p.get("FX_ACCUMULATOR_3C")->instrument()->NPV()) *
122 1.1469,
123 0.01);
124}
125
126BOOST_AUTO_TEST_SUITE_END()
127
128BOOST_AUTO_TEST_SUITE_END()
accumulator wrapper for scripted trade
Serializable portfolio.
Definition: portfolio.hpp:43
void build(const QuantLib::ext::shared_ptr< EngineFactory > &, const std::string &context="unspecified", const bool emitStructuredError=true)
Call build on all trades in the portfolio, the context is included in error messages.
Definition: portfolio.cpp:122
QuantLib::ext::shared_ptr< Trade > get(const std::string &id) const
Definition: portfolio.cpp:194
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
Currency and instrument specific conventions/defaults.
Market Datum Loader Implementation.
Curve configuration repository.
#define ORE_REGISTER_TRADE_BUILDER(NAME, CLASS, OVERWRITE)
#define ORE_REGISTER_ENGINE_BUILDER(CLASS, OVERWRITE)
A class to hold pricing engine parameters.
Pricing Engine Factory.
BOOST_AUTO_TEST_CASE(testNPV)
Portfolio class.
scripted trade data model
vector< string > curveConfigs
An concrete implementation of the Market class that loads todays market and builds the required curve...
A class to hold todays market configuration(s)