Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
conventionsbasedfutureexpiry.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#include <oret/datapaths.hpp>
24#include <oret/toplevelfixture.hpp>
25
29
30using namespace std;
31using namespace boost::unit_test_framework;
32using namespace QuantLib;
33using namespace ore::data;
34
36
37// List of commodity names for data test case below
38vector<string> commodityNames = {
39 "ice_brent",
40 "ice_brent_old",
41 "nymex_cl",
42 "nymex_ng",
43 "ice_cig_basis",
44 "myr_palm_oil",
45 "ice_wti_midland_basis",
46 "ice_pmi",
47 "ice_erh",
48 "ice_eiw",
49 "ice_hen_basis",
50 "ice_his_basis",
51 "cme_myr_palm_oil"
52};
53
54BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
55
56BOOST_AUTO_TEST_SUITE(ConventionsBasedFutureExpiryTests)
57
58BOOST_DATA_TEST_CASE(testExpiryDates, bdata::make(commodityNames), commodityName) {
59
60 BOOST_TEST_MESSAGE("Testing expiry dates for commodity: " << commodityName);
61
62 // Read in the relevant conventions file
63 Conventions conventions;
64 string filename = commodityName + "_conventions.xml";
65 conventions.fromFile(TEST_INPUT_FILE(filename));
66
67 // Create the conventions based expiry calculator
68 BOOST_TEST_REQUIRE(conventions.has(commodityName));
69 auto convention = QuantLib::ext::dynamic_pointer_cast<CommodityFutureConvention>(conventions.get(commodityName));
70 BOOST_TEST_REQUIRE(convention);
71 ConventionsBasedFutureExpiry cbfe(*convention);
72
73 // Read in the contract months and expected expiry dates
74 filename = commodityName + "_expiries.csv";
75 CSVFileReader reader(TEST_INPUT_FILE(filename), true, ",");
76 BOOST_REQUIRE_EQUAL(reader.numberOfColumns(), 3);
77
78 while (reader.next()) {
79
80 // Get the contract date and the expected expiry date from the file
81 Date contractDate = parseDate(reader.get(0));
82 Date expExpiryDate = parseDate(reader.get(1));
83
84 // Calculate the expiry date using the future expiry calculator
85 Date expiryDate = cbfe.expiryDate(contractDate, 0);
86
87 // Check that the calculated expiry equals the expected expiry date
88 BOOST_CHECK_EQUAL(expExpiryDate, expiryDate);
89
90 // If there is an expected option expiry date, test that also
91 string strExpOptionExpiry = reader.get(2);
92 if (!strExpOptionExpiry.empty()) {
93
94 // Expected option expiry date
95 Date expOptionExpiry = parseDate(strExpOptionExpiry);
96
97 // Calculate the option expiry date using the future expiry calculator
98 Date optionExpiry = cbfe.expiryDate(contractDate, 0, true);
99
100 BOOST_CHECK_EQUAL(expOptionExpiry, optionExpiry);
101 }
102 }
103}
104
105BOOST_AUTO_TEST_SUITE_END()
106
107BOOST_AUTO_TEST_SUITE_END()
Size numberOfColumns() const
std::string get(const std::string &field) const
Perform date calculations for future contracts based on conventions.
QuantLib::Date expiryDate(const QuantLib::Date &contractDate, QuantLib::Natural monthOffset=0, bool forOption=false) override
Repository for currency dependent market conventions.
bool has(const std::string &id) const
Checks if we have a convention with the given id.
QuantLib::ext::shared_ptr< Convention > get(const string &id) const
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
Base class for classes that perform date calculations for future contracts.
utility class to access CSV files
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
Map text representations to QuantLib/QuantExt types.
vector< string > commodityNames
BOOST_DATA_TEST_CASE(testExpiryDates, bdata::make(commodityNames), commodityName)