1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2023 Marcin Rybacki
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/cashflows/equitycashflow.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/indexes/equityindex.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/quotes/simplequote.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(EquityCashFlowTests)
struct CommonVars {
Date today;
Calendar calendar;
DayCounter dayCount;
Real notional;
ext::shared_ptr<EquityIndex> equityIndex;
RelinkableHandle<YieldTermStructure> localCcyInterestHandle;
RelinkableHandle<YieldTermStructure> dividendHandle;
RelinkableHandle<YieldTermStructure> quantoCcyInterestHandle;
RelinkableHandle<BlackVolTermStructure> equityVolHandle;
RelinkableHandle<BlackVolTermStructure> fxVolHandle;
RelinkableHandle<Quote> spotHandle;
RelinkableHandle<Quote> correlationHandle;
// utilities
CommonVars() {
calendar = TARGET();
dayCount = Actual365Fixed();
notional = 1.0e7;
today = calendar.adjust(Date(27, January, 2023));
Settings::instance().evaluationDate() = today;
equityIndex = ext::make_shared<EquityIndex>("eqIndex", calendar, EURCurrency(), localCcyInterestHandle,
dividendHandle, spotHandle);
equityIndex->addFixing(Date(5, January, 2023), 9010.0);
equityIndex->addFixing(today, 8690.0);
localCcyInterestHandle.linkTo(flatRate(0.0375, dayCount));
dividendHandle.linkTo(flatRate(0.005, dayCount));
quantoCcyInterestHandle.linkTo(flatRate(0.001, dayCount));
equityVolHandle.linkTo(flatVol(0.4, dayCount));
fxVolHandle.linkTo(flatVol(0.2, dayCount));
spotHandle.linkTo(ext::make_shared<SimpleQuote>(8700.0));
correlationHandle.linkTo(ext::make_shared<SimpleQuote>(0.4));
}
ext::shared_ptr<EquityCashFlow>
createEquityQuantoCashFlow(const ext::shared_ptr<EquityIndex>& index,
const Date& start,
const Date& end,
bool useQuantoPricer = true) {
auto cf = ext::make_shared<EquityCashFlow>(notional, index, start, end, end);
if (useQuantoPricer) {
auto pricer = ext::make_shared<EquityQuantoCashFlowPricer>(
quantoCcyInterestHandle, equityVolHandle, fxVolHandle, correlationHandle);
cf->setPricer(pricer);
}
return cf;
}
ext::shared_ptr<EquityCashFlow>
createEquityQuantoCashFlow(const ext::shared_ptr<EquityIndex>& index,
bool useQuantoPricer = true) {
Date start(5, January, 2023);
Date end(5, April, 2023);
return createEquityQuantoCashFlow(index, start, end, useQuantoPricer);
}
ext::shared_ptr<EquityCashFlow> createEquityQuantoCashFlow(bool useQuantoPricer = true) {
return createEquityQuantoCashFlow(equityIndex, useQuantoPricer);
}
};
void bumpMarketData(CommonVars& vars) {
vars.localCcyInterestHandle.linkTo(flatRate(0.04, vars.dayCount));
vars.dividendHandle.linkTo(flatRate(0.01, vars.dayCount));
vars.quantoCcyInterestHandle.linkTo(flatRate(0.03, vars.dayCount));
vars.equityVolHandle.linkTo(flatVol(0.45, vars.dayCount));
vars.fxVolHandle.linkTo(flatVol(0.25, vars.dayCount));
vars.spotHandle.linkTo(ext::make_shared<SimpleQuote>(8710.0));
}
void checkQuantoCorrection(bool includeDividend, bool bumpData = false) {
const Real tolerance = 1.0e-6;
CommonVars vars;
ext::shared_ptr<EquityIndex> equityIndex =
includeDividend ?
vars.equityIndex :
vars.equityIndex->clone(vars.localCcyInterestHandle, Handle<YieldTermStructure>(),
vars.spotHandle);
auto cf = vars.createEquityQuantoCashFlow(equityIndex);
if (bumpData)
bumpMarketData(vars);
Real strike = vars.equityIndex->fixing(cf->fixingDate());
Real indexStart = vars.equityIndex->fixing(cf->baseDate());
Real time = vars.localCcyInterestHandle->timeFromReference(cf->fixingDate());
Real rf = vars.localCcyInterestHandle->zeroRate(time, Continuous);
Real q = includeDividend ? vars.dividendHandle->zeroRate(time, Continuous) : Real(0.0);
Real eqVol = vars.equityVolHandle->blackVol(cf->fixingDate(), strike);
Real fxVol = vars.fxVolHandle->blackVol(cf->fixingDate(), 1.0);
Real rho = vars.correlationHandle->value();
Real spot = vars.spotHandle->value();
Real quantoForward = spot * std::exp((rf - q - rho * eqVol * fxVol) * time);
Real expectedAmount = (quantoForward / indexStart - 1.0) * vars.notional;
Real actualAmount = cf->amount();
if ((std::fabs(actualAmount - expectedAmount) > tolerance))
BOOST_ERROR("could not replicate equity quanto correction\n"
<< " actual amount: " << actualAmount << "\n"
<< " expected amount: " << expectedAmount << "\n"
<< " index start: " << indexStart << "\n"
<< " index end: " << quantoForward << "\n"
<< " local rate: " << rf << "\n"
<< " equity volatility: " << eqVol << "\n"
<< " FX volatility: " << fxVol << "\n"
<< " correlation: " << rho << "\n"
<< " spot: " << spot << "\n");
}
void checkRaisedError(const ext::shared_ptr<EquityCashFlow>& cf, const std::string& message) {
BOOST_CHECK_EXCEPTION(cf->amount(), Error, ExpectedErrorMessage(message));
}
BOOST_AUTO_TEST_CASE(testSimpleEquityCashFlow) {
BOOST_TEST_MESSAGE("Testing simple equity cash flow...");
const Real tolerance = 1.0e-6;
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow(false);
Real indexStart = vars.equityIndex->fixing(cf->baseDate());
Real indexEnd = vars.equityIndex->fixing(cf->fixingDate());
Real expectedAmount = (indexEnd / indexStart - 1.0) * vars.notional;
Real actualAmount = cf->amount();
if ((std::fabs(actualAmount - expectedAmount) > tolerance))
BOOST_ERROR("could not replicate simple equity quanto cash flow\n"
<< " actual amount: " << actualAmount << "\n"
<< " expected amount: " << expectedAmount << "\n"
<< " index start: " << indexStart << "\n"
<< " index end: " << indexEnd << "\n");
}
BOOST_AUTO_TEST_CASE(testQuantoCorrection) {
BOOST_TEST_MESSAGE("Testing quanto correction...");
checkQuantoCorrection(true);
checkQuantoCorrection(false);
// Checks whether observers are being notified
// about changes in market data handles.
checkQuantoCorrection(false, true);
}
BOOST_AUTO_TEST_CASE(testErrorWhenBaseDateAfterFixingDate) {
BOOST_TEST_MESSAGE("Testing error when base date after fixing date...");
CommonVars vars;
Date end(5, January, 2023);
Date start(5, April, 2023);
auto cf = vars.createEquityQuantoCashFlow(vars.equityIndex, start, end);
checkRaisedError(cf, "Fixing date cannot fall before base date.");
}
BOOST_AUTO_TEST_CASE(testErrorWhenQuantoCurveHandleIsEmpty) {
BOOST_TEST_MESSAGE("Testing error when quanto currency curve handle is empty...");
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow();
ext::shared_ptr<YieldTermStructure> yts;
vars.quantoCcyInterestHandle.linkTo(yts);
checkRaisedError(cf, "Quanto currency term structure handle cannot be empty.");
}
BOOST_AUTO_TEST_CASE(testErrorWhenEquityVolHandleIsEmpty) {
BOOST_TEST_MESSAGE("Testing error when equity vol handle is empty...");
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow();
ext::shared_ptr<BlackVolTermStructure> vol;
vars.equityVolHandle.linkTo(vol);
checkRaisedError(cf, "Equity volatility term structure handle cannot be empty.");
}
BOOST_AUTO_TEST_CASE(testErrorWhenFXVolHandleIsEmpty) {
BOOST_TEST_MESSAGE("Testing error when FX vol handle is empty...");
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow();
ext::shared_ptr<BlackVolTermStructure> vol;
vars.fxVolHandle.linkTo(vol);
checkRaisedError(cf, "FX volatility term structure handle cannot be empty.");
}
BOOST_AUTO_TEST_CASE(testErrorWhenCorrelationHandleIsEmpty) {
BOOST_TEST_MESSAGE("Testing error when correlation handle is empty...");
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow();
ext::shared_ptr<Quote> correlation;
vars.correlationHandle.linkTo(correlation);
checkRaisedError(cf, "Correlation handle cannot be empty.");
}
BOOST_AUTO_TEST_CASE(testErrorWhenInconsistentMarketDataReferenceDate) {
BOOST_TEST_MESSAGE("Testing error when market data reference dates are inconsistent...");
CommonVars vars;
auto cf = vars.createEquityQuantoCashFlow();
vars.quantoCcyInterestHandle.linkTo(flatRate(Date(26, January, 2023), 0.02, vars.dayCount));
checkRaisedError(
cf, "Quanto currency term structure, equity and FX volatility need to have the same "
"reference date.");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|