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

FX Index. More...

#include <qle/indexes/fxindex.hpp>

+ Inheritance diagram for FxIndex:
+ Collaboration diagram for FxIndex:

Public Member Functions

 FxIndex (const std::string &familyName, Natural fixingDays, const Currency &source, const Currency &target, const Calendar &fixingCalendar, const Handle< YieldTermStructure > &sourceYts=Handle< YieldTermStructure >(), const Handle< YieldTermStructure > &targetYts=Handle< YieldTermStructure >(), bool fixingTriangulation=false)
 
 FxIndex (const std::string &familyName, Natural fixingDays, const Currency &source, const Currency &target, const Calendar &fixingCalendar, const Handle< Quote > fxSpot, const Handle< YieldTermStructure > &sourceYts=Handle< YieldTermStructure >(), const Handle< YieldTermStructure > &targetYts=Handle< YieldTermStructure >(), bool fixingTriangulation=true)
 
Index interface
std::string name () const override
 
Calendar fixingCalendar () const override
 
bool isValidFixingDate (const Date &fixingDate) const override
 
Real fixing (const Date &fixingDate, bool forecastTodaysFixing=false) const override
 
Observer interface
void update () override
 
Inspectors
std::string familyName () const
 
std::string oreName () const
 
Natural fixingDays () const
 
Date fixingDate (const Date &valueDate) const
 
const Currency & sourceCurrency () const
 
const Currency & targetCurrency () const
 
const Handle< YieldTermStructure > & sourceCurve () const
 
const Handle< YieldTermStructure > & targetCurve () const
 
const Handle< Quote > fxQuote (bool withSettlementLag=false) const
 fxQuote returns instantaneous Quote by default, otherwise settlement after fixingDays More...
 
const bool useQuote () const
 
Date calculations
virtual Date valueDate (const Date &fixingDate) const
 
- Public Member Functions inherited from EqFxIndexBase
virtual ~EqFxIndexBase ()
 
virtual Real forecastFixing (const Time &fixingTime) const =0
 returns the fixing at the given time More...
 
virtual Real pastFixing (const Date &fixingDate) const =0
 returns a past fixing at the given date More...
 

Fixing calculations

std::string familyName_
 
std::string oreName_
 
Natural fixingDays_
 
Currency sourceCurrency_
 
Currency targetCurrency_
 
const Handle< YieldTermStructure > sourceYts_
 
const Handle< YieldTermStructure > targetYts_
 
std::string name_
 
const Handle< Quote > fxSpot_
 
Handle< Quote > fxRate_
 
bool useQuote_
 
Calendar fixingCalendar_
 
bool fixingTriangulation_
 
virtual Real forecastFixing (const Time &fixingTime) const override
 It can be overridden to implement particular conventions. More...
 
virtual Real forecastFixing (const Date &fixingDate) const
 
Real pastFixing (const Date &fixingDate) const override
 returns a past fixing at the given date More...
 
QuantLib::ext::shared_ptr< FxIndexclone (const Handle< Quote > fxQuote=Handle< Quote >(), const Handle< YieldTermStructure > &sourceYts=Handle< YieldTermStructure >(), const Handle< YieldTermStructure > &targetYts=Handle< YieldTermStructure >(), const std::string &familyName=std::string())
 clone the index, the clone will be linked to the provided handles More...
 
void initialise ()
 

Detailed Description

FX Index.

Definition at line 94 of file fxindex.hpp.

Constructor & Destructor Documentation

◆ FxIndex() [1/2]

FxIndex ( const std::string &  familyName,
Natural  fixingDays,
const Currency &  source,
const Currency &  target,
const Calendar &  fixingCalendar,
const Handle< YieldTermStructure > &  sourceYts = Handle<YieldTermStructure>(),
const Handle< YieldTermStructure > &  targetYts = Handle<YieldTermStructure>(),
bool  fixingTriangulation = false 
)

familyName may be e.g. ECB fixingDays determine the spot date of the currency pair source is the asset or foreign currency target is the numeraire or domestic currency fixingCalendar is the calendar defining good days for the pair this class uses the exchange rate manager to retrieve spot values fxSpot is the fx rate settled at today + fixingDays

Definition at line 101 of file fxindex.cpp.

