Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
curvespecparser.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/errors.hpp>
22
23#include <boost/algorithm/string.hpp>
24#include <boost/make_shared.hpp>
25#include <boost/tokenizer.hpp>
26
27#include <map>
28#include <vector>
29
30using namespace std;
31
32namespace ore {
33namespace data {
34
35static CurveSpec::CurveType parseCurveSpecType(const string& s) {
36 static map<string, CurveSpec::CurveType> b = {
38 {"CapFloorVolatility", CurveSpec::CurveType::CapFloorVolatility},
39 {"SwaptionVolatility", CurveSpec::CurveType::SwaptionVolatility},
40 {"YieldVolatility", CurveSpec::CurveType::YieldVolatility},
42 {"FXVolatility", CurveSpec::CurveType::FXVolatility},
44 {"CDSVolatility", CurveSpec::CurveType::CDSVolatility},
45 {"BaseCorrelation", CurveSpec::CurveType::BaseCorrelation},
47 {"InflationCapFloorVolatility", CurveSpec::CurveType::InflationCapFloorVolatility},
49 {"EquityVolatility", CurveSpec::CurveType::EquityVolatility},
52 {"Correlation", CurveSpec::CurveType::Correlation},
53 {"CommodityVolatility", CurveSpec::CurveType::CommodityVolatility}};
54
55 auto it = b.find(s);
56 if (it != b.end()) {
57 return it->second;
58 } else {
59 QL_FAIL("Cannot convert \"" << s << "\" to CurveSpecType");
60 }
61}
62
63//! function to convert a string into a curve spec
64QuantLib::ext::shared_ptr<CurveSpec> parseCurveSpec(const string& s) {
65
66
67 boost::escaped_list_separator<char> sep('\\', '/', '\"');
68 boost::tokenizer<boost::escaped_list_separator<char> > tokenSplit(s, sep);
69
70 vector<string> tokens(tokenSplit.begin(), tokenSplit.end());
71
72 QL_REQUIRE(tokens.size() > 1, "number of tokens too small in curve spec " << s);
73
74 CurveSpec::CurveType curveType = parseCurveSpecType(tokens[0]);
75
76 switch (curveType) {
77
79 // Expected format: Yield/CCY/CurveConfigID
80 // Example: Yield/EUR/eur-6M-curve
81 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
82 " of tokens in yield curve spec "
83 << s);
84 const string& ccy = tokens[1];
85 const string& curveConfigID = tokens[2];
86 return QuantLib::ext::make_shared<YieldCurveSpec>(ccy, curveConfigID);
87 }
88
90 // Default/USD/CurveConfigID
91 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
92 " of tokens in default curve spec "
93 << s);
94 const string& ccy = tokens[1];
95 const string& curveConfigID = tokens[2];
96 return QuantLib::ext::make_shared<DefaultCurveSpec>(ccy, curveConfigID);
97 }
98
100 // CDSVolatility/CurveConfigID
101 QL_REQUIRE(tokens.size() == 2, "Unexpected number"
102 " of tokens in cds vol spec "
103 << s);
104 const string& curveConfigID = tokens[1];
105 return QuantLib::ext::make_shared<CDSVolatilityCurveSpec>(curveConfigID);
106 }
107
109 // BaseCorrelation/CurveConfigID
110 QL_REQUIRE(tokens.size() == 2, "Unexpected number"
111 " of tokens in cds vol spec "
112 << s);
113 const string& curveConfigID = tokens[1];
114 return QuantLib::ext::make_shared<BaseCorrelationCurveSpec>(curveConfigID);
115 }
116
118 // FX/USD/CHF
119 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
120 " of tokens in FX curve spec "
121 << s);
122 const string& unitCcy = tokens[1];
123 const string& ccy = tokens[2];
124 return QuantLib::ext::make_shared<FXSpotSpec>(unitCcy, ccy);
125 }
126
128 // FX/USD/CHF/CurveConfigID
129 QL_REQUIRE(tokens.size() == 4, "Unexpected number"
130 " of tokens in fx vol curve spec "
131 << s);
132 const string& unitCcy = tokens[1];
133 const string& ccy = tokens[2];
134 const string& curveConfigID = tokens[3];
135 return QuantLib::ext::make_shared<FXVolatilityCurveSpec>(unitCcy, ccy, curveConfigID);
136 }
137
139 // SwaptionVolatility/EUR/CurveConfigID
140 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
141 " of tokens in swaption vol curve spec "
142 << s);
143 const string& key = tokens[1];
144 const string& curveConfigID = tokens[2];
145 return QuantLib::ext::make_shared<SwaptionVolatilityCurveSpec>(key, curveConfigID);
146 }
147
149 // YieldVolatility/CurveConfigID
150 QL_REQUIRE(tokens.size() == 2, "Unexpected number"
151 " of tokens in yield vol curve spec "
152 << s);
153 const string& curveConfigID = tokens[1];
154 return QuantLib::ext::make_shared<YieldVolatilityCurveSpec>(curveConfigID);
155 }
156
158 // e.g. CapFloorVolatility/EUR-EURIBOR-3M/CurveConfigID
159 // CapFloorVolatility/EUR-ESTER/CurveConfigID
160 // CapFloorVolatility/EUR/CurveConfigID
161 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
162 " of tokens in CapFloor volatility curve spec "
163 << s);
164 const string& key = tokens[1];
165 const string& curveConfigID = tokens[2];
166 return QuantLib::ext::make_shared<CapFloorVolatilityCurveSpec>(key, curveConfigID);
167 }
168
170 // Inflation/EUHICPXT/CurveConfigID
171 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
172 " of tokens in inflation curve spec "
173 << s);
174 const string& index = tokens[1];
175 const string& curveConfigID = tokens[2];
176 return QuantLib::ext::make_shared<InflationCurveSpec>(index, curveConfigID);
177 }
178
180 // e.g. InflationCapFloorVolatility/EUHICPXT/CurveConfigID
181 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
182 " of tokens in InflationCapFloor volatility curve spec "
183 << s);
184 const string& index = tokens[1];
185 const string& curveConfigID = tokens[2];
186 return QuantLib::ext::make_shared<InflationCapFloorVolatilityCurveSpec>(index, curveConfigID);
187 }
188
190 // Equity/USD/CurveConfigID
191 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
192 " of tokens in default curve spec "
193 << s);
194 const string& ccy = tokens[1];
195 const string& curveConfigID = tokens[2];
196 return QuantLib::ext::make_shared<EquityCurveSpec>(ccy, curveConfigID);
197 }
198
200 // EquityVolatility/USD/CurveConfigID
201 QL_REQUIRE(tokens.size() == 3, "Unexpected number"
202 " of tokens in default curve spec "
203 << s);
204 const string& ccy = tokens[1];
205 const string& curveConfigID = tokens[2];
206 return QuantLib::ext::make_shared<EquityVolatilityCurveSpec>(ccy, curveConfigID);
207 }
208
210 // Security/ISIN
211 QL_REQUIRE(tokens.size() == 2, "Unexpected number"
212 " of tokens in Security Spread spec "
213 << s);
214 const string& securityID = tokens[1];
215 return QuantLib::ext::make_shared<SecuritySpec>(securityID);
216 }
217
219 // Commodity/CCY/CommodityCurveConfigId
220 QL_REQUIRE(tokens.size() == 3, "Unexpected number of tokens in commodity curve spec " << s);
221 return QuantLib::ext::make_shared<CommodityCurveSpec>(tokens[1], tokens[2]);
222 }
223
225 // CommodityVolatility/CCY/CommodityVolatilityConfigId
226 QL_REQUIRE(tokens.size() == 3, "Unexpected number of tokens in commodity volatility spec " << s);
227 return QuantLib::ext::make_shared<CommodityVolatilityCurveSpec>(tokens[1], tokens[2]);
228 }
229
231 // Correlation/CorrelationCurveConfigId
232 QL_REQUIRE(tokens.size() == 2, "Unexpected number of tokens in correlation spec " << s);
233 string id = tokens[1];
234 return QuantLib::ext::make_shared<CorrelationCurveSpec>(id);
235 }
236
237 // TODO: the rest...
238 }
239
240 QL_FAIL("Unable to convert \"" << s << "\" into CurveSpec");
241}
242
243
245 static const map<string, CurveSpec::CurveType> b = {
246 {"YieldCurves", CurveSpec::CurveType::Yield},
247 {"CapFloorVolatilities", CurveSpec::CurveType::CapFloorVolatility},
248 {"SwaptionVolatilities", CurveSpec::CurveType::SwaptionVolatility},
249 {"YieldVolatilities", CurveSpec::CurveType::YieldVolatility},
250 {"FXSpots", CurveSpec::CurveType::FX},
251 {"FXVolatilities", CurveSpec::CurveType::FXVolatility},
252 {"DefaultCurves", CurveSpec::CurveType::Default},
253 {"CDSVolatilities", CurveSpec::CurveType::CDSVolatility},
254 {"BaseCorrelations", CurveSpec::CurveType::BaseCorrelation},
255 {"InflationCurves", CurveSpec::CurveType::Inflation},
256 {"InflationCapFloorVolatilities", CurveSpec::CurveType::InflationCapFloorVolatility},
257 {"EquityCurves", CurveSpec::CurveType::Equity},
258 {"EquityVolatilities", CurveSpec::CurveType::EquityVolatility},
259 {"Securities", CurveSpec::CurveType::Security},
260 {"CommodityCurves", CurveSpec::CurveType::Commodity},
261 {"Correlations", CurveSpec::CurveType::Correlation},
262 {"CommodityVolatilities", CurveSpec::CurveType::CommodityVolatility}};
263
264 auto it = b.find(s);
265 if (it != b.end()) {
266 return it->second;
267 } else {
268 QL_FAIL("Cannot convert \"" << s << "\" to CurveSpecType");
269 }
270}
271
272} // namespace data
273} // namespace ore
CurveType
Supported curve types.
Definition: curvespec.hpp:43
CurveSpec parser.
QuantLib::ext::shared_ptr< CurveSpec > parseCurveSpec(const string &s)
function to convert a string into a curve spec
CurveSpec::CurveType parseCurveConfigurationType(const std::string &s)
function to convert a curve configuration node string into a curve spec type
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23