22#include <ql/cashflows/cashflows.hpp>
23#include <ql/cashflows/floatingratecoupon.hpp>
24#include <ql/exchangerate.hpp>
25#include <ql/utilities/dataformatters.hpp>
27#include <ql/cashflows/cashflows.hpp>
28#include <ql/errors.hpp>
30#include <boost/unordered_map.hpp>
35 const std::vector<Handle<YieldTermStructure>>& discountCurves,
const std::vector<Handle<Quote>>& fxQuotes,
36 const std::vector<Currency>& currencies,
const Currency& npvCurrency,
const std::vector<Time>& bucketTimes,
37 const bool computeDelta,
const bool computeGamma,
const bool linearInZero,
const bool applySimmExemptions)
38 : discountCurves_(discountCurves), fxQuotes_(fxQuotes), currencies_(currencies), npvCurrency_(npvCurrency),
39 bucketTimes_(bucketTimes), computeDelta_(computeDelta), computeGamma_(computeGamma), linearInZero_(linearInZero),
40 applySimmExemptions_(applySimmExemptions) {
43 "currencies does not match number of discount curves.");
45 "currencies does not match number of FX quotes.");
52 QL_REQUIRE(!
bucketTimes_.empty() || (!computeDelta && !computeGamma),
53 "bucket times are empty, although sensitivities have to be calculated");
59 return Handle<YieldTermStructure>();
67 return Handle<Quote>();
74 for (Size i = 0; i <
arguments_.currency.size(); i++) {
76 Handle<YieldTermStructure> yts =
fetchTS(ccy);
77 QL_REQUIRE(!yts.empty(),
"Discounting term structure is "
80 Handle<Quote> fxQuote =
fetchFX(ccy);
81 QL_REQUIRE(!fxQuote.empty(),
"FX quote is empty "
91 results_.errorEstimate = Null<Real>();
95 results_.inCcyLegNPV.resize(numLegs);
99 std::map<Currency, std::map<Date, Real>,
CurrencyComparator> deltaDiscountRaw, deltaForwardRaw, gammaDiscountRaw;
100 std::map<Currency, std::map<std::pair<Date, Date>, Real>,
CurrencyComparator> gammaForwardRaw, gammaDscFwdRaw;
101 std::map<Currency, Real, CurrencyComparator> fxLinkedForeignNpv;
102 std::map<Currency, Real, CurrencyComparator> fxSpot, fxSpotDelta;
103 std::set<Currency, CurrencyComparator>
currencies;
104 std::map<Date, Real> empty, fxLinkedDeltaEmpty;
106 for (Size i = 0; i < numLegs; ++i) {
115 for (Size j = 0; j < numLegs; ++j) {
124 Handle<YieldTermStructure> yts =
fetchTS(ccy);
125 Real npv = 0.0, bps = 0.0, simpleCashFlowNpv = 0.0;
126 Real fxLinkedForeignNpvTmp = 0.0;
129 deltaForwardRaw[ccy], empty, gammaDiscountRaw[ccy], gammaForwardRaw[ccy], gammaDscFwdRaw[ccy], empty,
130 fxLinkedForeignNpvTmp,
133 for (Size ii = 0; ii < leg.size(); ++ii) {
134 CashFlow& cf = *leg[ii];
135 if (cf.date() <= yts->referenceDate()) {
140 Real fx =
fetchFX(ccy)->value();
141 fxSpotDelta[ccy] += npv;
143 results_.inCcyLegNPV[i] = npv + simpleCashFlowNpv;
150 fxSpotDelta[ccy] -= fxLinkedForeignNpvTmp * fx2 / fx;
151 fxLinkedForeignNpv[ccy2] += fxLinkedForeignNpvTmp;
153 }
catch (std::exception& e) {
154 QL_FAIL(
"DiscountingCurrencySwapEngineDeltaGamma, leg " << i <<
": " << e.what());
160 for (
auto const& f : fxLinkedForeignNpv) {
161 fxSpotDelta[f.first] += f.second;
166 results_.additionalResults[
"fxSpot"] = fxSpot;
167 results_.additionalResults[
"deltaFxSpot"] = fxSpotDelta;
174 std::map<Currency, std::vector<Real>,
CurrencyComparator> deltaDiscount, deltaForward;
175 for (std::map<Currency, std::map<Date, Real>,
CurrencyComparator>::const_iterator i = deltaDiscountRaw.begin();
176 i != deltaDiscountRaw.end(); ++i) {
177 Handle<YieldTermStructure> yts =
fetchTS(i->first);
178 deltaDiscount[i->first] =
181 results_.additionalResults[
"deltaDiscount"] = deltaDiscount;
183 for (std::map<Currency, std::map<Date, Real>,
CurrencyComparator>::const_iterator i = deltaForwardRaw.begin();
184 i != deltaForwardRaw.end(); ++i) {
185 Handle<YieldTermStructure> yts =
fetchTS(i->first);
186 deltaForward[i->first] =
189 results_.additionalResults[
"deltaForward"] = deltaForward;
195 std::map<Currency, Matrix, CurrencyComparator> gamma;
197 Handle<YieldTermStructure> yts =
fetchTS(*i);
200 true, yts->referenceDate(), yts->dayCounter(),
linearInZero_);
203 results_.additionalResults[
"gamma"] = gamma;
const Instrument::results * results_
Handle< Quote > fetchFX(Currency ccy) const
const std::vector< Time > bucketTimes_
const std::vector< Handle< Quote > > fxQuotes_
const std::vector< Handle< YieldTermStructure > > discountCurves_
std::vector< Currency > currencies()
Handle< YieldTermStructure > fetchTS(Currency ccy) const
void calculate() const override
DiscountingCurrencySwapEngineDeltaGamma(const std::vector< Handle< YieldTermStructure > > &discountCurves, const std::vector< Handle< Quote > > &fxQuotes, const std::vector< Currency > ¤cies, const Currency &npvCurrency, const std::vector< Time > &bucketTimes=std::vector< Time >(), const bool computeDelta=false, const bool computeGamma=false, const bool linearInZero=true, const bool applySimmExemptions=false)
const std::vector< Currency > currencies_
const bool applySimmExemptions_
discounting currency swap engine providing analytical deltas and gammas for vanilla swaps
Swap engine providing analytical deltas and gammas for vanilla swaps.
Matrix rebucketGammas(const std::vector< Time > &gammaTimes, const std::map< Date, Real > &gammaDscRaw, std::map< std::pair< Date, Date >, Real > &gammaForward, std::map< std::pair< Date, Date >, Real > &gammaDscFwd, const bool forceFullMatrix, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
std::vector< Real > rebucketDeltas(const std::vector< Time > &deltaTimes, const std::map< Date, Real > &deltaRaw, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
Swap::arguments * arguments_