105 sourceYts_(sourceYts), targetYts_(targetYts), useQuote_(false), fixingCalendar_(fixingCalendar),
106 fixingTriangulation_(fixingTriangulation) {
107
108 initialise();
109}
Natural fixingDays() const
Definition: fxindex.hpp:127
Calendar fixingCalendar() const override
Definition: fxindex.cpp:269
Currency sourceCurrency_
Definition: fxindex.hpp:157
bool fixingTriangulation_
Definition: fxindex.hpp:168
Natural fixingDays_
Definition: fxindex.hpp:156
const Handle< YieldTermStructure > targetYts_
Definition: fxindex.hpp:158
std::string familyName_
Definition: fxindex.hpp:155
const Handle< YieldTermStructure > sourceYts_
Definition: fxindex.hpp:158
Currency targetCurrency_
Definition: fxindex.hpp:157
Calendar fixingCalendar_
Definition: fxindex.hpp:167
std::string familyName() const
Definition: fxindex.hpp:125
+ Here is the call graph for this function:

◆ FxIndex() [2/2]

FxIndex ( const std::string &  familyName,
Natural  fixingDays,
const Currency &  source,
const Currency &  target,
const Calendar &  fixingCalendar,
const Handle< Quote >  fxSpot,
const Handle< YieldTermStructure > &  sourceYts = Handle<YieldTermStructure>(),
const Handle< YieldTermStructure > &  targetYts = Handle<YieldTermStructure>(),
bool  fixingTriangulation = true 
)

Definition at line 111 of file fxindex.cpp.

116 sourceYts_(sourceYts), targetYts_(targetYts), fxSpot_(fxSpot), useQuote_(true), fixingCalendar_(fixingCalendar),
117 fixingTriangulation_(fixingTriangulation) {
118
119 initialise();
120}
const Handle< Quote > fxSpot_
Definition: fxindex.hpp:161
+ Here is the call graph for this function:

Member Function Documentation

◆ name()

std::string name ( ) const
override

Definition at line 267 of file fxindex.cpp.

267{ return name_; }
std::string name_
Definition: fxindex.hpp:159
+ Here is the caller graph for this function:

◆ fixingCalendar()

Calendar fixingCalendar ( ) const
override

Definition at line 269 of file fxindex.cpp.

269{ return fixingCalendar_; }
+ Here is the caller graph for this function:

◆ isValidFixingDate()

bool isValidFixingDate ( const Date &  fixingDate) const
override

Definition at line 271 of file fxindex.cpp.

271{ return fixingCalendar().isBusinessDay(d); }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fixing()

Real fixing ( const Date &  fixingDate,
bool  forecastTodaysFixing = false 
) const
override

Definition at line 158 of file fxindex.cpp.

158 {
159
160 Date adjustedFixingDate = fixingCalendar().adjust(fixingDate, Preceding);
161
162 Date today = Settings::instance().evaluationDate();
163
164 Real result = Null<Real>();
165
166 if (adjustedFixingDate > today || (adjustedFixingDate == today && forecastTodaysFixing))
167 result = forecastFixing(adjustedFixingDate);
168
169 if (result == Null<Real>()) {
170 if (adjustedFixingDate < today || Settings::instance().enforcesTodaysHistoricFixings()) {
171 // must have been fixed
172 // do not catch exceptions
173 result = pastFixing(adjustedFixingDate);
174 QL_REQUIRE(result != Null<Real>(), "Missing " << name() << " fixing for " << adjustedFixingDate);
175 } else {
176 try {
177 // might have been fixed
178 result = pastFixing(adjustedFixingDate);
179 } catch (Error&) {
180 ; // fall through and forecast
181 }
182 if (result == Null<Real>())
183 result = forecastFixing(adjustedFixingDate);
184 }
185 }
186
187 return result;
188}
virtual Real forecastFixing(const Time &fixingTime) const override
It can be overridden to implement particular conventions.
Definition: fxindex.cpp:190
std::string name() const override
Definition: fxindex.cpp:267
Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
Definition: fxindex.cpp:290
Date fixingDate(const Date &valueDate) const
Definition: fxindex.cpp:278
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update()

void update ( )
override

Definition at line 273 of file fxindex.cpp.

273 {
274 fxRate_ = Handle<Quote>();
275 notifyObservers();
276}
Handle< Quote > fxRate_
Definition: fxindex.hpp:163

◆ familyName()

std::string familyName ( ) const

Definition at line 125 of file fxindex.hpp.

125{ return familyName_; }
+ Here is the caller graph for this function:

◆ oreName()

std::string oreName ( ) const

Definition at line 126 of file fxindex.hpp.

126{ return oreName_; }
std::string oreName_
Definition: fxindex.hpp:155

◆ fixingDays()

Natural fixingDays ( ) const

Definition at line 127 of file fxindex.hpp.

127{ return fixingDays_; }

◆ fixingDate()

Date fixingDate ( const Date &  valueDate) const

Definition at line 278 of file fxindex.cpp.

