-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathmarketimpl.cpp
More file actions
638 lines (577 loc) · 31.3 KB
/
marketimpl.cpp
File metadata and controls
638 lines (577 loc) · 31.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
Copyright (C) 2016 Quaternion Risk Management Ltd
All rights reserved.
This file is part of ORE, a free-software/open-source library
for transparent pricing and risk analysis - http://opensourcerisk.org
ORE is free software: you can redistribute it and/or modify it
under the terms of the Modified BSD License. You should have received a
copy of the license along with this program.
The license is also available online at <http://opensourcerisk.org>
This program is distributed on the basis that it will form a useful
contribution to risk analytics and model standardisation, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <boost/algorithm/string.hpp>
#include <boost/make_shared.hpp>
#include <ored/configuration/conventions.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/utilities/indexparser.hpp>
#include <ored/utilities/marketdata.hpp>
#include <ored/utilities/parsers.hpp>
#include <qle/termstructures/blackinvertedvoltermstructure.hpp>
using namespace std;
using std::make_pair;
using std::map;
using std::string;
using namespace QuantLib;
using QuantExt::PriceTermStructure;
using QuantExt::CommodityIndex;
using QuantExt::FxIndex;
namespace ore {
namespace data {
namespace {
template <class A, class B, class C>
A lookup(const B& map, const C& key, const string& configuration, const string& type, bool continueOnError = false) {
auto it = map.find(make_pair(configuration, key));
if (it == map.end()) {
// fall back to default configuration
it = map.find(make_pair(Market::defaultConfiguration, key));
if (it == map.end()) {
if (!continueOnError)
QL_FAIL("did not find object '" << key << "' of type " << type
<< " under configuration '" << configuration << "' or 'default'");
else
return A();
}
}
return it->second;
}
template <class A, class B, class C>
A lookup(const B& map, const C& key, const YieldCurveType y, const string& configuration, const string& type) {
auto it = map.find(make_tuple(configuration, y, key));
if (it == map.end()) {
// fall back to default configuration
it = map.find(make_tuple(Market::defaultConfiguration, y, key));
QL_REQUIRE(it != map.end(), "did not find object " << key << " of type " << type << " under configuration '"
<< configuration << "' or 'default' in YieldCurves");
}
return it->second;
}
} // anonymous namespace
Handle<YieldTermStructure> MarketImpl::yieldCurve(const YieldCurveType& type, const string& key,
const string& configuration) const {
// we allow for standard (i.e. not convention based) ibor index names as keys and return the index forward curve in
// case of a match
QuantLib::ext::shared_ptr<IborIndex> notUsed;
if (tryParseIborIndex(key, notUsed)) {
return iborIndex(key, configuration)->forwardingTermStructure();
}
// no ibor index found under key => look for a genuine yield curve
DLOG("no ibor index found under '" << key << "' - look for a genuine yield curve");
if (type == YieldCurveType::Discount)
require(MarketObject::DiscountCurve, key, configuration);
else if (type == YieldCurveType::Yield)
require(MarketObject::YieldCurve, key, configuration);
else if (type == YieldCurveType::EquityDividend)
require(MarketObject::EquityCurve, key, configuration);
else {
QL_FAIL("yield curve type not handled");
}
return lookup<Handle<YieldTermStructure>>(yieldCurves_, key, type, configuration, "yield curve / ibor index");
}
Handle<YieldTermStructure> MarketImpl::discountCurveImpl(const string& key, const string& configuration) const {
require(MarketObject::DiscountCurve, key, configuration);
return lookup<Handle<YieldTermStructure>>(yieldCurves_, key, YieldCurveType::Discount, configuration,
"discount curve");
}
Handle<YieldTermStructure> MarketImpl::yieldCurve(const string& key, const string& configuration) const {
require(MarketObject::YieldCurve, key, configuration);
return yieldCurve(YieldCurveType::Yield, key, configuration);
}
Handle<IborIndex> MarketImpl::iborIndex(const string& key, const string& configuration) const {
require(MarketObject::IndexCurve, key, configuration);
return lookup<Handle<IborIndex>>(iborIndices_, key, configuration, "ibor index");
}
Handle<SwapIndex> MarketImpl::swapIndex(const string& key, const string& configuration) const {
require(MarketObject::SwapIndexCurve, key, configuration);
return lookup<Handle<SwapIndex>>(swapIndices_, key, configuration, "swap index");
}
Handle<QuantLib::SwaptionVolatilityStructure> MarketImpl::swaptionVol(const string& key,
const string& configuration) const {
require(MarketObject::SwaptionVol, key, configuration);
auto it = swaptionCurves_.find(make_pair(configuration, key));
if (it != swaptionCurves_.end())
return it->second;
// try the default config with the same key
if (configuration != Market::defaultConfiguration) {
require(MarketObject::SwaptionVol, key, Market::defaultConfiguration);
auto it2 = swaptionCurves_.find(make_pair(Market::defaultConfiguration, key));
if (it2 != swaptionCurves_.end())
return it2->second;
}
// if key is an index name and we have a swaption curve for its ccy, we return that
QuantLib::ext::shared_ptr<IborIndex> index;
if (!tryParseIborIndex(key, index)) {
QL_FAIL("did not find swaption curve for key '" << key << "'");
}
auto ccy = index->currency().code();
require(MarketObject::SwaptionVol, ccy, configuration);
auto it3 = swaptionCurves_.find(make_pair(configuration, ccy));
if (it3 != swaptionCurves_.end()) {
return it3->second;
}
// check if we have a curve for the ccy in the default config
if (configuration != Market::defaultConfiguration) {
require(MarketObject::SwaptionVol, ccy, Market::defaultConfiguration);
auto it4 = swaptionCurves_.find(make_pair(Market::defaultConfiguration, ccy));
if (it4 != swaptionCurves_.end())
return it4->second;
}
QL_FAIL("did not find swaption curve for key '" << key << "'");
}
pair<string, string> MarketImpl::swapIndexBases(const string& key, const string& configuration) const {
require(MarketObject::SwaptionVol, key, configuration);
auto it = swaptionIndexBases_.find(make_pair(configuration, key));
if (it != swaptionIndexBases_.end())
return it->second;
// try the default config with the same key
if (configuration != Market::defaultConfiguration) {
require(MarketObject::SwaptionVol, key, Market::defaultConfiguration);
auto it2 = swaptionIndexBases_.find(make_pair(Market::defaultConfiguration, key));
if (it2 != swaptionIndexBases_.end())
return it2->second;
}
// if key is an index name and we have a swaption curve for its ccy, we return that
QuantLib::ext::shared_ptr<IborIndex> index;
if (!tryParseIborIndex(key, index)) {
QL_FAIL("did not find swaption index bases for key '" << key << "'");
}
auto ccy = index->currency().code();
require(MarketObject::SwaptionVol, ccy, configuration);
auto it3 = swaptionIndexBases_.find(make_pair(configuration, ccy));
if (it3 != swaptionIndexBases_.end()) {
return it3->second;
}
// check if we have a curve for the ccy in the default config
if (configuration != Market::defaultConfiguration) {
require(MarketObject::SwaptionVol, ccy, configuration);
auto it4 = swaptionIndexBases_.find(make_pair(Market::defaultConfiguration, ccy));
if (it4 != swaptionIndexBases_.end())
return it4->second;
}
QL_FAIL("did not find swaption index bases for key '" << key << "'");
}
string MarketImpl::shortSwapIndexBase(const string& key, const string& configuration) const {
return swapIndexBases(key, configuration).first;
}
string MarketImpl::swapIndexBase(const string& key, const string& configuration) const {
return swapIndexBases(key, configuration).second;
}
Handle<QuantLib::SwaptionVolatilityStructure> MarketImpl::yieldVol(const string& key,
const string& configuration) const {
require(MarketObject::YieldVol, key, configuration);
return lookup<Handle<QuantLib::SwaptionVolatilityStructure>>(yieldVolCurves_, key, configuration,
"yield volatility curve");
}
Handle<QuantExt::FxIndex> MarketImpl::fxIndexImpl(const string& fxIndex, const string& configuration) const {
QL_REQUIRE(fx_ != nullptr,
"MarketImpl::fxIndex(" << fxIndex << "): fx_ is null. This is an internal error. Contact dev.");
return fx_->getIndex(fxIndex, this, configuration);
}
Handle<Quote> MarketImpl::fxRateImpl(const string& ccypair, const string& configuration) const {
// if rate requested for a currency against itself, return 1.0
if (ccypair.substr(0,3) == ccypair.substr(3))
return Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
return fxIndex(ccypair, configuration)->fxQuote();
}
Handle<Quote> MarketImpl::fxSpotImpl(const string& ccypair, const string& configuration) const {
if (ccypair.substr(0, 3) == ccypair.substr(3))
return Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
return fxIndex(ccypair, configuration)->fxQuote(true);
}
Handle<BlackVolTermStructure> MarketImpl::fxVolImpl(const string& ccypair, const string& configuration) const {
require(MarketObject::FXVol, ccypair, configuration);
auto it = fxVols_.find(make_pair(configuration, ccypair));
if (it != fxVols_.end())
return it->second;
else {
// check for reverse EURUSD or USDEUR and add to the map
QL_REQUIRE(ccypair.length() == 6, "invalid ccy pair length");
std::string ccypairInverted = ccypair.substr(3, 3) + ccypair.substr(0, 3);
require(MarketObject::FXVol, ccypairInverted, configuration);
it = fxVols_.find(make_pair(configuration, ccypairInverted));
if (it != fxVols_.end()) {
Handle<BlackVolTermStructure> h(QuantLib::ext::make_shared<QuantExt::BlackInvertedVolTermStructure>(it->second));
h->enableExtrapolation();
// we have found a surface for the inverted pair.
// so we can invert the surface and store that under the original pair.
fxVols_[make_pair(configuration, ccypair)] = h;
return h;
} else {
if (configuration == Market::defaultConfiguration)
QL_FAIL("did not find fx vol object " << ccypair);
else
return fxVol(ccypair, Market::defaultConfiguration);
}
}
}
Handle<QuantExt::CreditCurve> MarketImpl::defaultCurve(const string& key, const string& configuration) const {
require(MarketObject::DefaultCurve, key, configuration);
return lookup<Handle<QuantExt::CreditCurve>>(defaultCurves_, key, configuration, "default curve");
}
Handle<Quote> MarketImpl::recoveryRate(const string& key, const string& configuration) const {
// recovery rates can be built together with default curve or securities
require(MarketObject::DefaultCurve, key, configuration);
require(MarketObject::Security, key, configuration);
return lookup<Handle<Quote>>(recoveryRates_, key, configuration, "recovery rate");
}
Handle<Quote> MarketImpl::conversionFactor(const string& key, const string& configuration) const {
require(MarketObject::Security, key, configuration);
return lookup<Handle<Quote>>(conversionFactors_, key, configuration, "conversion factor");
}
Handle<Quote> MarketImpl::securityPrice(const string& key, const string& configuration) const {
require(MarketObject::Security, key, configuration);
return lookup<Handle<Quote>>(securityPrices_, key, configuration, "security price");
}
Handle<QuantExt::CreditVolCurve> MarketImpl::cdsVol(const string& key, const string& configuration) const {
require(MarketObject::CDSVol, key, configuration);
return lookup<Handle<QuantExt::CreditVolCurve>>(cdsVols_, key, configuration, "cds vol curve");
}
Handle<QuantExt::BaseCorrelationTermStructure>
MarketImpl::baseCorrelation(const string& key, const string& configuration) const {
require(MarketObject::BaseCorrelation, key, configuration);
return lookup<Handle<QuantExt::BaseCorrelationTermStructure>>(baseCorrelations_, key, configuration,
"base correlation curve");
}
Handle<OptionletVolatilityStructure> MarketImpl::capFloorVol(const string& key, const string& configuration) const {
require(MarketObject::CapFloorVol, key, configuration);
auto it = capFloorCurves_.find(make_pair(configuration, key));
if (it != capFloorCurves_.end())
return it->second;
// first try the default config with the same key
if (configuration != Market::defaultConfiguration) {
require(MarketObject::CapFloorVol, key, Market::defaultConfiguration);
auto it2 = capFloorCurves_.find(make_pair(Market::defaultConfiguration, key));
if (it2 != capFloorCurves_.end())
return it2->second;
}
// if key is an index name and we have a cap floor surface for its ccy, we return that
QuantLib::ext::shared_ptr<IborIndex> index;
if (!tryParseIborIndex(key, index)) {
QL_FAIL("did not find capfloor curve for key '" << key << "'");
}
auto ccy = index->currency().code();
require(MarketObject::CapFloorVol, ccy, configuration);
auto it3 = capFloorCurves_.find(make_pair(configuration, ccy));
if (it3 != capFloorCurves_.end()) {
return it3->second;
}
// check if we have a curve for the ccy in the default config
if (configuration != Market::defaultConfiguration) {
require(MarketObject::CapFloorVol, ccy, configuration);
auto it4 = capFloorCurves_.find(make_pair(Market::defaultConfiguration, ccy));
if (it4 != capFloorCurves_.end())
return it4->second;
}
QL_FAIL("did not find capfloor curve for key '" << key << "'");
}
std::pair<string, QuantLib::Period> MarketImpl::capFloorVolIndexBase(const string& key,
const string& configuration) const {
require(MarketObject::CapFloorVol, key, configuration);
auto it = capFloorIndexBase_.find(make_pair(configuration, key));
if (it != capFloorIndexBase_.end())
return it->second;
// first try the default config with the same key
if (configuration != Market::defaultConfiguration) {
require(MarketObject::CapFloorVol, key, Market::defaultConfiguration);
auto it2 = capFloorIndexBase_.find(make_pair(Market::defaultConfiguration, key));
if (it2 != capFloorIndexBase_.end())
return it2->second;
}
// if key is an index name and we have a cap floor surface for its ccy, we return that
QuantLib::ext::shared_ptr<IborIndex> index;
if (!tryParseIborIndex(key, index)) {
return std::make_pair(string(),0*Days);
}
auto ccy = index->currency().code();
require(MarketObject::CapFloorVol, ccy, configuration);
auto it3 = capFloorIndexBase_.find(make_pair(configuration, ccy));
if (it3 != capFloorIndexBase_.end()) {
return it3->second;
}
// check if we have a curve for the ccy in the default config
if (configuration != Market::defaultConfiguration) {
require(MarketObject::CapFloorVol, ccy, configuration);
auto it4 = capFloorIndexBase_.find(make_pair(Market::defaultConfiguration, ccy));
if (it4 != capFloorIndexBase_.end())
return it4->second;
}
return std::make_pair(string(), 0 * Days);
}
Handle<YoYOptionletVolatilitySurface> MarketImpl::yoyCapFloorVol(const string& key, const string& configuration) const {
require(MarketObject::YoYInflationCapFloorVol, key, configuration);
return lookup<Handle<YoYOptionletVolatilitySurface>>(yoyCapFloorVolSurfaces_, key, configuration,
"yoy inflation capfloor curve");
}
Handle<ZeroInflationIndex> MarketImpl::zeroInflationIndex(const string& indexName, const string& configuration) const {
require(MarketObject::ZeroInflationCurve, indexName, configuration);
return lookup<Handle<ZeroInflationIndex>>(zeroInflationIndices_, indexName, configuration, "zero inflation index");
}
Handle<YoYInflationIndex> MarketImpl::yoyInflationIndex(const string& indexName, const string& configuration) const {
require(MarketObject::YoYInflationCurve, indexName, configuration);
return lookup<Handle<YoYInflationIndex>>(yoyInflationIndices_, indexName, configuration, "yoy inflation index");
}
Handle<CPIVolatilitySurface> MarketImpl::cpiInflationCapFloorVolatilitySurface(const string& indexName,
const string& configuration) const {
require(MarketObject::ZeroInflationCapFloorVol, indexName, configuration);
return lookup<Handle<CPIVolatilitySurface>>(cpiInflationCapFloorVolatilitySurfaces_, indexName, configuration,
"cpi cap floor volatility surface");
}
Handle<Quote> MarketImpl::equitySpot(const string& key, const string& configuration) const {
require(MarketObject::EquityCurve, key, configuration);
return lookup<Handle<Quote>>(equitySpots_, key, configuration, "equity spot");
}
Handle<QuantExt::EquityIndex2> MarketImpl::equityCurve(const string& key, const string& configuration) const {
require(MarketObject::EquityCurve, key, configuration);
return lookup<Handle<QuantExt::EquityIndex2>>(equityCurves_, key, configuration, "equity curve");
};
Handle<YieldTermStructure> MarketImpl::equityDividendCurve(const string& key, const string& configuration) const {
require(MarketObject::EquityCurve, key, configuration);
return lookup<Handle<YieldTermStructure>>(yieldCurves_, key, YieldCurveType::EquityDividend, configuration,
"dividend yield curve");
}
Handle<BlackVolTermStructure> MarketImpl::equityVol(const string& key, const string& configuration) const {
require(MarketObject::EquityVol, key, configuration);
return lookup<Handle<BlackVolTermStructure>>(equityVols_, key, configuration, "equity vol curve");
}
Handle<YieldTermStructure> MarketImpl::equityForecastCurve(const string& eqName, const string& configuration) const {
require(MarketObject::EquityCurve, eqName, configuration);
return equityCurve(eqName, configuration)->equityForecastCurve();
}
Handle<Quote> MarketImpl::securitySpread(const string& key, const string& configuration) const {
require(MarketObject::Security, key, configuration);
return lookup<Handle<Quote>>(securitySpreads_, key, configuration, "security spread");
}
Handle<QuantExt::InflationIndexObserver> MarketImpl::baseCpis(const string& key, const string& configuration) const {
require(MarketObject::ZeroInflationCurve, key, configuration);
return lookup<Handle<QuantExt::InflationIndexObserver>>(baseCpis_, key, configuration, "base CPI");
}
Handle<PriceTermStructure> MarketImpl::commodityPriceCurve(const string& commodityName,
const string& configuration) const {
return commodityIndex(commodityName, configuration)->priceCurve();
}
Handle<CommodityIndex> MarketImpl::commodityIndex(const string& commodityName, const string& configuration) const {
require(MarketObject::CommodityCurve, commodityName, configuration);
return lookup<Handle<CommodityIndex>>(commodityIndices_, commodityName, configuration, "commodity indices");
}
Handle<BlackVolTermStructure> MarketImpl::commodityVolatility(const string& commodityName,
const string& configuration) const {
require(MarketObject::CommodityVolatility, commodityName, configuration);
return lookup<Handle<BlackVolTermStructure>>(commodityVols_, commodityName, configuration, "commodity volatility");
}
Handle<QuantExt::CorrelationTermStructure> MarketImpl::correlationCurve(const string& index1, const string& index2,
const string& configuration) const {
// straight pair
require(MarketObject::Correlation, index1 + "&" + index2, configuration);
auto it = correlationCurves_.find(make_tuple(configuration, index1, index2));
if (it != correlationCurves_.end())
return it->second;
// inverse pair
require(MarketObject::Correlation, index2 + "&" + index1, configuration);
it = correlationCurves_.find(make_tuple(configuration, index2, index1));
if (it != correlationCurves_.end())
return it->second;
// inverse fx index1
if (isFxIndex(index1)) {
require(MarketObject::Correlation, inverseFxIndex(index1) + "&" + index2, configuration);
it = correlationCurves_.find(make_tuple(configuration, inverseFxIndex(index1), index2));
if (it != correlationCurves_.end())
return Handle<QuantExt::CorrelationTermStructure>(
QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
require(MarketObject::Correlation, index2 + "&" + inverseFxIndex(index1), configuration);
it = correlationCurves_.find(make_tuple(configuration, index2, inverseFxIndex(index1)));
if (it != correlationCurves_.end())
return Handle<QuantExt::CorrelationTermStructure>(
QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
}
// inverse fx index2
if (isFxIndex(index2)) {
require(MarketObject::Correlation, index1 + "&" + inverseFxIndex(index2), configuration);
it = correlationCurves_.find(make_tuple(configuration, index1, inverseFxIndex(index2)));
if (it != correlationCurves_.end())
return Handle<QuantExt::CorrelationTermStructure>(
QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
require(MarketObject::Correlation, inverseFxIndex(index2) + "&" + index1, configuration);
it = correlationCurves_.find(make_tuple(configuration, inverseFxIndex(index2), index1));
if (it != correlationCurves_.end())
return Handle<QuantExt::CorrelationTermStructure>(
QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
}
// both fx indices inverted
if (isFxIndex(index1) && isFxIndex(index2)) {
require(MarketObject::Correlation, inverseFxIndex(index1) + "&" + inverseFxIndex(index2), configuration);
it = correlationCurves_.find(make_tuple(configuration, inverseFxIndex(index1), inverseFxIndex(index2)));
if (it != correlationCurves_.end())
return it->second;
require(MarketObject::Correlation, inverseFxIndex(index2) + "&" + inverseFxIndex(index1), configuration);
it = correlationCurves_.find(make_tuple(configuration, inverseFxIndex(index2), inverseFxIndex(index1)));
if (it != correlationCurves_.end())
return it->second;
}
// if not found, fall back to default configuration
if (configuration == Market::defaultConfiguration) {
QL_FAIL("did not find object " << index1 << "/" << index2 << " in CorrelationCurves");
} else {
return correlationCurve(index1, index2, Market::defaultConfiguration);
}
}
Handle<Quote> MarketImpl::cpr(const string& securityID, const string& configuration) const {
require(MarketObject::Security, securityID, configuration);
return lookup<Handle<Quote>>(cprs_, securityID, configuration, "cpr");
}
void MarketImpl::addSwapIndex(const string& swapIndex, const string& discountIndex, const string& configuration) const {
if (swapIndices_.find(make_pair(configuration, swapIndex)) != swapIndices_.end())
return;
try {
std::vector<string> tokens;
split(tokens, swapIndex, boost::is_any_of("-"));
QL_REQUIRE(tokens.size() == 3 || tokens.size() == 4,
"three or four tokens required in " << swapIndex << ": CCY-CMS-TENOR or CCY-CMS-TAG-TENOR");
QL_REQUIRE(tokens[0].size() == 3, "invalid currency code in " << swapIndex);
QL_REQUIRE(tokens[1] == "CMS", "expected CMS as second token in " << swapIndex);
Handle<YieldTermStructure> discounting, forwarding;
QuantLib::ext::shared_ptr<IborIndex> dummeyIndex;
if (tryParseIborIndex(discountIndex, dummeyIndex))
discounting = iborIndex(discountIndex, configuration)->forwardingTermStructure();
else
discounting = yieldCurve(discountIndex, configuration);
const QuantLib::ext::shared_ptr<Conventions>& conventions = InstrumentConventions::instance().conventions();
auto swapCon = QuantLib::ext::dynamic_pointer_cast<data::SwapIndexConvention>(conventions->get(swapIndex));
QL_REQUIRE(swapCon, "expected SwapIndexConvention for " << swapIndex);
auto conIbor = QuantLib::ext::dynamic_pointer_cast<data::IRSwapConvention>(conventions->get(swapCon->conventions()));
auto conOisComp = QuantLib::ext::dynamic_pointer_cast<data::OisConvention>(conventions->get(swapCon->conventions()));
auto conOisAvg = QuantLib::ext::dynamic_pointer_cast<data::AverageOisConvention>(conventions->get(swapCon->conventions()));
QL_REQUIRE(conIbor || conOisComp || conOisAvg,
"expected IRSwapConvention, OisConvention, AverageOisConvention for " << swapCon->conventions());
string fi = conIbor ? conIbor->indexName() : (conOisComp ? conOisComp->indexName() : conOisAvg->indexName());
if (isGenericIborIndex(fi))
forwarding = discounting;
else
forwarding = iborIndex(fi, configuration)->forwardingTermStructure();
QuantLib::ext::shared_ptr<SwapIndex> si = data::parseSwapIndex(swapIndex, forwarding, discounting);
swapIndices_[make_pair(configuration, swapIndex)] = Handle<SwapIndex>(si);
} catch (std::exception& e) {
QL_FAIL("Failure in MarketImpl::addSwapIndex() with index " << swapIndex << " : " << e.what());
}
}
void MarketImpl::refresh(const string& configuration) {
auto it = refreshTs_.find(configuration);
if (it == refreshTs_.end()) {
it = refreshTs_.insert(make_pair(configuration, std::set<QuantLib::ext::shared_ptr<TermStructure>>())).first;
}
if (it->second.empty()) {
for (auto& x : yieldCurves_) {
if (get<0>(x.first) == configuration || get<0>(x.first) == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : iborIndices_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
Handle<YieldTermStructure> y = x.second->forwardingTermStructure();
if (!y.empty())
it->second.insert(*y);
}
}
for (auto& x : swapIndices_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
Handle<YieldTermStructure> y = x.second->forwardingTermStructure();
if (!y.empty())
it->second.insert(*y);
y = x.second->discountingTermStructure();
if (!y.empty())
it->second.insert(*y);
}
}
for (auto& x : swaptionCurves_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : capFloorCurves_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : yoyCapFloorVolSurfaces_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : fxVols_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : defaultCurves_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second->curve());
}
for (auto& x : cdsVols_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : baseCorrelations_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : zeroInflationIndices_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
it->second.insert(*x.second->zeroInflationTermStructure());
}
}
for (auto& x : yoyInflationIndices_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
it->second.insert(*x.second->yoyInflationTermStructure());
}
}
for (auto& x : cpiInflationCapFloorVolatilitySurfaces_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : yoyCapFloorVolSurfaces_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : equityVols_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : equityCurves_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
Handle<YieldTermStructure> y = x.second->equityForecastCurve();
if (!y.empty())
it->second.insert(*y);
y = x.second->equityDividendCurve();
if (!y.empty())
it->second.insert(*y);
}
}
for (auto& x : commodityIndices_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration) {
const auto& pts = x.second->priceCurve();
if (!pts.empty())
it->second.insert(*pts);
}
}
for (auto& x : commodityVols_) {
if (x.first.first == configuration || x.first.first == Market::defaultConfiguration)
it->second.insert(*x.second);
}
for (auto& x : correlationCurves_) {
if (get<0>(x.first) == configuration || get<0>(x.first) == Market::defaultConfiguration)
it->second.insert(*x.second);
}
}
// term structures might be wrappers around nested termstructures that need to be updated as well,
// therefore we need to call deepUpdate() (=update() if no such nesting is present)
for (auto& x : it->second)
x->deepUpdate();
} // refresh
} // namespace data
} // namespace ore