Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
utilities.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
19/*! \file ored/scripting/utilities.hpp
20 \brief some utility functions
21 \ingroup utilities
22*/
23
27
31
35
36#pragma once
37
38namespace QuantLib {
39class GeneralizedBlackScholesProcess;
40}
41
42namespace ore {
43namespace data {
44
45/*! coarsens given date grid starting at eval date using the given rule, which is of the form
46 3M(1W),1Y(1M),5Y(3M),10Y(1Y),50Y(5Y)
47 the rough idea is out to 3M at least a 1W spacing is used, output 1Y a 1M spacing etc.
48 for the exact algorithm that generates the coarsened grid, see the code */
49std::vector<Date> coarsenDateGrid(const std::vector<Date>& date, const std::string& rule,
50 const Date& referenceDate = Null<Date>());
51
52/*! get product tag + script, if a name is defined in the scriptTrade, get the script from the library, otherwise
53 from the trade itself; use the give purpose and fall back on an empty purpose if specified */
54std::pair<std::string, ScriptedTradeScriptData> getScript(const ScriptedTrade& scriptedTrade,
55 const ScriptLibraryData& scriptLibrary,
56 const std::string& purpose,
57 const bool fallBackOnEmptyPurpose);
58
59/*! parse script and return ast */
60ASTNodePtr parseScript(const std::string& code);
61
62/*! convert a IR / FX / EQ index name to a correlation label that is understood by the cam builder;
63 return the tenor of the index too (or 0*Days if not applicable) */
64std::pair<std::string, Period>
65convertIndexToCamCorrelationEntry(const std::string& i);
66
67/*! check whether variable name is already present in given context, if yes throw an exception */
68void checkDuplicateName(const QuantLib::ext::shared_ptr<Context> context, const std::string& name);
69
70/*! build a context from the given data and apply the given gridCoarsening rule, if required */
71QuantLib::ext::shared_ptr<Context> makeContext(const Size nPaths, const std::string& gridCoarsening,
72 const std::vector<std::string>& schedulesEligibleForCoarsening,
73 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
74 const std::vector<ScriptedTradeEventData>& events,
75 const std::vector<ScriptedTradeValueTypeData>& numbers,
76 const std::vector<ScriptedTradeValueTypeData>& indices,
77 const std::vector<ScriptedTradeValueTypeData>& currencies,
78 const std::vector<ScriptedTradeValueTypeData>& daycounters);
79
80/*! add new schedules (as specified in the script node) to schedules */
81void addNewSchedulesToContext(QuantLib::ext::shared_ptr<Context> context,
82 const std::vector<ScriptedTradeScriptData::NewScheduleData>& newSchedules);
83
84/*! maend the variables sizes in a context to a new size, this is only possible for deterministic variables */
85void amendContextVariablesSizes(QuantLib::ext::shared_ptr<Context> context, const Size newSize);
86
87/*! - helper class that takes and index name string and provides the index type and a parsed version of
88 the index with no market data attached
89 - commodity indices can be of the extended form for scripting, see parseScriptedCommodityIndex for details
90 - if a market is given, the class attempts to retrieve an eq index from the market, so that it has the
91 correct business day calendar (market curve will be attached to the index too in this case)
92*/
93class IndexInfo {
94public:
95 // ctor taking the ORE name of an index and conventions
96 explicit IndexInfo(const std::string& name, const QuantLib::ext::shared_ptr<Market>& market = nullptr);
97 // the (ORE, i.e. Input) name of the index
98 std::string name() const { return name_; }
99 // type of index
100 bool isFx() const { return isFx_; }
101 bool isEq() const { return isEq_; }
102 bool isComm() const { return isComm_; }
103 bool isIr() const { return isIr_; }
104 bool isIrIbor() const { return isIrIbor_; }
105 bool isIrSwap() const { return isIrSwap_; }
106 bool isInf() const { return isInf_; }
107 bool isGeneric() const { return isGeneric_; }
108 // get parsed version of index, or null if na
109 QuantLib::ext::shared_ptr<FxIndex> fx() const { return fx_; }
110 QuantLib::ext::shared_ptr<EquityIndex2> eq() const { return eq_; }
111 QuantLib::ext::shared_ptr<QuantExt::CommodityIndex>
112 // requires obsDate + conventions for forms 3-6 below, throws otherwise
113 comm(const Date& obsDate = Date()) const;
114 QuantLib::ext::shared_ptr<InterestRateIndex> ir() const { return ir_; }
115 QuantLib::ext::shared_ptr<IborIndex> irIbor() const { return irIbor_; }
116 // nullptr if it is no ibor fallback index
117 QuantLib::ext::shared_ptr<FallbackIborIndex> irIborFallback(const IborFallbackConfig& iborFallbackConfig,
118 const Date& asof = QuantLib::Date::maxDate()) const;
119 // nullptr if it is no overnight fallback index
120 QuantLib::ext::shared_ptr<FallbackOvernightIndex> irOvernightFallback(const IborFallbackConfig& iborFallbackConfig,
121 const Date& asof = QuantLib::Date::maxDate()) const;
122 QuantLib::ext::shared_ptr<SwapIndex> irSwap() const { return irSwap_; }
123 QuantLib::ext::shared_ptr<ZeroInflationIndex> inf() const { return inf_; }
124 QuantLib::ext::shared_ptr<Index> generic() const { return generic_; }
125 // get ptr to base class (comm forms 3-6 below require obsDate + conventions, will throw otherwise)
126 QuantLib::ext::shared_ptr<Index>
127 index(const Date& obsDate = Date()) const;
128 // get comm underlying name NYMEX:CL (no COMM- prefix, no suffixes)
129 std::string commName() const;
130 // get inf name INF-EUHICP (i.e. without the #L, #F suffix)
131 std::string infName() const;
132 // comparisons (based on the name)
133 bool operator==(const IndexInfo& j) const { return name() == j.name(); }
134 bool operator!=(const IndexInfo& j) const { return !(*this == j); }
135 bool operator<(const IndexInfo& j) const { return name() < j.name(); }
136 bool operator>(const IndexInfo& j) const { return j < (*this); }
137 bool operator<=(const IndexInfo& j) const { return !((*this) > j); }
138 bool operator>=(const IndexInfo& j) const { return !((*this) < j); }
139
140private:
141 std::string name_;
142 QuantLib::ext::shared_ptr<Market> market_; // might be null
144 QuantLib::ext::shared_ptr<FxIndex> fx_;
145 QuantLib::ext::shared_ptr<EquityIndex2> eq_;
146 QuantLib::ext::shared_ptr<InterestRateIndex> ir_;
147 QuantLib::ext::shared_ptr<IborIndex> irIbor_;
148 QuantLib::ext::shared_ptr<SwapIndex> irSwap_;
149 QuantLib::ext::shared_ptr<ZeroInflationIndex> inf_;
150 QuantLib::ext::shared_ptr<Index> generic_;
151 std::string commName_, infName_;
152};
153
154std::ostream& operator<<(std::ostream& o, const IndexInfo& i);
155
156/*! This method tries to parse an commodity index name used in the scripting context
157
158 0) COMM-name
159 1) COMM-name-YYYY-MM-DD
160 2) COMM-name-YYYY-MM
161 ===
162 3) CMMM-name#N#D#Cal
163 4) COMM-name#N#D
164 5) COMM-name#N
165 ===
166 6) COMM-name!N
167
168 Here 0) - 2) are corresponding to the usual ORE conventions while 3) - 6) are specific to the scripting
169 module: Expressions of the form 3) - 5) are resolved to one of the forms 1) and 2) using a given commodity
170 future expiry calculator as follows:
171
172 3) COMM-name#N#D#Cal is resolved to the (N+1)th future with expiry greater than the given obsDate advanced by D
173 business days w.r.t. Calendar Cal, N >= 0
174 4) as 3), Cal is taken as the commodity index's fixing calendar
175 5) as 4), D is set to 0 if not given
176 6) COMM-name!N is resolved to the future with month / year equal to the obsDate and monthOffst = N, N >=0
177
178 Notice that the forms 1) and 2) can be parsed without an obsDate and a commodity future convention given. If no
179 convention is given, the fixing calendar in the index is set to the NullCalendar. In case a commodity future
180 convention is given for the name, the fixing calendar is set to the calendar from the convention.
181
182 TODO if the form is COMM-name-YYYY-MM, the day of month of the expiry date will be set to 01, consistently
183 with the ORE index parser, even if a convention is present, that would allow us to determine the correct
184 expiry date. Should we use that latter date in the returned index?
185
186 Forms 3) to 6) on the other hand require a commodity future convention in any case, and an obsDate.
187*/
188
189QuantLib::ext::shared_ptr<QuantExt::CommodityIndex> parseScriptedCommodityIndex(const std::string& indexName,
190 const QuantLib::Date& obsDate = Date());
191
192/*! This method tries to parse an inflation index name used in the scripting context
193
194 1) EUHICPXT
195 2) EUHICPXT#F
196 3) EUHICPXT#L
197
198 Here 1) is the original form used in ORE. This represents a non-interpolated index.
199 2,3) is the extended form including a flag indicating the interpolation F (flat, =1) or L (linear)
200
201 The function returns a ql inflation index accounting for the interpolation (but without ts attached),
202 and the ORE index name without the #F, #L suffix.
203*/
204std::pair<QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex>, std::string>
205parseScriptedInflationIndex(const std::string& indexName);
206
207/*! Builds an index (EQ-SP5-EUR, FX-ECB-EUR-USD, ...) that can be used in scripted trades, from an underlying */
208std::string scriptedIndexName(const QuantLib::ext::shared_ptr<Underlying>& underlying);
209
210/*! Get inflation simulation lag in calendar days */
211Size getInflationSimulationLag(const QuantLib::ext::shared_ptr<ZeroInflationIndex>& index);
212
213/*! Get map index => calibration strikes as vector<Real> from calibration spec and context */
214std::map<std::string, std::vector<Real>>
215getCalibrationStrikes(const std::vector<ScriptedTradeScriptData::CalibrationData>& calibrationSpec,
216 const QuantLib::ext::shared_ptr<Context>& context);
217
218} // namespace data
219} // namespace ore
abstract syntax tree for payoff scripting
QuantLib::ext::shared_ptr< Index > index(const Date &obsDate=Date()) const
Definition: utilities.cpp:449
std::string infName() const
Definition: utilities.cpp:479
bool operator>=(const IndexInfo &j) const
Definition: utilities.hpp:138
QuantLib::ext::shared_ptr< Market > market_
Definition: utilities.hpp:142
QuantLib::ext::shared_ptr< ZeroInflationIndex > inf_
Definition: utilities.hpp:149
std::string name() const
Definition: utilities.hpp:98
QuantLib::ext::shared_ptr< IborIndex > irIbor_
Definition: utilities.hpp:147
bool isComm() const
Definition: utilities.hpp:102
QuantLib::ext::shared_ptr< SwapIndex > irSwap() const
Definition: utilities.hpp:122
bool operator==(const IndexInfo &j) const
Definition: utilities.hpp:133
QuantLib::ext::shared_ptr< ZeroInflationIndex > inf() const
Definition: utilities.hpp:123
bool isIr() const
Definition: utilities.hpp:103
QuantLib::ext::shared_ptr< FxIndex > fx_
Definition: utilities.hpp:144
bool isIrIbor() const
Definition: utilities.hpp:104
std::string infName_
Definition: utilities.hpp:151
QuantLib::ext::shared_ptr< InterestRateIndex > ir_
Definition: utilities.hpp:146
bool operator>(const IndexInfo &j) const
Definition: utilities.hpp:136
QuantLib::ext::shared_ptr< EquityIndex2 > eq_
Definition: utilities.hpp:145
bool operator!=(const IndexInfo &j) const
Definition: utilities.hpp:134
bool operator<(const IndexInfo &j) const
Definition: utilities.hpp:135
QuantLib::ext::shared_ptr< EquityIndex2 > eq() const
Definition: utilities.hpp:110
QuantLib::ext::shared_ptr< FallbackIborIndex > irIborFallback(const IborFallbackConfig &iborFallbackConfig, const Date &asof=QuantLib::Date::maxDate()) const
Definition: utilities.cpp:501
bool isGeneric() const
Definition: utilities.hpp:107
QuantLib::ext::shared_ptr< SwapIndex > irSwap_
Definition: utilities.hpp:148
QuantLib::ext::shared_ptr< InterestRateIndex > ir() const
Definition: utilities.hpp:114
QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > comm(const Date &obsDate=Date()) const
Definition: utilities.cpp:467
QuantLib::ext::shared_ptr< IborIndex > irIbor() const
Definition: utilities.hpp:115
std::string commName_
Definition: utilities.hpp:151
QuantLib::ext::shared_ptr< Index > generic_
Definition: utilities.hpp:150
bool isFx() const
Definition: utilities.hpp:100
QuantLib::ext::shared_ptr< FxIndex > fx() const
Definition: utilities.hpp:109
bool operator<=(const IndexInfo &j) const
Definition: utilities.hpp:137
bool isIrSwap() const
Definition: utilities.hpp:105
bool isEq() const
Definition: utilities.hpp:101
QuantLib::ext::shared_ptr< FallbackOvernightIndex > irOvernightFallback(const IborFallbackConfig &iborFallbackConfig, const Date &asof=QuantLib::Date::maxDate()) const
Definition: utilities.cpp:514
std::string commName() const
Definition: utilities.cpp:474
bool isInf() const
Definition: utilities.hpp:106
script engine context holding variable names and values
Map text representations to QuantLib/QuantExt types.
@ data
Definition: log.hpp:77
Date referenceDate
Definition: utilities.cpp:442
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
void amendContextVariablesSizes(QuantLib::ext::shared_ptr< Context > context, const Size newSize)
Definition: utilities.cpp:379
QL_DEPRECATED_ENABLE_WARNING std::string scriptedIndexName(const QuantLib::ext::shared_ptr< Underlying > &underlying)
Definition: utilities.cpp:614
std::vector< Date > coarsenDateGrid(const std::vector< Date > &dates, const std::string &rule, const Date &referenceDate)
Definition: utilities.cpp:41
ASTNodePtr parseScript(const std::string &code)
Definition: utilities.cpp:121
QL_DEPRECATED_DISABLE_WARNING std::pair< QuantLib::ext::shared_ptr< QuantLib::ZeroInflationIndex >, std::string > parseScriptedInflationIndex(const std::string &indexName)
Definition: utilities.cpp:593
void addNewSchedulesToContext(QuantLib::ext::shared_ptr< Context > context, const std::vector< ScriptedTradeScriptData::NewScheduleData > &newSchedules)
Definition: utilities.cpp:322
std::map< std::string, std::vector< Real > > getCalibrationStrikes(const std::vector< ScriptedTradeScriptData::CalibrationData > &calibrationSpec, const QuantLib::ext::shared_ptr< Context > &context)
Definition: utilities.cpp:673
void checkDuplicateName(const QuantLib::ext::shared_ptr< Context > context, const std::string &name)
Definition: utilities.cpp:156
Size getInflationSimulationLag(const QuantLib::ext::shared_ptr< ZeroInflationIndex > &index)
Definition: utilities.cpp:660
std::pair< std::string, ScriptedTradeScriptData > getScript(const ScriptedTrade &scriptedTrade, const ScriptLibraryData &scriptLibrary, const std::string &purpose, const bool fallBackOnEmptyPurpose)
Definition: utilities.cpp:105
QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > parseScriptedCommodityIndex(const std::string &indexName, const QuantLib::Date &obsDate)
Definition: utilities.cpp:530
std::pair< std::string, Period > convertIndexToCamCorrelationEntry(const std::string &i)
Definition: utilities.cpp:138
QuantLib::ext::shared_ptr< ASTNode > ASTNodePtr
Definition: ast.hpp:46
QuantLib::ext::shared_ptr< Context > makeContext(Size nPaths, const std::string &gridCoarsening, const std::vector< std::string > &schedulesEligibleForCoarsening, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData, const std::vector< ScriptedTradeEventData > &events, const std::vector< ScriptedTradeValueTypeData > &numbers, const std::vector< ScriptedTradeValueTypeData > &indices, const std::vector< ScriptedTradeValueTypeData > &currencies, const std::vector< ScriptedTradeValueTypeData > &daycounters)
Definition: utilities.cpp:163
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Reference data model and serialization.
scripted trade data model
string name
underlying data model