278 {
279 Date fixingDate = fixingCalendar().advance(valueDate, -static_cast<Integer>(fixingDays_), Days);
280 return fixingDate;
281}
virtual Date valueDate(const Date &fixingDate) const
Definition: fxindex.cpp:283
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sourceCurrency()

const Currency & sourceCurrency ( ) const

Definition at line 129 of file fxindex.hpp.

129{ return sourceCurrency_; }

◆ targetCurrency()

const Currency & targetCurrency ( ) const

Definition at line 130 of file fxindex.hpp.

130{ return targetCurrency_; }

◆ sourceCurve()

const Handle< YieldTermStructure > & sourceCurve ( ) const

Definition at line 131 of file fxindex.hpp.

131{ return sourceYts_; }

◆ targetCurve()

const Handle< YieldTermStructure > & targetCurve ( ) const

Definition at line 132 of file fxindex.hpp.

132{ return targetYts_; }

◆ fxQuote()

const Handle< Quote > fxQuote ( bool  withSettlementLag = false) const

fxQuote returns instantaneous Quote by default, otherwise settlement after fixingDays

Definition at line 135 of file fxindex.cpp.

135 {
136 Handle<Quote> quote;
137 if (withSettlementLag || fixingDays_ == 0)
138 quote = fxSpot_;
139
140 if (quote.empty()) {
141 if (fxRate_.empty()) {
142 Handle<Quote> tmpQuote;
143 if (!useQuote_)
144 tmpQuote = Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(
145 ExchangeRateManager::instance().lookup(sourceCurrency_, targetCurrency_).rate()));
146 else
147 tmpQuote = fxSpot_;
148
149 // adjust for spot
150 fxRate_ = Handle<Quote>(
151 QuantLib::ext::make_shared<FxRateQuote>(tmpQuote, sourceYts_, targetYts_, fixingDays_, fixingCalendar_));
152 }
153 quote = fxRate_;
154 }
155 return quote;
156}
+ Here is the caller graph for this function:

◆ useQuote()

const bool useQuote ( ) const

Definition at line 136 of file fxindex.hpp.

136{ return useQuote_; }

◆ valueDate()

Date valueDate ( const Date &  fixingDate) const
virtual

Definition at line 283 of file fxindex.cpp.

283 {
284 QL_REQUIRE(isValidFixingDate(fixingDate),
285 "FxIndex::valueDate(): " << fixingDate << " is not a valid fixing date for " << name()
286 << " (calendar is " << fixingCalendar().name() << ")");
287 return fixingCalendar().advance(fixingDate, fixingDays_, Days);
288}
bool isValidFixingDate(const Date &fixingDate) const override
Definition: fxindex.cpp:271
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ forecastFixing() [1/2]

Real forecastFixing ( const Time &  fixingTime) const
overridevirtual

It can be overridden to implement particular conventions.

Implements EqFxIndexBase.

Definition at line 190 of file fxindex.cpp.

190 {
191 QL_REQUIRE(!sourceYts_.empty() && !targetYts_.empty(),
192 "FxIndex::forecastFixing(): null term structure set to this instance of " << name());
193
194 // we base the forecast always on the exchange rate (and not on today's fixing)
195 Real rate;
196 if (!useQuote_) {
197 rate = ExchangeRateManager::instance().lookup(sourceCurrency_, targetCurrency_).rate();
198 } else {
199 QL_REQUIRE(!fxSpot_.empty(), "FxIndex::forecastFixing(): fx quote required for " << name());
200 rate = fxSpot_->value();
201 }
202
203 // TODO: do we need to add this a class variable?
204 DayCounter dc = Actual365Fixed();
205
206 // to make the spot adjustment we get the time to spot, and also add this to fixing time
207 Date refDate = sourceYts_->referenceDate();
208 Date spotValueDate = valueDate(fixingCalendar().adjust(refDate));
209
210 // time from ref to spot date
211 Real spotTime = dc.yearFraction(refDate, spotValueDate);
212 Real forwardTime = spotTime + fixingTime;
213
214 QL_REQUIRE(forwardTime > 0.0 || close_enough(forwardTime, 0.0),
215 "FxIndex::forecastFixing(" << fixingTime << "): forwardTime (" << forwardTime << ") is negative for "
216 << name());
217
218 // compute the forecast applying the usual no arbitrage principle
219 Real forward = rate * sourceYts_->discount(forwardTime) * targetYts_->discount(spotTime) /
220 (targetYts_->discount(forwardTime) * sourceYts_->discount(spotTime));
221 return forward;
222}
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ forecastFixing() [2/2]

