Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fxtriangulation.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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>
22#include <oret/toplevelfixture.hpp>
23#include <ql/quotes/simplequote.hpp>
24
25using namespace ore::data;
26using namespace QuantLib;
27using namespace std;
28
29using ore::test::TopLevelFixture;
30
31namespace {
32
33// Test data from ECB as of 8 Jan 2016
34vector<pair<string, Real>> fxtData() {
35
36 // clang-format off
37 vector<pair<string, Real>> testData{
38 { "EURUSD", 1.0861 },
39 { "EURJPY", 128.51 },
40 { "EURCZK", 27.022 },
41 { "EURDKK", 7.4598 },
42 { "EURGBP", 0.74519 },
43 { "EURHUF", 315.53 },
44 { "EURPLN", 4.3523 },
45 { "EURSEK", 9.2640 },
46 { "EURCHF", 1.0860 },
47 { "EURNOK", 9.6810 },
48 { "EURAUD", 1.5495 },
49 { "ZZZEUR", 3.141 }, // just to test reverse quotes
50 { "AUDNZD", 1.0616327848 } // Should be enough for USDNZD (value = 1.645 / 1.5495)
51 };
52 // clang-format on
53
54 return testData;
55}
56
57// Provide the FXTriangulation object for the tests
58class FxTriFixture : public TopLevelFixture {
59public:
61
62 FxTriFixture() {
63 // Initialise FX data
64 std::map<std::string, Handle<Quote>> quotes;
65 for (const auto& p : fxtData()) {
66 Handle<Quote> q(QuantLib::ext::make_shared<SimpleQuote>(p.second));
67 quotes[p.first] = q;
68 }
69 fx = FXTriangulation(quotes);
70 }
71
72 ~FxTriFixture() {}
73};
74
75} // namespace
76
77BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, TopLevelFixture)
78
79BOOST_FIXTURE_TEST_SUITE(FXTriangulationTests, FxTriFixture)
80
81BOOST_AUTO_TEST_CASE(testDataLoaded) {
82 for (const auto& p : fxtData()) {
83 BOOST_CHECK_EQUAL(fx.getQuote(p.first)->value(), p.second);
84 }
85}
86
88 BOOST_CHECK_EQUAL(fx.getQuote("EUREUR")->value(), 1.0);
89 BOOST_CHECK_EQUAL(fx.getQuote("USDUSD")->value(), 1.0);
90}
91
93
94 // Tolerance for comparisons
95 Real tol = 1e-12;
96
97 // Check inverse
98 BOOST_CHECK_CLOSE(fx.getQuote("USDEUR")->value(), 1.0 / 1.0861, tol);
99 BOOST_CHECK_CLOSE(fx.getQuote("JPYEUR")->value(), 1.0 / 128.51, tol);
100
101 // Check Triangulation
102 BOOST_CHECK_CLOSE(fx.getQuote("USDJPY")->value(), 128.51 / 1.0861, tol);
103 BOOST_CHECK_CLOSE(fx.getQuote("JPYUSD")->value(), 1.0861 / 128.51, tol);
104 BOOST_CHECK_CLOSE(fx.getQuote("USDGBP")->value(), 0.74519 / 1.0861, tol);
105 BOOST_CHECK_CLOSE(fx.getQuote("GBPUSD")->value(), 1.0861 / 0.74519, tol);
106 BOOST_CHECK_CLOSE(fx.getQuote("NOKSEK")->value(), 9.2640 / 9.6810, tol);
107
108 // Check Triangulation where the EUR quote is reversed
109 BOOST_CHECK_CLOSE(fx.getQuote("ZZZUSD")->value(), 3.141 * 1.0861, tol);
110 BOOST_CHECK_CLOSE(fx.getQuote("USDZZZ")->value(), 1 / (3.141 * 1.0861), tol);
111}
112
113BOOST_AUTO_TEST_CASE(testMoreThanOneStep) {
114
115 // Larger tolerance for multiple steps
116 Real tol = 1e-8;
117
118 BOOST_CHECK_CLOSE(fx.getQuote("USDNZD")->value(), 1.6450 / 1.0861, tol);
119}
120
121BOOST_AUTO_TEST_CASE(testBadInputsThrow) {
122 BOOST_CHECK_THROW(fx.getQuote("BadInput"), QuantLib::Error);
123 BOOST_CHECK_THROW(fx.getQuote(""), QuantLib::Error);
124 BOOST_CHECK_THROW(fx.getQuote("MXNZAR"), QuantLib::Error);
125}
126
127BOOST_AUTO_TEST_SUITE_END()
128
129BOOST_AUTO_TEST_SUITE_END()
QuantLib::Handle< QuantLib::Quote > getQuote(const std::string &pair) const
Intelligent FX price repository.
BOOST_AUTO_TEST_CASE(testDataLoaded)