Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
DiscountingFxForwardEngineDeltaGamma Class Reference

Discounting FX Forward Engine providing analytical deltas and gammas. More...

#include <qle/pricingengines/discountingfxforwardenginedeltagamma.hpp>

+ Inheritance diagram for DiscountingFxForwardEngineDeltaGamma:
+ Collaboration diagram for DiscountingFxForwardEngineDeltaGamma:

Public Member Functions

 DiscountingFxForwardEngineDeltaGamma (const Currency &domCcy, const Handle< YieldTermStructure > &domCurve, const Currency &forCcy, const Handle< YieldTermStructure > &forCurve, const Handle< Quote > &spotFx, const std::vector< Time > &bucketTimes=std::vector< Time >(), const bool computeDelta=false, const bool computeGamma=false, const bool linearInZero=true, boost::optional< bool > includeSettlementDateFlows=boost::none, const Date &settlementDate=Date(), const Date &npvDate=Date(), const bool applySimmExemptions=false)
 
void calculate () const override
 
const Handle< YieldTermStructure > & domCurve () const
 
const Handle< YieldTermStructure > & forCurve () const
 
const Currency & domCcy () const
 
const Currency & forCcy () const
 
const Handle< Quote > & spotFx () const
 

Private Attributes

Currency domCcy_
 
Currency forCcy_
 
Handle< YieldTermStructure > domCurve_
 
Handle< YieldTermStructure > forCurve_
 
Handle< Quote > spotFx_
 
const std::vector< Time > bucketTimes_
 
const bool computeDelta_
 
const bool computeGamma_
 
const bool linearInZero_
 
boost::optional< boolincludeSettlementDateFlows_
 
Date settlementDate_
 
Date npvDate_
 
bool applySimmExemptions_
 

Detailed Description

Discounting FX Forward Engine providing analytical deltas and gammas.

This class generalises QuantExt's DiscountingFxForwardEngine, in analogy to the DiscountingCurrencySwapEngineDeltaGamma. Additional results are

deltaDiscount (map<Currency, vector<Real>> ): Delta on discount curve, rebucketed, values are in currency deltaFxSpot (map<Currency, Real> ): Delta on FX Spot (for ccy != npvCcy)

gamma (map<Currency, Matrix> ): Gamma matrix per currency between discount curve tenor points

Note that the second derivatives including the FX Spot are zero for the pure second derivative w.r.t. the FX Spot or given by the in currency delta values provided as the additional result deltaDiscount to be reinterpreted as values in domestic currency)

fxSpot (map<Currency, Real> ): FX Spot used for conversion to npvCurrency (for ccy != npvCcy) bucketTimes (vector<Real> ): Bucketing grid for deltas and gammas

npvDom, npvFor : NPV of domestic flow resp. foreign flow (in dom resp. for ccy)

Definition at line 56 of file discountingfxforwardenginedeltagamma.hpp.

Constructor & Destructor Documentation

◆ DiscountingFxForwardEngineDeltaGamma()

DiscountingFxForwardEngineDeltaGamma ( const Currency &  domCcy,
const Handle< YieldTermStructure > &  domCurve,
const Currency &  forCcy,
const Handle< YieldTermStructure > &  forCurve,
const Handle< Quote > &  spotFx,
const std::vector< Time > &  bucketTimes = std::vector<Time>(),
const bool  computeDelta = false,
const bool  computeGamma = false,
const bool  linearInZero = true,
boost::optional< bool includeSettlementDateFlows = boost::none,
const Date &  settlementDate = Date(),
const Date &  npvDate = Date(),
const bool  applySimmExemptions = false 
)
Parameters
domCcy,domCurveCurrency 1 and its discount curve.
forCcy,forCurveCurrency 2 and its discount curve.
spotFxThe market spot rate quote, given as units of domCcy for one unit of forCcy. The spot rate must be given w.r.t. a settlement equal to the npv date.
bucketTimesBucketing grid for deltas and gammas
computeDelta,computeGammaSwitch to enable/disable delta and gamma calculation
linearInZeroInterpolation used in the delta/gamma rebucketing to the desired time grid
includeSettlementDateFlows,settlementDateIf includeSettlementDateFlows is true (false), cashflows on the settlementDate are (not) included in the NPV. If not given the settlement date is set to the npv date.
npvDateThe date w.r.t. which the npv should be computed.
applySimmExemptionsIf true, physically settled flows are ignored in sensi calculation, i.e. in he additional results above, including npvDom and npvFor

Definition at line 30 of file discountingfxforwardenginedeltagamma.cpp.