Real forecastFixing ( const Date &  fixingDate) const
virtual

Definition at line 224 of file fxindex.cpp.

224 {
225 QL_REQUIRE(!sourceYts_.empty() && !targetYts_.empty(), "null term structure set to this instance of " << name());
226
227 // we base the forecast always on the exchange rate (and not on today's
228 // fixing)
229 Real rate;
230 if (!useQuote_) {
231 rate = ExchangeRateManager::instance().lookup(sourceCurrency_, targetCurrency_).rate();
232 } else {
233 QL_REQUIRE(!fxSpot_.empty(), "FxIndex::forecastFixing(): fx quote required for " << name());
234 rate = fxSpot_->value();
235 }
236
237 // the exchange rate is interpreted as the spot rate w.r.t. the index's
238 // settlement date
239 Date refValueDate = valueDate(fixingCalendar().adjust(sourceYts_->referenceDate()));
240
241 // the fixing is obeying the settlement delay as well
242 Date fixingValueDate = valueDate(fixingDate);
243
244 // we can assume fixingValueDate >= valueDate
245 QL_REQUIRE(fixingValueDate >= refValueDate, "value date for requested fixing as of "
246 << fixingDate << " (" << fixingValueDate
247 << ") must be greater or equal to today's fixing value date ("
248 << refValueDate << ") for " << name());
249
250 // compute the forecast applying the usual no arbitrage principle
251 Real forward = rate * sourceYts_->discount(fixingValueDate) * targetYts_->discount(refValueDate) /
252 (sourceYts_->discount(refValueDate) * targetYts_->discount(fixingValueDate));
253
254 return forward;
255}
+ Here is the call graph for this function:

◆ pastFixing()

Real pastFixing ( const Date &  fixingDate) const
overridevirtual

returns a past fixing at the given date

the date passed as arguments must be the actual calendar date of the fixing; no settlement days must be used.

Implements EqFxIndexBase.

Definition at line 290 of file fxindex.cpp.

290 {
291 QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate << "FxIndex::pastFixing(): is not a valid fixing date for "
292 << name() << " (calendar is " << fixingCalendar().name()
293 << ")");
294
295 Real fixing = timeSeries()[fixingDate];
296 if (fixing != Null<Real>())
297 return fixing;
298
300 // check reverse
301 string revName = familyName_ + " " + targetCurrency_.code() + "/" + sourceCurrency_.code();
302 if (IndexManager::instance().hasHistoricalFixing(revName, fixingDate))
303 return 1.0 / IndexManager::instance().getHistory(revName)[fixingDate];
304
305 // Now we search for a pair of quotes that we can combine to construct the quote required.
306 // We only search for a pair of quotes a single step apart.
307 //
308 // Suppose we want a USDJPY quote and we have EUR based data, there are 4 combinations to
309 // consider:
310 // EURUSD, EURJPY => we want EURJPY / EURUSD [Triangulation]
311 // EURUSD, JPYEUR => we want 1 / (EURUSD * JPYEUR) [InverseProduct]
312 // USDEUR, EURJPY => we want USDEUR * EURJPY [Product]
313 // USDEUR, JPYEUR => we want USDEUR / JPYEUR [Triangulation (but in the reverse order)]
314 //
315 // Loop over the map, look for domestic then use the map to find the other side of the pair.
316
317 // Loop over all the possible Indexes
318 vector<string> availIndexes = IndexManager::instance().histories();
319 for (auto i : availIndexes) {
320 if (boost::starts_with(i, familyName_)) {
321 // check for a fixing
322 Real fixing = IndexManager::instance().getHistory(i)[fixingDate];
323 if (fixing != Null<Real>()) {
324
325 Size l = i.size();
326 string keyDomestic = i.substr(l - 7, 3);
327 string keyForeign = i.substr(l - 3);
328 string domestic = sourceCurrency_.code();
329 string foreign = targetCurrency_.code();
330
331 if (domestic == keyDomestic) {
332 // we have domestic, now look for foreign/keyForeign
333 // USDEUR, JPYEUR => we want USDEUR / JPYEUR [Triangulation (but in the reverse order)]
334 string forName = familyName_ + " " + foreign + "/" + keyForeign;
335 if (IndexManager::instance().hasHistoricalFixing(forName, fixingDate)) {
336 Real forFixing = IndexManager::instance().getHistory(forName)[fixingDate];
337 return fixing / forFixing;
338 }
339
340 // USDEUR, EURJPY => we want USDEUR * EURJPY [Product]
341 forName = familyName_ + " " + keyForeign + "/" + foreign;
342 if (IndexManager::instance().hasHistoricalFixing(forName, fixingDate)) {
343 Real forFixing = IndexManager::instance().getHistory(forName)[fixingDate];
344 return fixing * forFixing;
345 }
346 }
347
348 if (domestic == keyForeign) {
349 // we have fixing, now look for foreign/keyDomestic
350 // EURUSD, JPYEUR => we want 1 / (EURUSD * JPYEUR) [InverseProduct]
351 string forName = familyName_ + " " + foreign + "/" + keyDomestic;
352 if (IndexManager::instance().hasHistoricalFixing(forName, fixingDate)) {
353 Real forFixing = IndexManager::instance().getHistory(forName)[fixingDate];
354 return 1 / (fixing * forFixing);
355 }
356
357 // EURUSD, EURJPY => we want EURJPY / EURUSD [Triangulation]
358 forName = familyName_ + " " + keyDomestic + "/" + foreign;
359 if (IndexManager::instance().hasHistoricalFixing(forName, fixingDate)) {
360 Real forFixing = IndexManager::instance().getHistory(forName)[fixingDate];
361 return forFixing / fixing;
362 }
363 }
364 }
365 }
366 }
367 }
368 return fixing;
369}
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
Definition: fxindex.cpp:158
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clone()