37 bucketTimes_(bucketTimes), computeDelta_(computeDelta), computeGamma_(computeGamma), linearInZero_(linearInZero),
38 includeSettlementDateFlows_(includeSettlementDateFlows), settlementDate_(settlementDate), npvDate_(npvDate),
39 applySimmExemptions_(applySimmExemptions) {
40 registerWith(domCurve_);
41 registerWith(forCurve_);
42 registerWith(spotFx_);
43}

Member Function Documentation

◆ calculate()

void calculate ( ) const
override

Definition at line 45 of file discountingfxforwardenginedeltagamma.cpp.

45 {
46
47 QL_REQUIRE(!domCurve_.empty(), "domestic curve is empty");
48 QL_REQUIRE(!forCurve_.empty(), "foreign curve is empty");
49 QL_REQUIRE(!spotFx_.empty(), "FX quote is empty");
50
51 // we build this engine similar to the currency swap engine
52
53 std::vector<Leg> legs;
54 legs.push_back(Leg(1, QuantLib::ext::make_shared<SimpleCashFlow>(arguments_.nominal1, arguments_.maturityDate)));
55 legs.push_back(Leg(1, QuantLib::ext::make_shared<SimpleCashFlow>(arguments_.nominal2, arguments_.maturityDate)));
56
57 std::vector<Currency> currencies = {arguments_.currency1, arguments_.currency2};
58
59 std::vector<Real> payer = {arguments_.payCurrency1 ? -1.0 : 1.0, arguments_.payCurrency1 ? 1.0 : -1.0};
60
61 results_.value = 0.0;
62
63 std::map<Currency, std::map<Date, Real>, CurrencyComparator> deltaDiscountRaw, gammaDiscountRaw;
64 std::map<Currency, Real, CurrencyComparator> fxSpot, fxSpotDelta;
65 std::map<Date, Real> empty;
66 std::map<std::pair<Date, Date>, Real> empty2;
67 Real empty3 = 0.0;
68
69 Real domFlow = 0, forFlow = 0, domNPV = 0, forNPV = 0;
70 for (Size i = 0; i < 2; ++i) {
71 try {
72 Handle<YieldTermStructure> yts;
73 if (currencies[i] == domCcy_)
74 yts = domCurve_;
75 else if (currencies[i] == forCcy_)
76 yts = forCurve_;
77 else {
78 QL_FAIL("ccy " << currencies[i] << " not handled.");
79 }
80 Real npv = 0.0, bps = 0.0, simpleCashFlowNpv = 0.0;
81 detail::NpvDeltaGammaCalculator calc(
82 yts, payer[i], npv, bps, computeDelta_, computeGamma_, false, deltaDiscountRaw[currencies[i]], empty,
83 empty, gammaDiscountRaw[currencies[i]], empty2, empty2, empty, empty3,
84 applySimmExemptions_ && arguments_.isPhysicallySettled, simpleCashFlowNpv);
85 for (Size ii = 0; ii < legs[i].size(); ++ii) {
86 CashFlow& cf = *legs[i][ii];
87 if (cf.date() <= yts->referenceDate()) {
88 continue;
89 }
90 cf.accept(calc);
91 if (currencies[i] == domCcy_)
92 domFlow = cf.amount();
93 if (currencies[i] == forCcy_)
94 forFlow = cf.amount();
95 }
96 if (currencies[i] == domCcy_) {
97 domNPV = npv;
98 results_.additionalResults["npvDom"] = npv;
99 results_.value += npv + simpleCashFlowNpv;
100 } else {
101 forNPV = npv;
102 results_.additionalResults["npvFor"] = npv;
103 results_.value += (npv + simpleCashFlowNpv) * spotFx_->value();
104 fxSpot[currencies[i]] = spotFx_->value();
105 fxSpotDelta[currencies[i]] += npv;
106 }
107 } catch (const std::exception& e) {
108 QL_FAIL("DiscountingFxForwardEngineDeltaGamma, leg " << i << ": " << e.what());
109 }
110 } // for i = 0, 1 (legs)
111
112 results_.additionalResults["fxSpot"] = fxSpot;
113 results_.additionalResults["deltaFxSpot"] = fxSpotDelta;
114
115 Date npvDate = npvDate_;
116 if (npvDate == Null<Date>()) {
117 npvDate = domCurve_->referenceDate();
118 }
119 Date settlementDate = settlementDate_;
120 if (settlementDate == Null<Date>()) {
121 settlementDate = npvDate;
122 }
123
124 // convert raw deltas to given bucketing structure
125 if (computeDelta_) {
126 std::map<Currency, std::vector<Real>, CurrencyComparator> deltaDiscount;
127 for (std::map<Currency, std::map<Date, Real>, CurrencyComparator>::const_iterator i = deltaDiscountRaw.begin();
128 i != deltaDiscountRaw.end(); ++i) {
129 Handle<YieldTermStructure> yts = i->first == domCcy_ ? domCurve_ : forCurve_;
130 deltaDiscount[i->first] =
131 detail::rebucketDeltas(bucketTimes_, i->second, yts->referenceDate(), yts->dayCounter(), linearInZero_);
132 }
133 results_.additionalResults["deltaDiscount"] = deltaDiscount;
134 results_.additionalResults["bucketTimes"] = bucketTimes_;
135 }
136
137 // convert raw gammas to given bucketing structure
138 if (computeGamma_) {
139 std::map<Currency, Matrix, CurrencyComparator> gamma;
140 for (std::vector<Currency>::const_iterator i = currencies.begin(); i != currencies.end(); ++i) {
141 Handle<YieldTermStructure> yts = *i == domCcy_ ? domCurve_ : forCurve_;
142 Matrix tmp = detail::rebucketGammas(bucketTimes_, gammaDiscountRaw[*i], empty2, empty2, false,
143 yts->referenceDate(), yts->dayCounter(), linearInZero_);
144 gamma[*i] = tmp;
145 }
146 results_.additionalResults["gamma"] = gamma;
147 results_.additionalResults["bucketTimes"] = bucketTimes_;
148 }
149
150 // Align notional with ISDA AANA/GRID guidance as of November 2020 for deliverable forwards
151 if (fabs(domNPV) > fabs(forNPV) * spotFx_->value()) {
152 results_.additionalResults["currentNotional"] = domFlow;
153 results_.additionalResults["notionalCurrency"] = domCcy_.code();
154 } else {
155 results_.additionalResults["currentNotional"] = forFlow;
156 results_.additionalResults["notionalCurrency"] = forCcy_.code();
157 }
158
159} // calculate
const Instrument::results * results_
Definition: cdsoption.cpp:81
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_
+ Here is the call graph for this function:

◆ domCurve()

const Handle< YieldTermStructure > & domCurve ( ) const

Definition at line 94 of file discountingfxforwardenginedeltagamma.hpp.

94{ return domCurve_; }

◆ forCurve()

const Handle< YieldTermStructure > & forCurve ( ) const

Definition at line 96 of file discountingfxforwardenginedeltagamma.hpp.

96{ return forCurve_; }

◆ domCcy()

const Currency & domCcy ( ) const

Definition at line 98 of file discountingfxforwardenginedeltagamma.hpp.

98{ return domCcy_; }

◆ forCcy()

const Currency & forCcy ( ) const

Definition at line 99 of file discountingfxforwardenginedeltagamma.hpp.

99{ return forCcy_; }

◆ spotFx()

const Handle< Quote > & spotFx ( ) const

Definition at line 101 of file discountingfxforwardenginedeltagamma.hpp.

101{ return spotFx_; }

Member Data Documentation

◆ domCcy_

Currency domCcy_
private

Definition at line 104 of file discountingfxforwardenginedeltagamma.hpp.

◆ forCcy_

Currency forCcy_
private

Definition at line 104 of file discountingfxforwardenginedeltagamma.hpp.

◆ domCurve_

Handle<YieldTermStructure> domCurve_
private

Definition at line 105 of file discountingfxforwardenginedeltagamma.hpp.

◆ forCurve_

Handle<YieldTermStructure> forCurve_
private

Definition at line 105 of file discountingfxforwardenginedeltagamma.hpp.

◆ spotFx_

Handle<Quote> spotFx_
private

Definition at line 106 of file discountingfxforwardenginedeltagamma.hpp.

◆ bucketTimes_

const std::vector<Time> bucketTimes_
private

Definition at line 107 of file discountingfxforwardenginedeltagamma.hpp.

◆ computeDelta_

const bool computeDelta_
private

Definition at line 108 of file discountingfxforwardenginedeltagamma.hpp.

◆ computeGamma_

const bool computeGamma_
private

Definition at line 108 of file discountingfxforwardenginedeltagamma.hpp.

◆ linearInZero_

const bool linearInZero_
private

Definition at line 108 of file discountingfxforwardenginedeltagamma.hpp.

◆ includeSettlementDateFlows_

boost::optional<bool> includeSettlementDateFlows_
private

Definition at line 109 of file discountingfxforwardenginedeltagamma.hpp.

◆ settlementDate_

Date settlementDate_
private

Definition at line 110 of file discountingfxforwardenginedeltagamma.hpp.

◆ npvDate_

Date npvDate_
private

Definition at line 111 of file discountingfxforwardenginedeltagamma.hpp.

◆ applySimmExemptions_

bool applySimmExemptions_
private

Definition at line 112 of file discountingfxforwardenginedeltagamma.hpp.