QuantLib::ext::shared_ptr< FxIndex > clone ( const Handle< Quote >  fxQuote = Handle<Quote>(),
const Handle< YieldTermStructure > &  sourceYts = Handle<YieldTermStructure>(),
const Handle< YieldTermStructure > &  targetYts = Handle<YieldTermStructure>(),
const std::string &  familyName = std::string() 
)

clone the index, the clone will be linked to the provided handles

Definition at line 257 of file fxindex.cpp.

258 {
259 Handle<Quote> quote = fxQuote.empty() ? fxSpot_ : fxQuote;
260 Handle<YieldTermStructure> source = sourceYts.empty() ? sourceYts_ : sourceYts;
261 Handle<YieldTermStructure> target = targetYts.empty() ? targetYts_ : targetYts;
262 string famName = familyName.empty() ? familyName_ : familyName;
263 return QuantLib::ext::make_shared<FxIndex>(famName, fixingDays_, sourceCurrency_, targetCurrency_, fixingCalendar_, quote,
264 source, target);
265}
const Handle< Quote > fxQuote(bool withSettlementLag=false) const
fxQuote returns instantaneous Quote by default, otherwise settlement after fixingDays
Definition: fxindex.cpp:135
+ Here is the call graph for this function:

◆ initialise()

void initialise ( )
private

Definition at line 122 of file fxindex.cpp.

122 {
123 std::ostringstream tmp;
124 tmp << familyName_ << " " << sourceCurrency_.code() << "/" << targetCurrency_.code();
125 name_ = tmp.str();
126
127 oreName_ = "FX-" + familyName_ + "-" + sourceCurrency_.code() + "-" + targetCurrency_.code();
128
129 registerWith(IndexManager::instance().notifier(name()));
130 registerWith(fxSpot_);
131 registerWith(sourceYts_);
132 registerWith(targetYts_);
133}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ familyName_

std::string familyName_
protected

Definition at line 155 of file fxindex.hpp.

◆ oreName_

std::string oreName_
protected

Definition at line 155 of file fxindex.hpp.

◆ fixingDays_

Natural fixingDays_
protected

Definition at line 156 of file fxindex.hpp.

◆ sourceCurrency_

Currency sourceCurrency_
protected

Definition at line 157 of file fxindex.hpp.

◆ targetCurrency_

Currency targetCurrency_
protected

Definition at line 157 of file fxindex.hpp.

◆ sourceYts_

const Handle<YieldTermStructure> sourceYts_
protected

Definition at line 158 of file fxindex.hpp.

◆ targetYts_

const Handle<YieldTermStructure> targetYts_
protected

Definition at line 158 of file fxindex.hpp.

◆ name_

std::string name_
protected

Definition at line 159 of file fxindex.hpp.

◆ fxSpot_

const Handle<Quote> fxSpot_
protected

Definition at line 161 of file fxindex.hpp.

◆ fxRate_

Handle<Quote> fxRate_
mutableprotected

Definition at line 163 of file fxindex.hpp.

◆ useQuote_

bool useQuote_
protected

Definition at line 164 of file fxindex.hpp.

◆ fixingCalendar_

Calendar fixingCalendar_
private

Definition at line 167 of file fxindex.hpp.

◆ fixingTriangulation_

bool fixingTriangulation_
private

Definition at line 168 of file fxindex.hpp.